<?php require "_header.php";?>
<?php require "includeGridFilters.php";?>
<!-- start custom CSS/JS -->
<script type="text/javascript">
Ext.onReady(function(){
    
	Ext.QuickTips.init();

	
	var xg = Ext.grid;
	
	var sm2 = new xg.CheckboxSelectionModel({
 		listeners: {
			selectionchange: function(sm) {
            	if (sm.getCount()) {
                    grid.removeButton.enable();
                } else {
                    grid.removeButton.disable();
                }
            }
        }
    });  

	var encode = false;
    var local = false;
   	
	function confirmDeleteEmployees(){
		var selections = grid.selModel.getSelections();
      	var prez = [];
        for(i = 0; i< grid.selModel.getCount(); i++){
        	prez.push(selections[i].json.operator_id);
		}
        var encoded_array = Ext.encode(prez);
		
		Ext.Ajax.request({
			loadMask: true,
			sync : true,
			url: 'data/getalltableope.php',
			params: {operator_id: encoded_array},
			success: function(resp) {
				resbool=resp.responseText;
				if(resbool){
					Ext.MessageBox.confirm('Confirmation','It has related record in ' + resbool +' Are you sure want to delete.?', deleteEmployees);
				}	 			
			}				
		});	
	} 
	
	function deleteEmployees(btn){
    	if(btn=='yes'){
        	var selections = grid.selModel.getSelections();
         	var prez = [];
         	for(i = 0; i< grid.selModel.getCount(); i++){
         	 	prez.push(selections[i].json.operator_id);
         	}
         	var encoded_array = Ext.encode(prez);
         	Ext.Ajax.request({  
            	waitMsg: 'Please Wait',
            	url: 'data/operator-grid-delete.php', 
            	params: { 
               		task: "DELETEEMP", 
               		ids: encoded_array
			   	}, 
            	success: function(response){
					var result=eval(response.responseText);
					switch(result){
						case 1: 
						store.reload();
						break;
					default:
						Ext.MessageBox.alert('Warning','Could not delete the entire selection.');
						break;
					}
				},
				failure: function(response){
					var result=response.responseText;
					Ext.MessageBox.alert('error','could not connect to the database. retry later');      
				}
   			});
      	}  
  	}
	
	store = new Ext.data.JsonStore({
		autoDestroy: true,
        url:'data/operator-grid-filter.php',
        remoteSort: true,
       	sortInfo:{ field: 'operator_id',direction: 'ASC' },
        storeId: 'myStore',
        idProperty: 'operator_id',
        root: 'data',
        totalProperty: 'total',
        fields:[
		{
        	name:'operator_id', 
			type:'int'
        },{
            name: 'operator_name'
        },{
            name: 'operator_address'
        },{
            name: 'operator_phone'
        },{
            name: 'operator_mobile'
        },{
            name: 'operator_panno'
        },{
            name: 'operator_aadharno',
			
		},{
            name: 'operator_otherid', 
			
		},{
            name: 'operator_joindate', 
			type: 'date', 
			dateFormat: 'Y-m-d'
		}] 
    });
    
	var filters = new Ext.ux.grid.GridFilters({
		encode: encode, 
        local:false,                                      
		filters: [{
			type: 'numeric',
            dataIndex: 'operator_id'
        },{
            type:'string',
            dataIndex: 'operator_name'
        },{
            type:'string',
            dataIndex: 'operator_phone'
        },{
            type:'string',
            dataIndex: 'operator_mobile'
        },{
            type:'string',
            dataIndex: 'operator_panno'
        },{
            type:'numeric',
            dataIndex: 'operator_aadharno'
        },{
            type:'date',
            dataIndex: 'operator_otherid'
        },{
            type:'date',
            dataIndex: 'operator_joindate'
		}]
    });    

	var createColModel = function (finish, start) {
    	var columns = [
		sm2,
		{
			header: "Edit",	
			width:35,
		    renderer: function(value, metaData, record, rowIndex, colIndex, store) {
		   		return '<a href="addedit-operator.php?ID='+ record.get('operator_id') +'">Edit</a>';
			}
		},
		{
 			dataIndex:'operator_id',
            header:'Operator ID',
			align:'center',
			hidden:true
		},{
            dataIndex: 'operator_name',
            header: 'Name',
			width:130
		},{
            dataIndex: 'operator_address',
            header: 'Address',
			width:175
		},{
            dataIndex: 'operator_phone',
            header: 'Phone No',
			width:75
		},{
            dataIndex: 'operator_mobile',
            header: 'Mobile No',
			width:75
		},{
            dataIndex: 'operator_panno',
            header: 'Pan No',
			width:75
		},{
            dataIndex: 'operator_aadharno',
            header: 'Aadhar No',
			width:75
		},{
            dataIndex: 'operator_otherid',
			align:'center',
            header: 'Other Id',
			
			width:65
		},{
            dataIndex: 'operator_joindate',
			align:'center',
            header: 'Joining Date',
			renderer: Ext.util.Format.dateRenderer('d/m/Y'),
			width:75
        }];
		return new Ext.grid.ColumnModel({
        	columns: columns.slice(start || 0, finish),
			defaults: {
				sortable: true
			}
        });
    };
	
	var grid = new Ext.grid.GridPanel({
    	store: store,
        colModel: createColModel(40), 
        loadMask: true,
		frame: true,
		enableColLock:false,
		columnLines:true,
	    height: 380,   
        width: 775,
        //layout: 'fit',
    	selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
        plugins: [filters],
		sm: sm2,
        listeners: {
            render: {
                fn: function(){
                    store.reload({
                        params: {
                            start: 0,
                            limit: 10    
                        }
                    });
                }
            }
        },
		tbar: [
 			{
		    	iconCls: 'icon-add',
           		text: 'Add Operator',
				handler: function() {
               		location.href="addedit-operator.php"; 
				}
      		},'-',
			{
				text: 'Delete Employee',
            	handler: confirmDeleteEmployees,  
            	iconCls:'remove',
				ref: '../removeButton',
				disabled: true
			}
	  	], 
        bbar: new Ext.PagingToolbar({
            store: store,
            pageSize: 10,
			displayInfo: true,
            plugins: [filters]
        })
    });
    grid.getBottomToolbar().add([
        '->',
        {
            text: 'Clear Filter Data',
            handler: function () {
                grid.filters.clearFilters();
            } 
        }    
    ]);

	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 Edit Link to Edit Record.";
				}
			}
		});
  	});
	
	store.on({
		'beforeload': {
    		fn: function(store, options){
            	options.params || (options.params = {}); 
            	Ext.apply(options.params, {
            		pageNo: this.pageNumber
            	});
        	},
        	scope: this
    	}
	});
	
	var ViewEmployeeWindow = new Ext.Window({
        title: 'View Operator Information',
        height: 360,
        width: 915,
		modal:true,
        layout: 'fit',
		closeAction:'close',
		border: false,
		collapsible:'true',
		items: grid
    }).show();
	
	var varWidth =  (screen.width - ViewEmployeeWindow.width)/2;
	ViewEmployeeWindow.setPosition(varWidth,((screen.height/4)-100));
});

</script>
<!-- end custom CSS/JS -->
<div id="main-content" align="center"></div>
<?php require "_footer.php";?>