<?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>'],
	['Fees Head:<span class="comp">*</span>'],
	['Amount:<span class="comp">*</span>']];

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

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



var gtufeesheadstore = new Ext.data.ArrayStore({		
	fields: ['gtuhead_id','gtuhead_name','gtuhead_amount','max_amount','gtufee_type'],
	data:Ext.semester.gtufeeshead
});

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: 150,
	allowBlank:false,
	store:branchstore,
    listeners:{'select': function(cmb, rec, idx){
	semesterstore.filter('branch_id',cmb.getValue(),false,true,true);
		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',
	lastQuery:'',
	tabIndex:'2',
	width: 150,
	allowBlank:false,
	store:semesterstore,
    listeners:{'select': function(cmb, rec, idx){
		
		/*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:'',
	width: 100
	,allowBlank:true
	,triggerAction:'all'
	,mode:'local'
	,store:divisionstore
	,selectOnFocus: true,
	listeners: {'select': function(cmb, rec, idx) {
	

}
    }}


var gtuhead = new Ext.form.ComboBox({
	name: 'gtuhead',
	id:'gtuhead',
	hiddenName:'gtuhead_id',
	labelSeparator:'',	
	fieldLabel :labelArray[3] ,
	displayField:'gtuhead_name',
	valueField: 'gtuhead_id',
	editable:true,
	triggerAction:'all',
	forceSelection:true,
	mode:'local',
	width: 150,
	tabIndex:'3',
	allowBlank:false,
	store: gtufeesheadstore,
    listeners:{'select': function(cmb, rec, idx){
		 
		 var record = gtufeesheadstore.getAt(idx);
		 var headamt=record.data.gtuhead_amount;
		 Ext.getCmp('gtuhead_amount').setValue(headamt);
		  var maxamt=record.data.max_amount;
		 Ext.getCmp('max_amount').setValue(maxamt);
		 var gtufee_type=record.data.gtufee_type;
		 Ext.getCmp('gtufee_type').setValue(gtufee_type);
		 if(cmb.getRawValue()!='GTU Exam Retest Fee')
		 {
			
		 cm = grid.getColumnModel();
         cm.setHidden(6,true);
		 }
		 else
		 {
			  cm = grid.getColumnModel();
         	  cm.setHidden(6,false);
		 }
		 LoadData1();
	}}
});

var gtuhead_amount = new Ext.form.NumberField({
	
	name: 'gtuhead_amount',
	id: 'gtuhead_amount',
	labelSeparator:'',	
	fieldLabel :labelArray[4],
	tabIndex:4,
	allowBlank:false,
	width:150,
	readOnly:true
	
});
var max_amount = new Ext.form.NumberField({
	
	name: 'max_amount',
	id: 'max_amount',
	labelSeparator:'',	
	fieldLabel :'Max Limit :',
	allowBlank:false,
	width:100,
	hidden:true
	
});
var gtufee_type = new Ext.form.TextField({
	
	name: 'gtufee_type',
	id: 'gtufee_type',
	labelSeparator:'',	
	fieldLabel :'Type:',
	allowBlank:false,
	width:100,
	hidden:true
	
});


/* START EDITABLE GRID */

store = new Ext.data.JsonStore({
	url:'data/getstudent-gtufeesdata1.php',
	root: 'data',
	storeId: 'student_id',
	totalProperty: 'total',
	fields: [{
		name:'student_name',
		type:'string'
	},,{
		name:'branch_name',
		type:'string'
	},{
		name:'division',
		type:'string'
	},{
		name:'student_id',
		type:'int'
	},{
		name:'student_gturegno',
		type:'int'
	},{
		name:'noofsub',
		type:'int'
	},{
		name:'receive_amount',
		type:'int'
	},{
		name:'gtuhead_amount',
		type:'int'
	},,{
		name:'paid',
		type:'bool'
	}] 
});

var grid = new Ext.grid.EditorGridPanel({
	store: store,
	clicksToEdit:1,
	columns: [
		new Ext.grid.RowNumberer(),
		{
			dataIndex: 'student_gturegno',
			header: 'Enrollment No',
			sortable:true,
			id: 'student_gturegno',
			width:90	
		},
		{
			dataIndex: 'student_name',
			header: 'Student Name',
			sortable:true,
			id: 'student_name',
			width:200
		},
		{
			dataIndex: 'branch_name',
			header: 'branch',
			sortable:true,
			id: 'branch_name',
			width:80	
		},
		{
			dataIndex: 'division',
			header: 'Division',
			sortable:true,
			id: 'division',
			width:50
		},{
			dataIndex: 'student_id',
			header: 'Student ID',
			id: 'student_id',
			hidden: true
		},{ 
			xtype: 'checkcolumn',
			header: 'Status',
			id: 'paid',
			dataIndex: 'paid',
			width:50	
		},{
			dataIndex: 'noofsub',
			header: 'Subjects',
			id: 'noofsub',
			sortable:true,
			hidden: false,
			width:60, 
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank: false,
				maxLength:2,
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '2'}
			}
		},{
			dataIndex: 'receive_amount',
			header: 'Amt Received',
			id: 'receive_amount',
			sortable:true,
			width:80
		},{
			dataIndex: 'gtuhead_amount',
			header: 'Head Amount',
			id: 'gtuhead_amount',
			hidden: true
			
		}
	],
	height:450,
	layout: 'fit',
	frame:true,
	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 Checkbox for taking fees.";
			}
		}
	});
});


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.getStore().on('update', function(store, record, action){
		
	if(Ext.getCmp('gtuhead').getRawValue()=='GTU Exam Retest Fee')
	{
	if(record.data.paid==true)
	{
		//alert(record.data.gtuhead_amount);
		if(Ext.getCmp('gtuhead').getRawValue()=='GTU Exam Retest Fee')
		{
		var index=store.indexOf(record);
		grid.startEditing(index,6);
		if(record.data.noofsub==0)
			{//record.set('noofsub','');
			}
		var rcvamt=record.data.gtuhead_amount*record.data.noofsub;
		var maxamt= Ext.getCmp('max_amount').getValue();
		if(rcvamt > maxamt)
			record.set('receive_amount',maxamt);
		else
			record.set('receive_amount',rcvamt);	
		
		}
		else
		{
		var rcvamt=record.data.gtuhead_amount;
		var maxamt= Ext.getCmp('max_amount').getValue();
		if(rcvamt > maxamt)
			record.set('receive_amount',maxamt);
		else
			record.set('receive_amount',rcvamt);	
		}
	}
	else if(record.data.paid==false && record.data.noofsub != 0 && record.data.receive_amount == 0)
	{
		var rcvamt=record.data.gtuhead_amount*record.data.noofsub;
		record.set('paid',true);
		var maxamt= Ext.getCmp('max_amount').getValue();
		if(rcvamt > maxamt)
			record.set('receive_amount',maxamt);
		else
			record.set('receive_amount',rcvamt);	
		
	}
	else if(record.data.paid==false && record.data.noofsub != 0 && record.data.receive_amount != 0)
	{
		
		record.set('receive_amount',0);
		record.set('noofsub',0);
		record.set('paid',false);
	}
	}
	else
	{
		if(record.data.paid==true)
		{
			var rcvamt=record.data.gtuhead_amount;
			record.set('receive_amount',rcvamt);
		}
		else
		{
		record.set('receive_amount',0);
		}
	}
	/*var receive_amt = Ext.getCmp('receive_amt').getValue();
	var temp = record.data.fees_amount - record.data.fees_recviced_amount;
	var temp1 = record.data.fees_due_amount;
	if(record.data.selected == true){
		if(temp1 > 0){
			record.set('fees_due_amount',temp1);
		} 
		else{
			record.set('fees_due_amount',temp);	
		}
	}
	else{
		record.set('fees_due_amount',0);
	}*/
});

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 LoadData1(){
	var branch_name 	= Ext.getCmp('branch_name').getValue();
	var semester_name 	= Ext.getCmp('semester_name').getValue();
	var gtuhead 		= Ext.getCmp('gtuhead').getValue();
	var division 		= Ext.getCmp('division_id').getValue();

	if(branch_name != '' && semester_name != '' && gtuhead != ''){	
	if(Ext.getCmp('gtufee_type').getValue()=='year')
	{
	Ext.Ajax.request({
				loadMask: true,
				sync : true,
				url: 'data/checkfeesreceived.php',
				params: {branch_id:branch_name,semester_id:semester_name,division:division,gtuhead_id:gtuhead},
				success: function(resp){
					var feesflag=Ext.decode(resp.responseText).feesflag;
					if(feesflag == '1')
					{
						
						Ext.MessageBox.show({
							title: 'Message',
							msg: 'Fees for this head had been taken!For the remaining student please take it by loging in to previous part of the year!',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.INFO
						});
					}
					else
					{
						store.load({params:{
							branch_name:branch_name,
							semester_name:semester_name,
							division:division,
							gtuhead_id:gtuhead
						}});
						
					}
							//Ext.getCmp('btnreset').fireEvent('click',Ext.getCmp('btnreset'));
							
					
				}
	});
	
	}
	else
	{
		store.load({params:{
							branch_name:branch_name,
							semester_name:semester_name,
							division:division,
							gtuhead_id:gtuhead
						}});
	}
			
	}
	else{
		Ext.getCmp('gtuhead').setValue('');
		alert('Select branch,semester,division and GTU FeesHead First!');
		store.removeAll();	
	}}

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


	
	var ExamForm = new Ext.FormPanel({
		id:'ExamForm',
    	labelWidth:70,
		autoHeight:true,
        url:'save-gtufeesdata.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:.37,
				items:[branch,gtuhead]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.36,
				labelWidth:62,
				items:[semester,gtuhead_amount]
			},
			{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.27,
				labelWidth:55,
				items:[divisionCombo,gtufee_type]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				style:{padding:'5px 0px 0px 0px'},
				columnWidth:1,
				items:[grid]
			}]
		}]
 	});
	
	var ExamWindow = new Ext.Window({
        title:'GTU Fees Collection Entry',
        //height:550,
		autoHeight:true,
        width:730,
		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_receive_amount = " ";
					var arr_noofsub = " ";
					var student_name = " ";
					var arr_paid = " ";
					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;
						receive_amount = record.data.receive_amount;
						noofsub = record.data.noofsub;
						paid = record.data.paid;
						
						if(paid==true && receive_amount==="" ){
								
							flg=1;
							student_name = record.data.student_name;
							at=	i;
							break;
						}
						else
						{
							arr_studentid+=student_id +",";	
							arr_receive_amount+= receive_amount+",";
							arr_noofsub+= noofsub+",";	
							arr_paid+=paid+",";	
						}
						
					}
					if(flg==0)
					{
							ExamForm.getForm().submit({
						params:{student_id:arr_studentid,receive_amount:arr_receive_amount,paid:arr_paid,noofsub:arr_noofsub},	
						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)-110));
	ExamWindow.show();
	Ext.getCmp('branch_name').focus(true,1000);
});
</script>
<div id="abcd"></div>
</div>
<?php require "_footer.php";?>