                                                                                                                                                                                                                               
var headFields = [{
        name:'school_id',
        type: 'int'
    },{
        name: 'employee_id',
        type: 'int'
    }, {
        name: 'employee_name',
        type: 'string'
    },{
        name: 'employee_shortname',
        type: 'string'
    },{
        name: 'occupied_periods',
        type: 'int'
    },{
        name: 'employee_workingperiods',
        type: 'int'
    }]

var Store = new Ext.data.JsonStore({
    url:'data/get-tt-teacherdetails.php',
    root: 'result',
	autoLoad:'true',
	remoteSort: true,
    sortInfo: {
    	field: 'employee_id',
        direction: 'ASC'
   	},
    fields: headFields,
    idProperty: 'employee_id',
    writer:new Ext.data.JsonWriter()
});


var tech = "isteaching";

Ext.onReady(function(){
	
	Ext.QuickTips.init();
   
	var colModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header:'Sr.'}),
		{
		   	header: "School ID", 
		   	dataIndex: 'school_id', 
		   	sortable: true, 
		   	hidden: true ,
			menuDisabled:true
		},
		{ 
			header: "Employee ID", 
			dataIndex: 'employee_id',
			sortable: true, 
			hidden: true ,
			menuDisabled:true
		},		
		{ 
			id: 'employee_name',
			header: "Full Name",
			width: 170,
			dataIndex: 'employee_name',
			align: 'left',
			sortable: true,
			menuDisabled:true
        },
		{ 
			id: 'employee_shortname',
			header: "Short Name",
			width: 80,
			dataIndex: 'employee_shortname',
			align: 'left',
			sortable: true,
			menuDisabled:true
        },{ 
			id: 'employee_workingperiods',
			header: "Working Periods",
			width: 80,
			dataIndex: 'employee_workingperiods',
			align: 'left',
			sortable: true,
			menuDisabled:true
        }
    ]);

	var occupied_periods ;
	var grid = new Ext.grid.GridPanel({
					
					ds: Store,
					cm: colModel,
					sm: new Ext.grid.RowSelectionModel({
						singleSelect:false,
						listeners: {
							rowselect: function(sm, row, rec) {
								Ext.getCmp("gridForm").getForm().loadRecord(rec);
								Ext.getCmp('employee_shortname').enable();
								Ext.getCmp('employee_workingperiods').enable();
								Ext.getCmp('btnSave').enable();
								occupied_periods = rec.data.occupied_periods;
							school_id = rec.data.school_id;
							}
						}
					}),
					height: 250,
					border: 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 = "Please Click Here To Edit Working Periods.";
     }
    }
   });
  });
	
	var gridForm = new Ext.FormPanel({	
	id: 'gridForm',
    frame: true,
	url:'save-tt-teacher.php',
    labelAlign: 'left',
    bodyStyle:'padding:5px',
    layout: 'column',
    items: [{
				columnWidth: 1,
				layout: 'fit',
				items:[grid]
			},
			{
	    	columnWidth: 1,
    	    xtype: 'fieldset',
        	labelWidth: 100 ,
	        defaults: {border:false},
            defaultType: 'textfield',
			//height:130,
			border:false,
			bodyStyle: Ext.isIE ? 'padding:10px 15px 5px 15px;' : 'padding:10px 15px;',
           
            items: [				
				{
					xtype: 'textfield',
					name: 'employee_name',
					id:'employee_name',
					fieldLabel: 'Employee Name',
					width:150,
					allowBlank:false,
					allowDecimal:false,
					disabled:true,
					readOnly:true,
					autoCreate: {tag: 'input', type: 'text',  maxlength: '80'}
            	},
				{
					xtype: 'textfield',
					name: 'employee_shortname',
					id:'employee_shortname',
					labelSeparator:'',	
					fieldLabel: "Short Name:<span class='comp'>*</span>",
					width:150,
					allowBlank:false,
					allowDecimal:false,
					disabled:true,
					autoCreate: {tag: 'input', type: 'text',  maxlength: '5'}
            	},
				{
					xtype: 'textfield',
					name: 'employee_workingperiods',
					id:'employee_workingperiods',
					labelSeparator:'',	
					fieldLabel: "Working Periods:<span class='comp'>*</span>",
					width:150,
					allowBlank:false,
					disabled:true,
					autoCreate: {tag: 'input', type: 'text',  maxlength: '3'}
            	},
				{
						xtype: 'numberfield',
						name: 'school_id',
						id:'school_id',
						hidden:true
				
				},
				{
						xtype: 'numberfield',
						name: 'employee_id',
						id:'employee_id',
						hidden:true
				
				},
			 	{
					xtype: 'textfield',
					name: 'mode',
					id:'mode',
					value:'edit',
					hidden:true
			
            	}
			]
			}
        ]
    });
	var formid = Ext.getCmp('gridForm');
								
	gridForm.getForm().findField('employee_shortname').on('blur',function (){		
	var varemployee_shortname =gridForm.getForm().findField('employee_shortname').getValue();			
	var varemployee_id = gridForm.getForm().findField('employee_id').getValue();						
	var schoolid=gridForm.getForm().findField('school_id').getValue();	
	var resbool="";		
				
		if(varemployee_shortname!="")
		{			
					Ext.Ajax.request({
							  loadMask: true,
							  sync : true,
							  url: 'data/checkttempshortsubjectname.php',
							  params: {employeename: varemployee_shortname, employee_id: varemployee_id,school_id: schoolid},
							  success: function(resp) {
								// resp is the XmlHttpRequest object
								resbool=Ext.decode(resp.responseText).success;			                     
										if(resbool=='false')
										{ 
											Ext.MessageBox.show({
												title: 'Error',
												msg: 'Employee Short Name already exist.',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.ERROR
											});
											
											
											//alert("Employee Short Name already exists.");
												//Ext.getCmp('btnSave').disable();	
												//Ext.getCmp('employee_shortname').();
												//Ext.getCmp('employee_workingperiods').disable();
												
											
											gridForm.getForm().findField('employee_shortname').reset();
											gridForm.getForm().findField('employee_workingperiods').reset();
											//Ext.getCmp("gridForm").getForm().loadRecord(rec);
										}	 			
							  }				
							});		
		}
	}); //subject_name blur event
	////////
 	var win = new Ext.Window({	    
        title: 'View Employee Information',
        height: 450,
        width: 400,
        layout: 'fit',
		modal:true,
		border:false,
		closeAction:'hide',
		collapsible:'true',
		items: gridForm,
		buttons:[	  {
						text: 'Save', 
						name:'btnSave',
						id:'btnSave',
						iconCls:'icon-save',
						disabled:true,
						handler: function (){
							
								formid.getForm().submit({
									waitMsg: 'Saving...',
									success: function () {
		
  									Ext.getCmp('employee_shortname').disable();
									Ext.getCmp('employee_workingperiods').disable();
									Ext.getCmp('btnSave').disable();									
									Store.load();
									formid.getForm().reset();							
									grid.getSelectionModel().clearSelections();
									},
									failure: function () {
										
											 Ext.MessageBox.show({
											   title: 'Error',
											   msg: 'Please fill up complete form.',
											   buttons: Ext.MessageBox.OK,
											   icon:Ext.MessageBox.ERROR
											  });						
											 
											 //alert("Please fill up Complete Form.");
									}
								});							
							}						 
						},
						{
						text: 'Reset', 
						name:'btnreset',
						id:'btnreset',
						iconCls:'icon-reset',
						handler: function (){
										formid.getForm().reset();										
										Ext.getCmp('employee_shortname').disable();
										Ext.getCmp('employee_workingperiods').disable();
										Ext.getCmp('btnSave').disable();
										grid.getSelectionModel().clearSelections();
									}
						},{
		        	text: 'Close',
					    iconCls:'icon-close',
					    tabIndex:61,
					    handler: function () {
						    win.close();	
					    }
		        }]
    })
	win.show();
	gridForm.getForm().findField('employee_workingperiods').on('blur',function (){
	    if( occupied_periods > gridForm.getForm().findField('employee_workingperiods').getValue() && 
				 gridForm.getForm().findField('employee_workingperiods').getValue() != '' ){
  	         alert("Working Periods can't be less then Occupied Periods"); 
		     gridForm.getForm().findField('employee_workingperiods').reset();
		}	          
		});
});