<?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 confirmdeleteSchool(){
		if(grid.selModel.getCount() == 1)
		{
		  Ext.MessageBox.confirm('Confirmation','You are about to delete a Product. Continue?', deleteSchool);
		} else if(grid.selModel.getCount() > 1){
		  Ext.MessageBox.confirm('Confirmation','Delete these Product(s)?', deleteSchool);
		} 
  } 
function deleteSchool(btn){
    if(btn=='yes'){
         var selections = grid.selModel.getSelections();
         var support_id_array=[];
         for(i = 0; i< grid.selModel.getCount(); i++){
          support_id_array.push(selections[i].json.support_id);
         }
         var encoded_support_id_array = Ext.encode(support_id_array);
         var customer_id_array=[];
         for(i = 0; i< grid.selModel.getCount(); i++){
          customer_id_array.push(selections[i].json.customer_id);
         }
         var encoded_customer_id_array = Ext.encode(customer_id_array);

         Ext.Ajax.request({  
            waitMsg: 'Please Wait',
            url:'data/customer-grid-delete.php', 
            params: { 
               task: "DELETEEMP", 
               support_ids:  encoded_support_id_array,
               customer_ids: encoded_customer_id_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/support-grid-filter.php',
        remoteSort: true,
        sortInfo: {
            field: 'support_id',
            direction: 'ASC'
        },        
        idProperty: 'support_id',
        root: 'data',
        totalProperty: 'total',
        fields: [{
            name: 'support_id',
			type: 'int'
        }, {
            name: 'estore_name'
        }, {
            name: 'address'
        }, {
            name: 'estore_phoneno'
        }, {
            name: 'order_no'
        }, 
		{
            name: 'courier_no'
        }, 
        {
            name:'invoice_no'
        },
        {
            name: 'invoice_date'
        },
        {
            name:'invoice_status'
        }
		] 
    });
    var filters = new Ext.ux.grid.GridFilters({
        encode: encode,
        local: false,                                      
		filters: [{
				  type: 'numeric',
            dataIndex: 'support_id',
			 phpMode: true
        }, {
			type: 'string',
            dataIndex: 'estore_name',
			phpMode: true

        }, {
			type: 'string',
            dataIndex: 'address',
			phpMode: true

        },
		{
			type: 'string',
            dataIndex: 'estore_phoneno',
			phpMode: true

        },
		{
			type: 'string',
            dataIndex: 'order_no',
			phpMode: true

        },
		{
			type: 'string',
            dataIndex: 'courier_no',
			phpMode: true

        },
		
		]
    });
	
var createColModel = function (finish, start) {
        var columns = [
		sm2,
		{
			header: "Edit",	
			width:40,
		    renderer: function(value, metaData, record, rowIndex, colIndex, store) {
		   		return '<a href="add-csc-order.php?ID='+ record.get('support_id') +'">Edit</a>';
			}
		},
       
		{
			width:40,
            dataIndex: 'support_id',
			header: 'Customer ID',
			hidden:true,
			filterable: true
        }, {
			width:180,
            dataIndex: 'estore_name',
			header: 'Name',
			
        },{
			width:90,
            dataIndex:'estore_phoneno',
			header: 'Contact',
			filterable: true
        },{
			width:100,
            dataIndex: 'order_no',
			header: 'Order No',
			filterable: true
        },
		{
			width:100,
            dataIndex: 'courier_no',
			header: 'Docket No',
			filterable: true
        },
        {
			width:100,
            dataIndex: 'invoice_no',
			header: 'Invoice No',
			filterable: true
        },
        {
			width:100,
            dataIndex: 'invoice_date',
			header: 'Invoice Date',
			filterable: true
        },{
			width:320,
            dataIndex: 'address',
			header: 'Address'
        },
        {
            width:100,
            dataIndex: 'invoice_status',
			header: 'Status',
			filterable: true
        }
		];
		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,
		//height: 450,    
       // width: 400,
    //    layout: 'fit',
		columnLines:true,
    	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 Order',
				ref: '../removeButton',
            	handler: function() {
               location.href="add-csc-order.php"; }
              
		            
      		},
              { 
		    	iconCls: 'remove',
           		text: 'Delete Order',
				ref: '../removeButton',
            	handler: confirmdeleteSchool
              
		            
      		},
			
	  ], 
        bbar: new Ext.PagingToolbar({
            store: store,
            pageSize: 10,
			displayInfo: true,
            plugins: [filters]
        })
    });

    grid.getBottomToolbar().add([
        '->',
      {
            text: 'Clear Filter Data',
            handler: function () {
                grid.filters.clearFilters();
            } 
        }    
    ]);
	
	store.on({
		'beforeload': {
    		fn: function(store, options){
            	options.params || (options.params = {}); 
            	Ext.apply(options.params, {
            		pageNo: this.pageNumber
            	});
        	},
        	scope: this
    	}
	});
	
	grid.on('render', function() {
    grid.tip = new Ext.ToolTip({
    view: grid.getView(),
    target: grid.getView().mainBody,
    delegate: '.x-grid3-row',
    trackMouse: true,
    anchor: 'top',
   renderTo: document.body,
    listeners: {
     beforeshow: function updateTipBody(tip) {
		
	     tip.body.dom.innerHTML = "Click on Edit Link To Edit Record.";
     }
    }
   });
  });
	
	 var SchoolWindow = new Ext.Window({
        title: 'View CSC Order Information',
        height: 400,
        width: 1100,
		modal:true,
		border: false,
        layout: 'fit',
		closeAction:'close',
		collapsible:'true',
        items: grid
    }).show();
	 
	var varWidth =  (screen.width - SchoolWindow.width)/2;
	SchoolWindow.setPosition(varWidth,((screen.height/4)-100));
});

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