<?php 
	require "_header.php";
	require "data/combo_semester.php";


				$out1 = "select * from cms_marksdate";
$out = dbQuery($out1);
if (dbRows($out)){
						while($row1 = dbArray($out))
						{
							$date = $row1['date_name'];	
						}
				}  
			$sysdate = date("d/m/Y");
		//	echo $sysdate; exit;
		$d= date("d/m/Y",strtotime($date));

?>
<div id="main-content">
<script type="text/javascript" src="jquery/ux/CheckColumn.js"></script>
<script type="text/javascript">
<?php if($_SESSION["VedantnrUserRole"] !="PowerUser" &&  $sysdate > $d ) {?>
	
alert('Marks Entry Not After <?php echo date("d/m/Y",strtotime($date)); ?>')
<?php
}
?>
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>']];

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){
		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){
		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:'5',
	allowBlank:false,
	store: subjectstore,
    listeners:{'select': function(cmb, rec, idx){
		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:'4',
	allowBlank:false,
	store: examstore,
    listeners:{'select': function(cmb, rec, idx){
		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:false,
	store: teststore,
    listeners:{'select': function(cmb, rec, idx){
		LoadData1();
	}}
});

/* START EDITABLE GRID */

store = new Ext.data.JsonStore({
	url:'data/getstudent-exam.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',
		type:'bool'
	},{
		name:'obtain_mark',	
		type:'string'
	},{
		name:'present_pr',
		type:'bool'
	},{
		name:'total_marks_pr_ese',
		type:'int'
	},{
		name:'total_marks_pr_pa',
		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_name',
			header: 'Student Name',
			sortable:true,
			id: 'student_name',
			width:200
		},{
			dataIndex: 'student_gturegno',
			header: 'Enrollment No',
			sortable:true,
			id: 'student_gturegno',
			width:125	
		},{ 
			xtype: 'checkcolumn',
			header: 'Present',
			id: 'present',
			dataIndex: 'present',
			width:50	
		},{
			dataIndex: 'obtain_mark',
			header: 'Obtain Marks',
			sortable:true,
			width:90, 
			
			
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank: false,
				maxLength:2,
				allowNegative: false,
				allowBlank: false,
				<?php if($_SESSION["VedantnrUserRole"] !="PowerUser" &&  $sysdate > $d  ) {?>
				readOnly:true,
			<?php 
			} else { ?>	
				readOnly:false,
			<?php } ?>
				autoCreate: {tag: 'input', type: 'text',  maxlength: '2' },
				
				
			}
		}
		
		
		,{ 
			xtype: 'checkcolumn',
			header: 'Prc. Present',
			id: 'present_pr',
			dataIndex: 'present_pr',
			width:70	
		},{
			dataIndex:'obtain_marks_pr_ese',
			header: 'Obtain Marks[ESE]',
			sortable:true,
			width:100, 
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank: false,
				maxLength:2,
				<?php if($_SESSION["VedantnrUserRole"] !="PowerUser" &&  $sysdate > $d) {?>
				readOnly:true,
			<?php 
			} else { ?>	
				readOnly:false,
			<?php } ?>
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '2'}
			}
		},{
			dataIndex: 'obtain_marks_pr_pa',
			header: 'Obtain Marks[PA]',
			sortable:true,
			width:100, 
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank: false,
				maxLength:2,
				allowNegative: false,
				<?php if($_SESSION["VedantnrUserRole"] !="PowerUser" &&  $sysdate > $d ) {?>
				readOnly:true,
			<?php 
			} else { ?>	
				readOnly:false,
			<?php } ?>
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '2'}
			}
		}
	],
	height:290,
	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);
	var flg=0;
	var max_mark=0;
	if(record.data.total_marks < record.data.obtain_mark){
		flg=1;
		max_mark=record.data.total_marks;
	}
	if(record.data.total_marks_pr_pa < record.data.obtain_marks_pr_pa){
		flg=1;
		max_mark=record.data.total_marks_pr_pa;
	}
	if(record.data.total_marks_pr_ese < record.data.obtain_marks_pr_ese){
		flg=1;
		max_mark=record.data.total_marks_pr_ese;
	}
	
	if(flg==1){
	grid.stopEditing();
		Ext.MessageBox.show({
			title: 'Error',
			msg: 'Total marks for this subject ' + max_mark + '. 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();

	if(branch_name != '' && semester_name != '' && division!='' && exam_name != '' && subject_name != '' && test_type != ''){	
	
		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
					}});
				}
			}				
		});	
	
		
	}
	else{
		store.removeAll();	
	}}

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


	
	var ExamForm = new Ext.FormPanel({
		id:'ExamForm',
    	labelWidth:70,
        url:'save-exammarks.php',
		frame:true,
		title:'',
        bodyStyle:'padding:6px 6px',
        layout:'form',
		defaultType: 'textfield',
        items :[{
			xtype:'container',
			autoEl:{},
			border:false,
			layout:'column',
			anchor:'100%',
			defaultType:'field',
			items:[{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.33,
				items:[branch,exam]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.33,
				items:[semester,subject]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.33,
				items:[divisionCombo,
				
				test]
			}
			
			,{
				xtype:'container',
				autoEl:{},
				layout:'form',
				style:{padding:'5px 0px 0px 0px'},
				columnWidth:1,
				items:[grid]
			}]
		}]
 	});
	
	var ExamWindow = new Ext.Window({
        title:'Exam Marks Entry',
        height:425,
        width:850,
		modal:true,
        layout:'fit',
		border:false,
		closeAction:'close',
		collapsible:'true',
		items:ExamForm,
		buttonAlign:'center',
	    buttons:[{
			text: 'Save', 
			iconCls:'icon-save',
			name:'btnSave',
			tabIndex:5,
			id:'btnSave',
			handler: function(){
				grid.stopEditing();	
				if(grid.store.getCount()>=1){
					var arr_studentid = " ";
					var arr_obtainmark = " ";
					var student_name = " ";
					var arr_present = " ";
					var arr_obtainmark_pr_pa = " ";
					var arr_obtainmark_pr_ese = " ";
					var arr_present_pr = " ";
					
					var flg=0;
					var at="";
					for(var i = 0, len = grid.store.getCount(); i < len; i++){
						record = grid.store.getAt(i);
						student_id = record.data.student_id;
						obtain_mark = record.data.obtain_mark;
						present = record.data.present;
						obtainmark_pr_pa = record.data.	obtain_marks_pr_pa;
						obtainmark_pr_ese = record.data.obtain_marks_pr_ese;
						present_pr = record.data.present_pr;
						
						if(present==true && obtain_mark==="" ){
								
							flg=1;
							student_name = record.data.student_name;
							at=	i;
							break;
						}
						else
						{
							arr_studentid+=student_id +",";	
							arr_obtainmark+= obtain_mark+",";	
							arr_present+=present+",";	
							arr_obtainmark_pr_pa+=obtainmark_pr_pa+",";
							arr_obtainmark_pr_ese+=obtainmark_pr_ese+",";
							arr_present_pr+=present_pr+",";
							
						}
						
					}
					if(flg==0)
					{
						ExamForm.getForm().submit({
						params:{student_id:arr_studentid,obtain_mark:arr_obtainmark,present:arr_present,obtainmark_pr_pa:arr_obtainmark_pr_pa,obtainmark_pr_ese:arr_obtainmark_pr_ese,present_pr:arr_present_pr},	
						waitMsg: 'Saving...',		
						success: function () {		
							Ext.MessageBox.show({
								title: 'Message',
								msg: 'Data has been saved..',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.INFO
							});
							ExamForm.getForm().reset();
							store.removeAll();
						},
						failure: function () {	
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Please fill complete form!',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.ERROR
							});
						}
					});
					}
					else
					{
						Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please Enter Marks For Student <span class="comp">'+student_name+'</span> Or Make Student Absent.',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});	
								grid.getSelectionModel().selectRow(at);
					}
				} else{
					Ext.MessageBox.show({
						title: 'Error',
						msg: 'Please enter valid form no.',
						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";?>