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

?>
<div id="main-content" >
<script type="text/javascript" language="javascript"> 
Ext.onReady(function(){
Ext.QuickTips.init(); 	

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

	
var divisionstore = new Ext.data.ArrayStore({
    	fields:['division_id','division_name'],
		data: Ext.student.division
	});	
	
	
  function formatDate(value){
		
		var temp = value.dateFormat('d/m/Y');
		if(temp=="28/02/1900")
		{
			return '';
		}
		else
		{
        	return temp;
		}
    }
	
	function tfwdata(value)
	{
		if(value==1)
		{
			return '<b>Yes</b>';
		}
		else
		{
        	return 'No';
		}
		
	}
	
	function leavedata(value)
	{
		if(value=='r')
		{
			return '<b><span class="comp">Reshuffling</span></b>';
		}
		else if(value=='s')
		{
			return '<b><span class="comp">Suspend</span></b>';
		}else if(value=='d')
		{
			return '<b><span class="comp">Detain</span></b>';
		}else if(value=='c')
		{
			return '<b><span class="comp">Cancel</span></b>';
		}else
		{
			return '<b>Active</b>';
		}
		
	}
	
	
	  	 		
store = new Ext.data.JsonStore({
    	autoDestroy: true,
		url:'data/student-scholarship-data.php',
        remoteSort: true,
        storeId: 'myStore',
        idProperty: 'student_id',
        root: 'data',
        totalProperty: 'total',
        fields: [{
            name: 'student_id',
			type:'int'
        }, {
            name: 'student_scholarship'
        },{
            name: 'semester_name'
        },{
			name:'branch_name'
		}, {
            name: 'studentname'
        },{
            name: 'cur_divison'
        }] 
    });
	
var createColModel = function (finish, start) {
        var columns = [
		new Ext.grid.RowNumberer(),
		{
            dataIndex: 'student_id',
            header: 'Student ID', 
			width:80,
			hidden:true,
	        filterable: true
        },{
            dataIndex: 'branch_name',
            header: 'Branche',
			width:100
        },{
            dataIndex: 'semester_name',
            header: 'Semester',
			width:80
        },{
            dataIndex: 'cur_divison',
            header: 'Div',
			width:60
        },{
            dataIndex: 'studentname',
            header: 'Student Name',
            id: 'SurName', 
			width:220
        },{
            dataIndex: 'student_scholarship',
            header: 'Scholarship',
			width:170,
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowNegative: false,
			//	allowBlank: false
			}
        }];
		
		return new Ext.grid.ColumnModel({
            columns: columns.slice(start || 0, finish),
            defaults: {
                sortable: true
            }
        });
    };
var grid = new Ext.grid.EditorGridPanel({
		id:'grid',
		name:'grid',
        store: store,
		clicksToEdit:1,
        colModel: createColModel(40), 
        loadMask: true,
		frame: false,
		enableColLock:false,
		columnLines:true,
		stripeRows:true,
	    height: 450,    
        width: 690,
        layout: 'fit',
    	selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
		
    });

   var studententry = new Ext.FormPanel({
		autoHeight:true,   
		url:'save-updated-scholarship.php',
		frame:true,
		standardSubmit:false,     
      	layout:'column',
		labelWidth:55,
		items:[{columnWidth: 0.32,
                            layout:'form',	
							items:[{		
										xtype: 'combo',
										name: 'branch_id',
										hiddenName: 'branch_hidden',
										id:'branch_id',
										labelSeparator:'',	
										fieldLabel :'Branch:<span class="comp">*</span>' ,
										forceSelection:true,				
										displayField:'branch_name',
										valueField: 'branch_id'
										,triggerAction:'all'
										,mode:'local'
										,store: branchstore
										,width:150
										,selectOnFocus: true,
										listeners: {'select': function(cmb, rec, idx) {
													 Ext.getCmp('division_id').reset();
													 semesterstore.filter('branch_id',cmb.getValue(),false,true,true);

											}
										}
								   }]
			    },
				{columnWidth: 0.32,
                            layout:'form',	
							items:[ {	
										xtype: 'combo',
										name: 'semester_id',
										hiddenName: 'semester_hidden',
										id:'semester_id',
										labelSeparator:'',	
										fieldLabel :'Sem:<span class="comp">*</span>' ,
										forceSelection:true,
										lastQuery:'',										
										displayField:'semester_name',
										valueField: 'semester_id'
										,triggerAction:'all'
										,mode:'local'
										,store: semesterstore
										,width:150
										,selectOnFocus: true,
										listeners: {'select': function(cmb, rec, idx) {
										Ext.getCmp('division_id').reset();
											var branch=Ext.getCmp('branch_id').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);
											}
										}}
								   }]},
				{columnWidth: 0.20,
				layout:'form',	
				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:true
				,triggerAction:'all'
				,mode:'local'
				,store:divisionstore
				,width:60
				,selectOnFocus: true
  			  }]},
				{columnWidth: 0.16,
				layout:'form',	
				items:[{
											xtype:'button',
											text:'Search',
											iconCls:'icon-search',
											handler: function() {
												var barnch=Ext.getCmp('branch_id').getValue();
												var semester=Ext.getCmp('semester_id').getValue();
												var division_id=Ext.getCmp('division_id').getValue();
												if(barnch!="" &&  semester!="" &&  division_id!="")
													{
														store.reload({
															params: {
																barnch:barnch,
																semester:semester,
																division_id:division_id
																}
														});	
													}
													else
													{
															alert("Select All Criteriya");													
													}
											}
										  }]}
				]
	
	 });	 
	 
	var StudentWindow = new Ext.Window({
        title: 'Student Scholarship',
		width:700,		
		modal:true,
		autoHeight:true,
		shadow: false,
		border: false,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items:[studententry,grid],
		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_scholarship_no = " ";
					var student_name = " ";
					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;
						scholarship_no = record.data.student_scholarship;
						
						if(scholarship_no==="" ){
								
							flg=1;
							student_name = record.data.studentname;
							at=	i;
						//	break;
						}
						else
						{
							arr_studentid+=student_id +",";	
							arr_scholarship_no+= scholarship_no+",";	
						}
						
					}
					
						studententry.getForm().submit({
						params:{student_id:arr_studentid,scholarship_no:arr_scholarship_no},	
						waitMsg: 'Saving...',		
						success: function () {		
							Ext.MessageBox.show({
								title: 'Message',
								msg: 'Data has been saved..',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.INFO
							});
							studententry.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',
			iconCls:'icon-reset',
			tabIndex:7,
			id:'btnreset',
			handler: function (){
				studententry.getForm().reset();
				grid.getSelectionModel().clearSelections();
				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 (){
				StudentWindow.close();
			}
		}]

	});
	
	var varWidth =  (screen.width - StudentWindow.width)/2;
	StudentWindow.setPosition(varWidth,((screen.height/4)-160));
	StudentWindow.show();
	Ext.getCmp('branch_id').focus(true,500);

});
</script>
</div>
<?php require "_footer.php";?>