<?php 
	require "_header.php"; 
	require "data/combo_semester.php";
	require "includeGridFilters.php";
?>
<style type="text/css">
	label.myBold
	{
	   font-weight: bold;
	   color:#15428B;
	   font-size: 10px;
	}
</style>
<script type="text/javascript" src="jquery/ux/CheckColumn.js"></script>
<link rel="stylesheet" type="text/css" href="css/Ext.css">
<script type="text/javascript" src="js/Ext.js"></script>
<script type="text/javascript" src="js/Ext_002.js"></script>	
<script language="javascript" type="text/javascript">
Ext.override(Ext.ux.form.LovCombo, {
	beforeBlur: Ext.emptyFn
});
Ext.onReady(function(){
	Ext.QuickTips.init();
    var abstudentSmsId ="";
	var dt = new Date();
	var xg = Ext.grid;
	var checkColumn = new Ext.grid.CheckColumn({
       	header: 'Present',
	   	id: 'day_present',
	   	dataIndex: 'day_present',
	    width: 55,
		editor:{xtype: 'checkbox',allowBlank: false}
	});	

	var shiftstore = new Ext.data.ArrayStore({
           fields: ['shift'],
	       data: [['Shift-1'],['Shift-2']]
        });
		
	var branchstore = new Ext.data.ArrayStore({		
		fields: ['branch_id','branch_name'],
		data:Ext.semester.branch
	});
	var lectypestore = new Ext.data.ArrayStore({		
		fields: ['lectype'],
		data:[['Theory'],['Practical']]
	});

	var semesterstore = new Ext.data.ArrayStore({		
		fields: ['semester_id','semester_name'],
		data:Ext.semester.semester
	});
	var divisionstore = new Ext.data.ArrayStore({
    	fields:['division_id','division_name'],
		data: Ext.semester.division
	});	     
	var subjectstore = new Ext.data.JsonStore({
	url:'data/get-subject.php',
    root: 'data',
	totalProperty: 'total',
	fields: ['subject_id','subject_name'] 
});
	
	var branch = new Ext.form.ComboBox({
		name: 'branch_name',
		id:'branch_name',
		hiddenName:'branch_hidden',
		labelSeparator:'',	
		fieldLabel :'Branch :<span class="comp">*</span>' ,
		displayField:'branch_name',
		valueField: 'branch_id',
		editable:true,
		triggerAction:'all',
		tabIndex:1,
		width:163,
		mode:'local',
		allowBlank:false,
		store: branchstore,
		listeners: {'select': function(cmb, rec, idx){
				Ext.getCmp('subject_name').reset();
				LoadData();
				grid.stopEditing();
			
		}}
	});

	var semester = new Ext.form.ComboBox({
		name: 'semester_name',
		id:'semester_name',
		hiddenName:'semester_hidden',
		labelSeparator:'',	
		fieldLabel :'Semester: <span class="comp">*</span>' ,
		displayField:'semester_name',
		valueField: 'semester_id',
		editable:true,
		tabIndex:2,
		width:163,
		triggerAction:'all',
		mode:'local',
		allowBlank:false,
		store: semesterstore,
		listeners: {'select': function(cmb, rec, idx){
			Ext.getCmp('subject_name').reset();
			LoadData();
			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('A');
			}
		}}
	});

	var subject = new Ext.form.ComboBox({
		name: 'subject_name',
		id:'subject_name',
		hiddenName:'subject_hidden',
		labelSeparator:'',	
		fieldLabel :'Subject: <span class="comp">*</span>' ,
		displayField:'subject_name',
		valueField: 'subject_id',
		editable:true,
		triggerAction:'all',
		mode:'local',
		tabIndex:6,
		width:163,
		allowBlank:false,
		store: subjectstore
	});
	
	var lectype = new Ext.form.ComboBox({
		name: 'lectype',
		id:'lectype',
		hiddenName:'lectype',
		labelSeparator:'',	
		fieldLabel :'Type: <span class="comp">*</span>' ,
		displayField:'lectype',
		valueField: 'lectype',
		editable:true,
		triggerAction:'all',
		mode:'local',
		value:'Theory',
		tabIndex:7,
		width:163,
		allowBlank:false,
		store: lectypestore
	});
 
 
 	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}});
	}}
 
	var workingstore = new Ext.data.ArrayStore({
   		fields: ['working_id','working_value'],
	    data: [['1','Working Day'],['0','Holiday']]
	});
	
	var lecstore = new Ext.data.ArrayStore({
   		fields: ['lec_id','lec_name'],
	    data:Ext.semester.lec
	});
	
	var notestore = new Ext.data.JsonStore({
    	url: 'data/note-combo.php',
    	root: 'data',
	 	fields: ['day_comment']
	});	
	var BatchStore = new Ext.data.JsonStore({
    url:'data/get-batch.php',
    root: 'data',
	remoteSort: true,
    fields: [{
        name:'batch_id',
        type: 'int'
    },{
        name: 'batch_name',
        type: 'string'
    }],autoLoad:true,
});
	store = new Ext.data.JsonStore({
		url:'data/getstudent-present.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: 'day_present'
		},{
			name: 'student_mobilestudents'
		},{
			name: 'student_mobileparents'
		}] 
	});
	
	var colModel = new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer(),
	{
		dataIndex: 'student_id',
		header: 'Student ID', 
		id: 'student_id',
		hidden: true
	},{
		dataIndex: 'student_gturegno',
		header: 'Enrollment No',
		sortable:true,
		width:120
	},{
		dataIndex: 'student_name',
		header: 'Student Name',
		sortable:true,
		width:500,
		id: 'SurName'  
	},{
		dataIndex: 'student_mobilestudents',
		header: 'Mobile Student',
		sortable:true,
		hidden: true,
		width:90
	},{
		dataIndex: 'student_mobileparents',
		header: 'Mobile Parents',
		sortable:true,
		width:100,
		hidden: true,
		id: 'student_mobileparents'  
	},checkColumn]);


	var grid = new Ext.grid.GridPanel({
        store: store,
		cm: colModel,			
        loadMask: true,
		enableColLock:false,
		height: 370,    
        width: 770,
		plugins:checkColumn,
		clicksToEdit: 1, 
        layout: 'fit',
		columnLines:true
    });
	
	Ext.override(Ext.Editor, {
		onBlur: function() {
			if (this.allowBlur === true && this.editing) {
				this.completeEdit();
			}
		}
	});
	var lec = new Ext.ux.form.LovCombo({
	allowBlank:false,
	id:'lecid',
	name:'lecid',
	hiddenName: 'lec_id',
	labelSeparator:'',
	fieldLabel : 'Lec:<span class="comp">*</span>',
	width:163,
	resizable: false,
	autoScroll:false,
	store: lecstore,
	mode: 'local',
	lastQuery:'',
	tabIndex:5,
	editable:false,
	displayField:'lec_name',
	displayFieldTpl:'{student_curdiv}',
	valueField: 'lec_id',
	forceSelection:true,
	navigateItemsWithTab: false
});
	var PresentForm = new Ext.FormPanel({
		method:'post',
        frame:true,
		url:'save-daily-present.php',
		standardSubmit:false,       
        layout:'column',        
		items :[{
			columnWidth: 0.33,
			layout: 'form',
			labelWidth:75,
			items:[branch,{
				xtype: 'combo',
				name: 'batch_id',
				id:'batch_id',
				hiddenName: 'batch_idhidden',
				allowBlank:true,
				fieldLabel: 'Batch :',
				forceSelection:true,
				tabIndex:4,
				labelSeparator:'',	
				displayField:'batch_name',
				valueField: 'batch_id',
				width:163,
				triggerAction:'all',
				mode:'local',
				store: BatchStore 
				},lectype,{	
				xtype: 'combo',
				name: 'day_comment',
				hiddenName: 'day_comment',
				id:'daily_note',
				fieldLabel: 'Note',
				emptyText:'Comment..',
				displayField:'day_comment',
				valueField: 'day_comment',
				width:163,
				tabIndex:9,
				triggerAction:'all',
				mode:'local',
				store: notestore 
			},
				{
				xtype:'textfield',
				id:'day_id',
				name:'day_id',
				hidden:true
			}]
		},{
			columnWidth: 0.34,
			layout: 'form',
			labelWidth:85,
			items:[semester,lec,{
				xtype: 'combo',
				name: 'workingday',
				hiddenName: 'working_id',
				allowBlank:false,
				id:'workingday',
				emptyText:'Please select day...',
				fieldLabel: 'W-H day',
				forceSelection:true,
				tabIndex:8,
				displayField:'working_value',
				valueField: 'working_id',
				width:163,
				triggerAction:'all',
				mode:'local',
				store: workingstore 
			},{		
					xtype: 'combo',
					name: 'shift',
					id:'shift',
					labelSeparator:'',	
					fieldLabel :'Shift :<span class="comp">*</span>' ,
					forceSelection:true,				
					displayField:'shift',
					valueField: 'shift'
					,lastQuery:''
					,allowBlank:false
					,triggerAction:'all'
					,mode:'local'
					,store:shiftstore
					,width:163
					,selectOnFocus: true,
					tabIndex:10
				}
			
			]},
			{
			columnWidth: 0.33,
			layout: 'form',
			labelWidth:85,
			items:[{		
					xtype: 'combo',
					name: 'division_id',
					id:'division_id',
					labelSeparator:'',	
					fieldLabel :'Division :<span class="comp">*</span>' ,
					forceSelection:true,				
					displayField:'division_name',
					valueField: 'division_name'
					,lastQuery:''
					,allowBlank:false
					,triggerAction:'all'
					,mode:'local'
					,store:divisionstore
					,width:163
					,selectOnFocus: true,
					tabIndex:3
				},subject,{
				xtype: 'datefield',
				fieldLabel: 'Date :<span class="comp">*</span>',
				width:163,
				labelSeparator:'',
				id: 'day_date',
				name: 'day_date',
				tabIndex:12,
				format:JSDateFormat,
				maxValue:new Date(),
				value:new Date(),
				vtype: 'daterange',
				allowBlank:false
			},{
				columnWidth: 0.5,
				layout: 'column',
				labelWidth:85,
				items:[{
					columnWidth: 0.69,
					layout: 'form',
					style: 'padding:0px 0px 0px 90px',
					labelWidth:85,
					items:[{  }]},{
					columnWidth: 0.69,
					layout: 'form',
					style: 'padding:0px 0px 0px 90px',
					labelWidth:85,
					items:[{  
						xtype:'button',
						text: 'View',	
						iconCls:'icon-view',
						width:70,
						tabIndex:12,
						handler: function(){	
							
							var varentrydate=Ext.getCmp('day_date').getValue();
							var varbranch=Ext.getCmp('branch_name').getValue();
							var varsemester=Ext.getCmp('semester_name').getValue();
							var varsubject=Ext.getCmp('subject_name').getValue();
							var varbatch=Ext.getCmp('batch_id').getValue();
							var varlec=Ext.getCmp('lecid').getValue();
							var varlectype=Ext.getCmp('lectype').getValue();
							var vardiv=Ext.getCmp('division_id').getValue();
							
							//var shift1=Ext.getCmp('shift').getValue();
							
							var resbool="";	
							if(varentrydate!="" &&  varbranch!="" && varsemester !="" && varsubject !="" && varlec !=""){ 
								Ext.Ajax.request({
									loadMask: true,
									sync : true,
									url: 'data/presententryrepeat.php',
									params:{
										branch:varbranch,
										semester:varsemester,
										subject:varsubject,
										day_date:varentrydate,
										batch_id:varbatch,
										lec_id:varlec,
										lectype:varlectype,
										div_id:vardiv
									},
									success:function(resp){
										resbool=Ext.decode(resp.responseText).success;	
										var day_id=Ext.decode(resp.responseText).day_id;	
										Ext.getCmp('day_id').setValue(day_id);		
										if(day_id==-1){
											Ext.MessageBox.show({
									title: 'Error',
									msg: 'Mismatch Sequence of Lec!',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});
										}
										else if(day_id==-2){
											Ext.MessageBox.show({
									title: 'Error',
									msg: Ext.getCmp('lecid').getRawValue()+'`s Attendance is already taken!',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});
										}
											else if(day_id==-3){
											Ext.MessageBox.show({
									title: 'Error',
									msg: 'Inform to PowerUser for Edit it!',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});
										}	  			                     						  	 															                                        else if(day_id!=0){
											Ext.MessageBox.confirm('Confirmation','Present Entry is already existed for this Branch,Semester and date.Do u want to modify this record?',editstudent);
										} else{
											grid.stopEditing();
											store.load({params: {'branch_id': Ext.getCmp('branch_name').getValue(),'semester_id': Ext.getCmp('semester_name').getValue(),'subject_id': Ext.getCmp('subject_name').getValue(),'batch_id': Ext.getCmp('batch_id').getValue(),'div_id': Ext.getCmp('division_id').getValue(),'shift1': Ext.getCmp('shift').getValue()}});
										}
									}				
								});	
							} else{
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please select branch, semester, division, subject, lecture and date',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});
							}
						}
					}]
				},{
					columnWidth: 0.29,
					layout: 'form',
					style: 'padding:0px 0px 0px 0px',
					labelWidth:70,
					items:[{
						xtype:'button',
						iconCls:'icon-reset',
						text: 'Reset', 
						width:70,
						tabIndex:13,
						name:'btnReset',
						id:'btnReset',
						handler: function (){
							store.removeAll();
							PresentForm.getForm().reset();
							Ext.getCmp('branch_name').enable();	
							Ext.getCmp('semester_name').enable();	
							Ext.getCmp('subject_name').enable();	
							Ext.getCmp('day_date').enable();
							Ext.getCmp('shift').enable();	
							Ext.getCmp('workingday').enable();	
							Ext.getCmp('daily_note').enable();
							Ext.getCmp('division_id').enable();
							Ext.getCmp('batch_id').enable();
							Ext.getCmp('lecid').setValue('1');
							Ext.getCmp('batch_id').setValue('0');
							Ext.getCmp('branch_name').focus(true,500);
						}
					}]
				}]
			}]
		
		},{
			columnWidth: 1,
			layout: 'form',
			items:[{height:5},grid]}
      	]		   
  	});
	
	Ext.getCmp('workingday').setValue(1);
   	
	function editstudent(btn){
		if(btn=='yes'){			  			
			var day_id=Ext.getCmp('day_id').getValue();
			store.load({params:{
				day_id:day_id,
				'branch_id': Ext.getCmp('branch_name').getValue(),
				'semester_id': Ext.getCmp('semester_name').getValue(),
				'subject_id': Ext.getCmp('subject_name').getValue(),
				'batch_id': Ext.getCmp('batch_id').getValue(),
				'lec_id': Ext.getCmp('lecid').getValue(),
				'div_id': Ext.getCmp('division_id').getValue(),
				'shift1': Ext.getCmp('shift').getValue()
			}});
			Ext.getCmp('branch_name').disable();	
			Ext.getCmp('semester_name').disable();
			Ext.getCmp('subject_name').disable();
			Ext.getCmp('division_id').disable();
			Ext.getCmp('batch_id').disable();
			Ext.getCmp('day_date').disable();	
			Ext.getCmp('workingday').disable();	
			Ext.getCmp('daily_note').disable();	
			Ext.getCmp('shift').disable();	
			Ext.getCmp('lecid').disable();	
			Ext.getCmp('lectype').disable();	
	  	} else{
			Ext.getCmp('branch_name').reset();
			Ext.getCmp('semester_name').reset();
			Ext.getCmp('subject_name').reset();
			Ext.getCmp('division_id').reset();
			Ext.getCmp('batch_id').reset();
			Ext.getCmp('day_date').reset();
			Ext.getCmp('workingday').reset();
			Ext.getCmp('daily_note').reset();
			Ext.getCmp('lec_id').reset();
			Ext.getCmp('shift').reset();
			Ext.getCmp('lectype').reset();
			store.removeAll();
	  	} 
  	}

	store.on({'load': {
		fn: function(store, options){ 
			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
				});
			} 
		}
	}});
 

	PresentForm.getForm().on({
		actioncomplete: function(form, action){
			if(action.type == 'submit'){	
				if(action.result.success=="true"){
					Ext.MessageBox.show({
						title: 'Message',
						msg: action.result.msg,
						buttons: Ext.MessageBox.OK,
						icon:Ext.MessageBox.INFO
					});
					store.removeAll();		 
								PresentForm.getForm().reset();
								Ext.getCmp('lecid').setValue('1');
								Ext.getCmp('batch_id').setValue(0);
								Ext.getCmp('batch_id').setRawValue('All');
								Ext.getCmp('branch_name').focus(true,1000);
				} else if(action.result.success=="false") {
					Ext.MessageBox.show({
						title: 'Error',
						msg: action.result.msg,
						buttons: Ext.MessageBox.OK,
						icon:Ext.MessageBox.ERROR
					});
					//store.removeAll();		 
					//PresentForm.getForm().reset();
					//Ext.getCmp('branch_name').focus(true,1000);		
				}
			}
		}
	}); 
		
	var PresentWindow = new Ext.Window({
        title: 'Daily Present Entry',
		width:950,		
		height:550,	
		modal:true,	
		border: false,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: PresentForm,
		buttonAlign: 'center',
	    buttons:[{
			text: 'Save',
			tabIndex:14,
			iconCls:'icon-save', 
			name:'btnSave',
			id:'btnSave',		
			handler: function(){
				var stcount = grid.store.getCount();
				if(stcount>=1){
					Ext.getCmp('branch_name').enable();	
					Ext.getCmp('semester_name').enable();	
					Ext.getCmp('subject_name').enable();	
					Ext.getCmp('day_date').enable();	
					Ext.getCmp('workingday').enable();	
					Ext.getCmp('daily_note').enable();
					Ext.getCmp('division_id').enable();
					Ext.getCmp('shift').enable();
					Ext.getCmp('batch_id').enable();
					var absantStudentids ="";
					var presentStudentid = "";
					var j=0;
					var k=0;
					for(var i = 0, len = grid.store.getCount(); i < len; i++){
						record = grid.store.getAt(i);
						if(record.data.day_present == false){
							absantStudentids +=  record.data.student_id + ",";
						} else{
							presentStudentid += record.data.student_id + ",";
						}
					}	
					
					if(Ext.getCmp('day_id').getValue() > 0){
						PresentForm.getForm().submit({	
							params:{absstudentids:absantStudentids,prestudentids:presentStudentid},				
							waitMsg: 'Saving...',
							success:function(){
								
							},
							failure: function (){
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please fill complete form',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});
							}
						});							
					}
					else{
						PresentForm.getForm().submit({	
							params:{absstudentids:absantStudentids,prestudentids:presentStudentid},				
							waitMsg: 'Saving...',
							success:function(){
								store.removeAll();		 
								PresentForm.getForm().reset();
								Ext.getCmp('branch_name').focus(true,1000);
							},
							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 click on view button to see the students record.',
						buttons: Ext.MessageBox.OK,
						icon:Ext.MessageBox.ERROR
					});
				}
			}	
		},{
			text: 'Close',
			iconCls:'icon-close', 
			name:'btncancle',
			tabIndex:15,
			id:'btncancle',
			handler: function (){
				PresentWindow.close();
			}
		}]
	});
	Ext.getCmp('lecid').on('expand',function (){
		Ext.getCmp('lecid').setValue('0');
		}); 
	var varWidth =  (screen.width - PresentWindow.width)/2;
	PresentWindow.setPosition(varWidth,((screen.height/4)-170));	 
	PresentWindow.show();
	Ext.getCmp('branch_name').focus(true,1000);
	Ext.getCmp('lecid').setValue('1');
	Ext.getCmp('batch_id').setValue(0);
	Ext.getCmp('batch_id').setRawValue('All');
	//Ext.getCmp('batch_id').getStore().getAt('2');
});
</script>
<div align="center">
<?php require "_footer.php";?>