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

		Ext.QuickTips.init();

		var designationObj = Ext.data.Record.create([{
				name: 'designation_id'
			},
			{
				name: 'designation_name'
			}
		]);

		var designationstore = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({
				url: 'data/getdesignation.php',
				success: function(response, options) {
					var json = Ext.util.JSON.decode(response.responseText);
				}
			}),
			reader: new Ext.data.JsonReader({
				root: 'rows',
				id: 'designation_id',
				labelField: 'designation_name'
			}, designationObj)
		});

		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.employee_id);
			}
			var encoded_array = Ext.encode(prez);

			Ext.Ajax.request({
				loadMask: true,
				sync: true,
				url: 'data/getalltableemp.php',
				params: {
					employee_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.employee_id);
				}
				var encoded_array = Ext.encode(prez);
				Ext.Ajax.request({
					waitMsg: 'Please Wait',
					url: 'data/employee-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/csccustomer-grid-filter.php',
			remoteSort: true,
			sortInfo: {
				field: 'customer_id',
				direction: 'ASC'
			},
			storeId: 'myStore',
			idProperty: 'customer_id',
			root: 'data',
			totalProperty: 'total',
			fields: [{
					name: 'customer_id',
					type: 'int'
				}, {
					name: 'estore_name'
				}, {
					name: 'address'
				}, {
					name: 'estore_phoneno'
				}, {
					name: 'order_no'
				},
				{
					name: 'invoice_status'
				}
			]
		});

		var filters = new Ext.ux.grid.GridFilters({
			encode: encode,
			local: false,
			filters: [{
				type: 'numeric',
				dataIndex: 'customer_id'
			}, {
				type: 'string',
				dataIndex: 'estore_name'
			}, {
				type: 'string',
				dataIndex: 'address'
			}, {
				type: 'string',
				dataIndex: 'estore_phoneno'
			}, {
				type: 'string',
				dataIndex: 'order_no'
			}, ]
		});

		var createColModel = function(finish, start) {
			var columns = [
				sm2,
				{
					header: "Print",
					width: 35,
					renderer: function(value, metaData, record, rowIndex, colIndex, store) {
						return '<a target=_blank href="csc-invoice.php?ID=' + record.get('customer_id') + '">Address</a>';
					}
				},
				{
					header: "Invoice",
					width: 50,
					renderer: function(value, metaData, record, rowIndex, colIndex, store) {
						if(record.get('invoice_status')=='Pending')
						{
							// return '<a target="_blank" href="csc-invoice-print.php?ID=' + record.get('customer_id') + '&Status=' + record.get('invoice_status') + '"  >Invoice</a>';

							 return '<p>Invoice</p>';
						}
						else
						{
							return '<a target="_blank" href="csc-invoice-print.php?ID=' + record.get('customer_id') + '&Status=' + record.get('invoice_status') + '">Invoice</a>';

						}
						

					}
				},
				{
					dataIndex: 'customer_id',
					header: 'Customer Id',
					align: 'center',
					hidden: true
				}, {
					dataIndex: 'estore_name',
					header: 'Store Name',
					width: 150,
					align: 'center'
				}, {
					dataIndex: 'address',
					header: 'Address',
					width: 400
				}, {
					dataIndex: 'estore_phoneno',
					header: 'Phone No',
					width: 100
				}, {
					dataIndex: 'order_no',
					header: 'Order No',
					width: 100
				},
				{
					dataIndex: 'invoice_status',
					header: 'Status',
					width: 100
				},
			];
			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 Employee',
					handler: function() {
						location.href = "addedit-employee.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();
				}
			}
		]);
		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,
				renderTo: document.body,
				listeners: {
					beforeshow: function updateTipBody(tip) {
						tip.body.dom.innerHTML = "Click On Edit Link to Edit Record.";
					}
				}
			});
		});



		var ViewEmployeeWindow = new Ext.Window({
			title: 'View Customer Information',
			height: 360,
			width: 1000,
			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"; ?>