<?php 
	require "_header.php";
	require "data/combo_semester.php";
?>
<div id="main-content">
<script type="text/javascript" src="jquery/ux/CheckColumn.js"></script>
<script type="text/javascript">
var labelArray;
labelArray = [
	['Branch:<span class="comp">*</span>'],
	['Semester:<span class="comp">*</span>'],
	['Subject:<span class="comp">*</span>'],
	['Exam:<span class="comp">*</span>'],
	['Test:<span class="comp"></span>'],
	['Result:<span class="comp">*</span>']];

var branchstore = new Ext.data.ArrayStore({		
	fields: ['branch_id','branch_name'],
	data:Ext.semester.branch
});

var semesterstore = new Ext.data.ArrayStore({		
	fields: ['semester_id','semester_name'],
	data:Ext.semester.semester
});

var subjectstore = new Ext.data.JsonStore({
	url:'data/get-subject.php',
    root: 'data',
	totalProperty: 'total',
	fields: ['subject_id','subject_name'] 
});

var examstore = new Ext.data.ArrayStore({		
	fields: ['exam_id','exam_name'],
	data:Ext.semester.exam
});

var divisionstore = new Ext.data.ArrayStore({
    	fields:['division_id','division_name'],
		data: Ext.semester.division
	});
	
	
var branch = new Ext.form.ComboBox({
	name:'branch_name',
	id:'branch_name',
	hiddenName:'branch_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[0] ,
	displayField:'branch_name',
	valueField: 'branch_id',
	editable:true,
	tabIndex:'1',
	forceSelection:true,
	triggerAction:'all',
	mode:'local',
	//width: 100,
	allowBlank:false,
	store:branchstore,
    listeners:{'select': function(cmb, rec, idx){
		Ext.getCmp('branch_disp').setValue(this.getRawValue());
		LoadData();
		LoadData1();
		Ext.getCmp('division_id').reset();
	}}
});

var semester = new Ext.form.ComboBox({
	name:'semester_name',
	id:'semester_name',
	hiddenName:'semester_hidden',
	labelSeparator:'',	
	fieldLabel:labelArray[1] ,
	displayField:'semester_name',
	valueField:'semester_id',
	editable:true,
	forceSelection:true,
	triggerAction:'all',
	mode:'local',
	tabIndex:'2',
	//width: 100,
	allowBlank:false,
	store:semesterstore,
    listeners:{'select': function(cmb, rec, idx){
		Ext.getCmp('semester_disp').setValue(this.getRawValue());
		LoadData();
		LoadData1();
		Ext.getCmp('division_id').reset();
		var branch=Ext.getCmp('branch_name').getValue();
		divisionstore.filter('division_id',branch+cmb.getValue(),false,true,true);
		var cnt=divisionstore.getCount();
		if(cnt==1)
		{
			Ext.getCmp('division_id').setValue(divisionstore.getAt(0).data.division_name);
		}
	}}
});

var divisionCombo = {		
						xtype: 'combo',
						name: 'division_id',
						id:'division_id',
						labelSeparator:'',	
						fieldLabel :'Division :<span class="comp">*</span>' ,
						forceSelection:true,				
						displayField:'division_name',
						valueField: 'division_name'
						,tabIndex:3
						,lastQuery:''
						,allowBlank:false
						,triggerAction:'all'
						,mode:'local'
						,store:divisionstore
						,selectOnFocus: true,
						listeners: {'select': function(cmb, rec, idx) {
						
						LoadData();
						LoadData1();
					}
    }}

var subject = new Ext.form.ComboBox({
	name: 'subject_name',
	id:'subject_name',
	hiddenName:'subject_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[2] ,
	displayField:'subject_name',
	valueField: 'subject_id',
	editable:true,
	forceSelection:true,	
	triggerAction:'all',
	mode:'local',
	//width: 127,
	tabIndex:'4',
	allowBlank:false,
	store: subjectstore,
    listeners:{'select': function(cmb, rec, idx){
		Ext.getCmp('subject_disp').setValue(this.getRawValue());
		LoadData1();
	}}
});

var exam = new Ext.form.ComboBox({
	name: 'exam_name',
	id:'exam_name',
	hiddenName:'exam_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[3] ,
	displayField:'exam_name',
	valueField: 'exam_id',
	editable:true,
	triggerAction:'all',
	forceSelection:true,
	mode:'local',
	//width: 100,
	tabIndex:'3',
	allowBlank:false,
	store: examstore,
    listeners:{'select': function(cmb, rec, idx){
		Ext.getCmp('exam_disp').setValue(this.getRawValue());
		LoadData1();
	}}
});

var teststore = new Ext.data.ArrayStore({		
	fields: ['test_val','test_disp'],
	data:[['firsttest','First Test'],['retest','Retest']]
});

var test = new Ext.form.ComboBox({
	name: 'test_type',
	id:'test_type',
	hiddenName:'test_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[4] ,
	displayField:'test_disp',
	valueField: 'test_val',
	editable:true,
	triggerAction:'all',
	forceSelection:true,
	mode:'local',
	//width: 100,
	tabIndex:'5',
	allowBlank:true,
	store: teststore,
    listeners:{'select': function(cmb, rec, idx){
		LoadData1();
	}}
});

var resultstore = new Ext.data.ArrayStore({		
	fields: ['result'],
	data:[['Both(Pass-Fail)'],['Pass'],['Fail']]
});

var result = new Ext.form.ComboBox({
	name:'result',
	id:'result',
	labelSeparator:'',	
	fieldLabel :labelArray[5],
	displayField:'result',
	valueField: 'result',
	editable:true,
	forceSelection:true,
	triggerAction:'all',
	mode:'local',
	allowBlank:false,
	store:resultstore,
	value:'Both(Pass-Fail)',
	listeners:{'select': function(cmb, rec, idx){
		LoadData1();
	}}
});


var branch_disp = new Ext.form.TextField({
	name: 'branch_disp',
	id:'branch_disp',
	hidden:true
});

var semester_disp = new Ext.form.TextField({
	name: 'semester_disp',
	id:'semester_disp',
	hidden:true
});

var exam_disp = new Ext.form.TextField({
	name: 'exam_disp',
	id:'exam_disp',
	hidden:true
});

var subject_disp = new Ext.form.TextField({
	name: 'subject_disp',
	id:'subject_disp',
	hidden:true
});


/* START EDITABLE GRID */

store = new Ext.data.JsonStore({
	url:'data/getstudent-exam-mark.php',
	root: 'data',
	storeId: 'student_id',
	totalProperty: 'total',
	fields: [{
		name:'student_id',
		type:'int'
	},{
		name:'total_marks',
		type:'int'
	},{
		name:'passing_marks',
		type:'int'
	},{
		name:'student_name',
		type:'string'
	},{
		name:'student_gturegno',
		type:'int'
	},{
		name:'present'
	},{
		name:'obtain_mark',	
		type:'string'
	},{
		name:'obtain_mark_re',	
		type:'string'
	},{
		name:'present_pr'
	},{
		name:'total_marks_pr_ese',
		type:'int'
	},{
		name:'total_marks_pr_pa',
		type:'int'
	},{
		name:'passing_marks_pr_pa',
		type:'int'
	},{
		name:'passing_marks_pr_ese',
		type:'int'
	},{
		name:'obtain_marks_pr_ese',
		type:'string'
	},{
		name:'obtain_marks_pr_pa',	
		type:'string'
	}] 
});

var grid = new Ext.grid.EditorGridPanel({
	store: store,
	clicksToEdit:1,
	columns: [
		new Ext.grid.RowNumberer(),
		{
			dataIndex: 'student_id',
			header: 'Student ID',
			id: 'student_id',
			hidden: true
		},{
			dataIndex: 'total_marks',
			header: 'total_marks',
			id: 'total_marks',
			hidden: true
		},{
			dataIndex: 'passing_marks',
			header: 'passing_marks',
			id: 'passing_marks',
			hidden: true
		},{
			dataIndex: 'student_gturegno',
			header: 'Enrollment Reg No',
			sortable:true,
			id: 'student_gturegno',
			width:90	
		},{
			dataIndex: 'student_name',
			header: 'Student Name',
			sortable:true,
			id: 'student_name',
			width:180
		},{ 
			header: 'Theory Total',
			id: 'total_marks',
			dataIndex: 'total_marks',
			width:50	
		},{ 
			header: 'Theory Passing',
			id: 'passing_marks',
			dataIndex: 'passing_marks',
			width:50	
		},/*{ 
			header: 'Theory Present',
			id: 'present',
			dataIndex: 'present',
			width:50,
			renderer: function(v, params, record){
				if(v =='P'){				 
					return '<span style="color:green;font-weight:bold;">' + v + '</span>';
				}else{
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">' + v + '</span>';
				}			
        	}	
		},*/{
			dataIndex: 'obtain_mark',
			header: 'First Obtain',
			sortable:true,
			width:50,
			renderer: function(v, params, record){
			var present=record.get('present');
			var passing_marks=record.get('passing_marks');
				if(present =='P'){				 
					if(v>=passing_marks){
					return v;
					}else{
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">' + v + '</span>';
					}
				}else{
					return '--';
				}
							
        	}	
			
		},{
			dataIndex: 'obtain_mark_re',
			header: 'Retest Obtain',
			sortable:true,
			width:50,
			renderer: function(v, params, record){
			var present=record.get('present');
			var passing_marks=record.get('passing_marks');
			if(v>=passing_marks){
					return v;
			}else{
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">' + v + '</span>';
			}
		  }	
			
		},/*{ 
			header: 'Pre Present',
			id: 'present_pr',
			dataIndex: 'present_pr',
			width:50,
			renderer: function(v, params, record){
				if(v =='P'){				 
					return '<span style="color:green;font-weight:bold;">' + v + '</span>';
				}else{
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">' + v + '</span>';
				}			
        	}	
		},*/{ 
			header: 'Total [ESE]',
			id: 'total_marks_pr_ese',
			dataIndex: 'total_marks_pr_ese',
			width:50	
		},{ 
			header: 'Passing [ESE]',
			id: 'passing_marks_pr_ese',
			dataIndex: 'passing_marks_pr_ese',
			width:50	
		},{ 
			header: 'Obtain [ESE]',
			id: 'obtain_marks_pr_ese',
			dataIndex: 'obtain_marks_pr_ese',
			width:50,
			renderer: function(v, params, record){
			var present=record.get('present_pr');
			var passing_marks=record.get('passing_marks_pr_ese');
				if(present =='P'){				 
					if(v>=passing_marks){
					return v;
					}else{
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">' + v + '</span>';
					}
				}else{
					return '--';
				}
							
        	}		
		}
		
		,{ 
			header: 'Total [PA]',
			id: 'total_marks_pr_pa',
			dataIndex: 'total_marks_pr_pa',
			width:40	
		},{ 
			header: 'Passing [PA]',
			id: 'passing_marks_pr_pa',
			dataIndex: 'passing_marks_pr_pa',
			width:60	
		},{ 
			header: 'Obtain [PA]',
			id: 'obtain_marks_pr_pa',
			dataIndex: 'obtain_marks_pr_pa',
			width:60,
			renderer: function(v, params, record){
			var present=record.get('present_pr');
			var passing_marks=record.get('passing_marks_pr_pa');
				if(present =='P'){				 
					if(v>=passing_marks){
					return v;
					}else{
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">' + v + '</span>';
					}
				}else{
					return '--';
				}
							
        	}		
		}/*,
		
		
		
		{
			header: 'Result',
			sortable:true,
			width:55,
			renderer: function(v, params, record){
			var obtain_mark=record.get('obtain_mark');
			var present=record.get('present');
			var passing_marks=record.get('passing_marks');
				if(present =='P'){				 
					if(obtain_mark<passing_marks){
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">Fail</span>';
					}else{
						return '<span style="color:green;font-weight:bold;">Pass</span>';
					}
				}else{
					return '<span style="color:red;font-weight:bold;text-decoration:underline;">Fail</span>';
				}
							
        	}	
			
		}*/
	],
	height:280,
	layout: 'fit',
	sm: new Ext.grid.RowSelectionModel({
			singleSelect: true
		}),
	loadMask: true,
	columnLines:true,
	stripeRows:true
});

grid.on('render', function() {
	grid.tip = new Ext.ToolTip({
		view: grid.getView(),
		target: grid.getView().mainBody,
		delegate: '.x-grid3-row',
		trackMouse: true,
		renderTo: document.body,
		listeners: {
			beforeshow: function updateTipBody(tip) {
				tip.body.dom.innerHTML = "Click on Rollno Column To Edit Rollno.";
			}
		}
	});
});

store.on({'load': {fn: function(){
	if(store.getCount() <= 0){
		Ext.MessageBox.show({
			title: 'Error',
			msg: 'There is no Student found in this Selection.',
			buttons: Ext.MessageBox.OK,
			icon:Ext.MessageBox.ERROR
		});
	} else{
		grid.startEditing(0,3);
	}
}}});	

grid.on('afteredit',function afteredit(e){
	var th=e.value;
	var record = grid.store.getAt(e.row);
	if(record.data.total_marks < record.data.obtain_mark){
		grid.stopEditing();
		Ext.MessageBox.show({
			title: 'Error',
			msg: 'Total marks for this subject ' + record.data.total_marks + '. You can not add more than marks',
			buttons: Ext.MessageBox.OK,
			icon: Ext.MessageBox.ERROR,
			fn: function(btn){
				if (btn == 'ok'){
					record.set(e.field,' ');
					grid.getSelectionModel().selectRow(e.row);
					grid.startEditing(e.row,e.column);
				}
			}
		});  
	}
});

/* END EDITABLE GRID */

function LoadData(){
	var branch_name = Ext.getCmp('branch_name').getValue();
	var semester_name = Ext.getCmp('semester_name').getValue();
	var exam_name = Ext.getCmp('exam_name').getValue();
	

	if(branch_name != '' && semester_name != ''){	
		Ext.getCmp('subject_name').reset();
		subjectstore.load({params:{
			branch_name:branch_name,
			semester_name:semester_name
		}});
	}}

function LoadData1(){
	var branch_name 	= Ext.getCmp('branch_name').getValue();
	var semester_name 	= Ext.getCmp('semester_name').getValue();
	var exam_name 		= Ext.getCmp('exam_name').getValue();
	var subject_name 	= Ext.getCmp('subject_name').getValue();
	var test_type		= Ext.getCmp('test_type').getValue();
	var division = Ext.getCmp('division_id').getValue();
	var result = Ext.getCmp('result').getValue();

	if(branch_name != '' && semester_name != '' && division!='' && exam_name != '' && subject_name != '' ){	
	
		Ext.Ajax.request({
			loadMask: true,
			sync : true,
			url: 'data/check-duplication.php',
			params: {
				action:'result',
				branch_name:branch_name,
				semester_name:semester_name,
				division:division,
				exam_name:exam_name,
				subject_name:subject_name
			},
			success: function(resp) {
				resbool=Ext.decode(resp.responseText).success;			                     
				if(resbool=='true'){
					Ext.MessageBox.show({
						title: 'Error',
						msg: 'Can no setup for this subject. First generate setup then enter marks.',
						buttons: Ext.MessageBox.OK,
						icon:Ext.MessageBox.ERROR
					});
				}	
				else{
					store.load({params:{
						branch_name:branch_name,
						semester_name:semester_name,
						division:division,
						exam_name:exam_name,
						subject_name:subject_name,
						test_type:test_type,
						result:result
					}});
				}
			}				
		});	
	
		
	}
	else{
		store.removeAll();	
	}}

Ext.onReady(function(){
	Ext.QuickTips.init();


	
	var ExamForm = new Ext.FormPanel({
		id:'ExamForm',
    	labelWidth:70,
        url:'view-markentry-result.php',
		frame:true,
		title:'',
        bodyStyle:'padding:6px 6px',
		standardSubmit:true,
        layout:'form',
		defaultType: 'textfield',
        items :[{
			xtype:'container',
			autoEl:{},
			border:false,
			layout:'column',
			anchor:'100%',
			defaultType:'field',
			items:[{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:0.33,
				items:[branch,subject,result]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.33,
				items:[semester,exam,branch_disp,semester_disp,exam_disp]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.33,
				items:[divisionCombo,test,subject_disp]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				style:{padding:'5px 0px 0px 0px'},
				columnWidth:1,
				items:[grid]
			}]
		}]
 	});
	
	var ExamWindow = new Ext.Window({
        title:'View Marks Entry',
        height:450,
        width:940,
		modal:true,
        layout:'fit',
		border:false,
		closeAction:'close',
		collapsible:'true',
		items:ExamForm,
		buttonAlign:'center',
	    buttons:[{
			text:'View Report',
			iconCls: 'new-tab',
			handler: function (){
			var count= store.getCount();
			if(count>0){
				
			var branch_hidden=ExamForm.getForm().findField('branch_hidden').getValue();
			var semester_hidden=ExamForm.getForm().findField('semester_hidden').getValue();
			var division_id=ExamForm.getForm().findField('division_id').getValue();
			
			var branch_disp=ExamForm.getForm().findField('branch_disp').getValue();
			var semester_disp=ExamForm.getForm().findField('semester_disp').getValue();
		
			var exam_hidden=ExamForm.getForm().findField('exam_hidden').getValue();
			var test_hidden=ExamForm.getForm().findField('test_hidden').getValue();
			var subject_hidden=ExamForm.getForm().findField('subject_hidden').getValue();
			
			var semester_disp=ExamForm.getForm().findField('semester_disp').getValue();
			var exam_disp=ExamForm.getForm().findField('exam_disp').getValue();
			var subject_disp=ExamForm.getForm().findField('subject_disp').getValue();
			
			
			var redirect = 'view-markentry-result.php?branch_hidden='+branch_hidden + '&semester_hidden='+semester_hidden + 
			'&division_id='+division_id+
			'&branch_disp='+branch_disp + '&semester_disp='+semester_disp +
			'&exam_disp='+exam_disp + '&subject_disp='+subject_disp +
			 '&exam_hidden='+exam_hidden + '&test_hidden='+test_hidden + '&subject_hidden='+subject_hidden + '&semester_disp='+semester_disp; 
							window.location = redirect;		
			}
			else{
			Ext.MessageBox.show({
									title: 'Error',
									msg: "Select proper criteria.",
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});	
			}

				
			}
		},{
			text: 'Reset', 
			name:'btnreset',
			iconCls:'icon-reset',
			tabIndex:7,
			id:'btnreset',
			handler: function (){
				ExamForm.getForm().reset();
				grid.getSelectionModel().clearSelections();
				//Ext.getCmp('btnDelete').disable();
				Ext.getCmp('branch_name').focus(true,500);
				store.removeAll();
				if(flgAdd == "true"){
								Ext.getCmp('btnSave').enable();
				 }
			}
		},{
			text:'Close', 
			name:'btnclose',
			tabIndex:8,
			id:'btnclose',
			iconCls:'icon-close',
			handler: function (){
				ExamWindow.close();
			}
		}]
	});
	
	var varWidth =  (screen.width - ExamWindow.width)/2;
	ExamWindow.setPosition(varWidth,((screen.height/4)-85));
	ExamWindow.show();
	Ext.getCmp('branch_name').focus(true,1000);
});
</script>
<div id="abcd"></div>
</div>
<?php require "_footer.php";?>