var secFields = [{
	name: 'designation_id',
    type: 'int'
    },{
 	name: 'designation_name',
    type: 'string'
}]

var Store = new Ext.data.JsonStore({
    url:'data/designation-grid-filter.php',
    root: 'rowe',
	remoteSort: true,
    sortInfo: {
    	field: 'designation_id',
        direction: 'ASC'
   	},
    fields: secFields,
    idProperty: 'designation_id',
	autoLoad: true,
    writer:new Ext.data.JsonWriter()
});

Ext.onReady(function(){
	
	Ext.QuickTips.init();
    
	var colModel = new Ext.grid.ColumnModel([
		{ 
			header: "Designation ID", 
			dataIndex: 'designation_id',
			sortable: true, 
			hidden: true 
		},{ 
			id: 'designation_name',
			header: "Designation Name",
			width: 50,
			dataIndex: 'designation_name',
			align: 'left',
			sortable: true
        }
		
    ]);
	
	var flgalert = 0;
	
	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("company-form").getForm().loadRecord(rec);
					gridForm.getForm().findField('designation_name').enable();
					Ext.getCmp('btnSave').enable();
					Ext.getCmp('btnDel').enable();
					Ext.getCmp('btnAdd').disable();
				}
			}
		}),
		autoExpandColumn: 'designation_name',
		height: 250,
		border: true
	});
	
	var gridForm = new Ext.FormPanel({	
		id: 'company-form',
		frame: true,
		url:'save-designation.php',
		labelAlign: 'left',
		bodyStyle:'padding:5px',
    	layout: 'column',
    	items:[
			{
				columnWidth: 1,
				layout: 'fit',
				items:[grid]
			},{
				columnWidth: 1,
				xtype: 'fieldset',
				labelWidth: 110 ,
				defaults: {border:false},
				defaultType: 'textfield',
				height:160,
				bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;',
				border:false,
				style:{
					"margin-left": "10px", 
					"margin-right": Ext.isIE6 ? (Ext.isStrict ? "-10px" : "-13px") : "0" 
				},
            	items:[				
					{
						xtype: 'textfield',
						name: 'designation_name',
						id:'designation_name',
						fieldLabel: 'Designation Name',
						width:150,
						disabled:true,
						allowBlank:false,
						autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
					},
					{
						xtype: 'numberfield',
						name: 'designation_id',
						id:'designation_id',
						hidden:true
					},
					{
						xtype: 'textfield',
						name: 'mode',
						id:'mode',
						value:'edit',
						hidden:true
					}
				]
			}
        ]
    });

	var DesignationWindow = new Ext.Window({
		title: 'Designation Information',
		width:420,		
		height:380,		
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: gridForm,
		buttonAlign: 'center',
	    buttons:[
			{
				text: 'Add New',
				name:'btnAdd',
				iconCls: 'icon-add',
				id:'btnAdd',
				handler: function (){
					gridForm.getForm().findField('designation_id').setValue(0);
					gridForm.getForm().findField('designation_name').reset();
					gridForm.getForm().findField('designation_name').enable();
					gridForm.getForm().findField('designation_name').focus(true,500);
					Ext.getCmp('btnAdd').disable();
					Ext.getCmp('btnDel').disable();
					Ext.getCmp('btnSave').enable();
				}
			},{
				text: 'Save', 
				name:'btnSave',
				iconCls: 'icon-save',		
				id:'btnSave',
				disabled:true,
				handler: function (){
					gridForm.getForm().submit({
						waitMsg: 'Saving...',
						success: function(){
							Ext.getCmp('btnDel').disable();
							Ext.getCmp('btnSave').disable();
							gridForm.getForm().findField('designation_name').disable();
							Store.load();	
							gridForm.getForm().reset();
							grid.getSelectionModel().clearSelections();
							Ext.getCmp('btnAdd').enable();
						},
						failure: function(){
							Ext.MessageBox.show({
								title: 'Error',
							   	msg: 'Please fill up complete form.',
							   	buttons: Ext.MessageBox.OK,
							   	icon:Ext.MessageBox.ERROR
							});									
						}
					});							
				}						 
			},{
				text: 'Delete',
				name:'btnDel',
				iconCls:'remove',
				id:'btnDel',
				disabled: true,
				handler: function(){
					gridForm.getForm().findField('mode').setValue('delete');
					if(gridForm.getForm().findField('designation_name').getValue() !=''){
						
						Ext.Ajax.request({
							loadMask: true,
							sync : true,
							url: 'data/check-designation-related-record.php',
							params: {designation_id: Ext.getCmp('designation_id').getValue()},
							success: function(resp) {
								
								var str = "";
								resbool=resp.responseText;
								if(resbool != ""){
									str = "It has related record in " + resbool;	
								}
								
								Ext.MessageBox.confirm('Confirmation', str +' Are you sure want to delete.?', deletesubject);
									 			
							}				
						});
						
						Ext.getCmp('btnAdd').enable();
						
					} 
				}	
			},{
				text: 'Reset', 
			    iconCls: 'icon-reset',		
				name:'btnreset',
				id:'btnreset',
				handler: function(){
					gridForm.getForm().reset();
					gridForm.getForm().findField('designation_name').disable();
					Ext.getCmp('btnSave').disable();
					Ext.getCmp('btnDel').disable();
					Ext.getCmp('btnAdd').enable();
					grid.getSelectionModel().clearSelections();
				}
			},{
				text: 'Close', 
				name:'btnclose',
				iconCls: 'icon-close',		
				id:'btnclose',
				handler: function(){
					DesignationWindow.close();
				}
			}
		]
	});
	
	var varWidth =  (screen.width - DesignationWindow.width)/2;
    DesignationWindow.setPosition(varWidth,((screen.height/4)-100));
	DesignationWindow.show();
	
	// start duplication
	gridForm.getForm().findField('designation_name').on('blur',function (){
		var varDesigName=gridForm.getForm().findField('designation_name').getValue();
		var designation_id=gridForm.getForm().findField('designation_id').getValue();	
		var resbool="";		
				
		if(varDesigName!=""){			
			Ext.Ajax.request({
				loadMask: true,
				sync : true,
				url: 'data/check-designation-duplicate.php',
				params: {designation_name: varDesigName,designation_id:designation_id},
				success:function(resp){
					resbool=Ext.decode(resp.responseText).success;	
					if(resbool=='false'){
						Ext.MessageBox.show({
							title: 'Error',
							msg: 'Designation Name already exists!',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.ERROR
						});
						gridForm.getForm().findField('designation_name').reset();											
						Ext.getCmp('designation_name').focus('', 10);
					}
				}				
			});		
		}
	       
	});
	// end duplication
	
	function deletesubject(btn){
		if(btn=='yes'){			  			
			gridForm.getForm().submit({
				waitMsg: 'Loading...',
				success: function(){
					
					gridForm.getForm().reset();
					gridForm.getForm().findField('designation_name').disable();
					Ext.getCmp('btnSave').disable();
					Ext.getCmp('btnDel').disable();
					Store.load();
					
				},
				failure: function(){
					Ext.MessageBox.show({
						title: 'Error',
						msg: 'Please fill up complete form.',
						buttons: Ext.MessageBox.OK,
						icon:Ext.MessageBox.ERROR
					});									
				}
			});
		} 
	}
});