<?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>']];

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

/* START EDITABLE GRID */

store = new Ext.data.JsonStore({
	url:'data/getstudent-atkt.php',
	root: 'data',
	storeId: 'student_id',
	totalProperty: 'total',
	fields: [{
		name:'student_id',
		type:'int'
	},{
		name:'student_name',
		type:'string'
	},{
		name:'student_gturegno',
		type:'int'
	},{
		name:'status',
		type:'bool'
	}] 
});

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: '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: 'ATKT',
			id: 'status',
			dataIndex: 'status',
			width:50	
		}
	],
	height:280,
	layout: 'fit',
	sm: new Ext.grid.RowSelectionModel({
		singleSelect: true
	}),
	loadMask: true
});

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();
	
	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 subject_name 	= Ext.getCmp('subject_name').getValue();
	var division = Ext.getCmp('division_id').getValue();

	if(branch_name != '' && semester_name != '' && subject_name != ''){	
	
		Ext.Ajax.request({
			loadMask: true,
			sync : true,
			url: 'data/check-duplication.php',
			params: {
				action:'atkt',
				branch_name:branch_name,
				semester_name:semester_name
			},
			success: function(resp) {
				resbool=Ext.decode(resp.responseText).success;			                     
				if(resbool=='false'){
					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,
						subject_name:subject_name
						
					}});
				}
			}				
		});	
	}
	else{
		store.removeAll();	
	}}

Ext.onReady(function(){
	Ext.QuickTips.init();
	
	var ExamForm = new Ext.FormPanel({
		id:'ExamForm',
    	labelWidth:70,
        url:'save-atkt.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:.5,
				items:[branch,divisionCombo]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.5,
				items:[semester,subject]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				style:{padding:'5px 0px 0px 0px'},
				columnWidth:1,
				items:[grid]
			}]
		}]
 	});
	
	var ExamWindow = new Ext.Window({
        title:'Allowed To Keep Term (ATKT)',
        height:425,
        width:580,
		modal:true,
        layout:'fit',
		border:false,
		closeAction:'close',
		collapsible:'true',
		items:ExamForm,
		buttonAlign:'center',
	    buttons:[{
			text: 'Save', 
			name:'btnSave',
			tabIndex:5,
			id:'btnSave',
			iconCls:'icon-save',
			handler: function(){
				grid.stopEditing();	
				if(grid.store.getCount()>=1){
					var arr_studentid = " ";
					var arr_status = " ";
										
					for(var i = 0, len = grid.store.getCount(); i < len; i++){
						record = grid.store.getAt(i);
						student_id = record.data.student_id;
						status = record.data.status;
						
						if(arr_studentid != " "){
							arr_studentid = arr_studentid + "," + student_id;	
						} else{
							arr_studentid = student_id;	
						}
						
						if(arr_status != " "){
							arr_status = arr_status + "," + status;	
						} else{
							arr_status = status;	
						}
					}
					ExamForm.getForm().submit({
						params:{student_id:arr_studentid,status:arr_status},	
						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 valid form no.',
						buttons: Ext.MessageBox.OK,
						icon:Ext.MessageBox.ERROR
					});
				}
			}	
		},{
			text: 'Reset', 
			name:'btnreset',
			tabIndex:7,
			id:'btnreset',iconCls:'icon-reset',
			handler: function (){
				ExamForm.getForm().reset();
				grid.getSelectionModel().clearSelections();
				//Ext.getCmp('btnDelete').disable();
				Ext.getCmp('branch_name').focus(true,500);
				if(flgAdd == "true"){
								Ext.getCmp('btnSave').enable();
				 }
			}
		},{
			text:'Close', 
			name:'btnclose',
			tabIndex:8,
			iconCls:'icon-close',
			id:'btnclose',
			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>
<?php require "_footer.php";?>