<?php
require "_header.php";
?>
<div id="main-content">
	<script type="text/javascript">
		var Route_Fields = [{
			name: 'route_id',
			type: 'int'
		},
		{
			name: 'route_name'
		},
		{
			name: 'route_area'
		},
		{
			name: 'route_fees'
		},
		]

		if (flgDelete == "true") flgDelete1 = true;
		else flgDelete1 = false;
		if (flgAdd == "true") flgAdd1 = true;
		else flgAdd1 = false;
		var flgalert = 0;


		var RouteStore = new Ext.data.JsonStore({
			url: 'data/Route-grid-data.php',
			root: 'data',
			fields: Route_Fields,
			autoLoad: true
		});

		Ext.onReady(function () {
			Ext.QuickTips.init();
			var colModel = new Ext.grid.ColumnModel([


				{
					header: "Route Name",
					width: 140,
					dataIndex: 'route_name',
					sortable: true
				},
				{
					header: "Route Area",
					width: 140,
					dataIndex: 'route_area',
					sortable: true,
					hidden: true
				},
				{
					header: "Route Fees",
					width: 140,
					dataIndex: 'route_fees',
					sortable: true,
					hidden: true
				},
			]);


			var grid = new Ext.grid.GridPanel({
				store: RouteStore,
				cm: colModel,
				columnLines: true,
				stripeRows: true,
				loadMask: true,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp("Routeform").getForm().loadRecord(rec);
							if (flgEdit == "true") {
								Ext.getCmp('btnSave').enable();
							} else {
								Ext.getCmp('btnSave').disable();
								alert('You do not have access to edit the record.');
							}

							if (flgDelete == "true") {
								Ext.getCmp('btnDelete').enable();
							}
						}
					}
				}),
				height: 210,
				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 record.";
						}
					}
				});
			});

			var Routeform = new Ext.FormPanel({
				id: 'Routeform',
				frame: true,
				url: 'save-route.php',
				labelAlign: 'left',
				items: [{
					xtype: 'textfield',
					labelSeparator: '',
					fieldLabel: 'Route Name',
					name: 'route_name',
					id: 'route_name',
					tabIndex: 1,
					allowBlank: false
				},
				{
					xtype: 'textfield',
					labelSeparator: '',
					fieldLabel: 'Route Area',
					name: 'route_area',
					id: 'route_area',
					tabIndex: 1,
					hidden: true
				},
				{
					xtype: 'textfield',
					labelSeparator: '',
					fieldLabel: 'Route Fees',
					name: 'route_fees',
					id: 'route_fees',
					tabIndex: 1,
					hidden: true
				},
				{
					xtype: 'textfield',
					name: 'route_id',
					id: 'route_id',
					value: '0',
					hidden: true
				}, {
					height: 10
				},
					grid
				]
			});


			var RouteWindow = new Ext.Window({
				title: 'Route Master',
				width: 350,
				height: 340,
				frame: true,
				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: Routeform,
				buttons: [{
					text: 'Save',
					name: 'btnSave',
					tabIndex: 2,
					iconCls: 'icon-save',
					id: 'btnSave',
					handler: function () {


						var route_name = Ext.getCmp('route_name').getValue();
						var route_area = Ext.getCmp('route_area').getValue();
						var route_id = Ext.getCmp('route_id').getValue();

						if (route_name != "") {
							Routeform.getForm().submit({
								waitMsg: 'Saving...',
								success: function () {
									grid.getSelectionModel().clearSelections();
									Ext.getCmp('btnDelete').disable();
									RouteStore.load();
									Routeform.getForm().reset();
									Ext.getCmp('route_name').focus(true, 500);
								},
								failure: function () {
									Ext.MessageBox.show({
										title: 'Error',
										msg: 'Please fill up complete form.',
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.ERROR
									});
								}

							});
						} else {
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Please fill up complete form.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						}



					}
				},
				{
					text: 'Delete',
					iconCls: 'remove',
					disabled: true,
					id: 'btnDelete',
					tabIndex: 3,
					name: 'btnDelete',
					handler: function () {
						var route_id = Ext.getCmp('route_id').getValue();
						Ext.Ajax.request({
							loadMask: true,
							sync: true,
							url: 'data/getRelatedRecord.php',
							params: {
								action: 'Route',
								route_id: route_id
							},
							success: function (resp) {
								var str = "";
								resbool = resp.responseText;
								if (resbool != 0) {
									str = "It has related record in " + resbool + "Please Delete This Record First";
									Ext.MessageBox.show({
										title: 'Error',
										msg: str,
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.ERROR
									});
								} else {
									Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
								}
							}
						});
					}
				},
				{
					text: 'Reset',
					name: 'btnreset',
					tabIndex: 4,
					iconCls: 'icon-reset',
					id: 'btnreset',
					handler: function () {
						Routeform.getForm().reset();
						grid.getSelectionModel().clearSelections();
						Ext.getCmp('btnDelete').disable();
						Ext.getCmp('Route_name').focus(true, 500);
						if (flgAdd == "true") {
							Ext.getCmp('btnSave').enable();
						}
					}
				},
				{
					text: 'Close',
					name: 'btnclose',
					tabIndex: 5,
					iconCls: 'icon-close',
					id: 'btnclose',
					handler: function () {
						RouteWindow.close();
					}
				}
				]
			});

			function deleterecord(btn) {
				if (btn == 'yes') {
					Routeform.getForm().submit({
						waitMsg: 'Saving...',
						params: {
							act: 'del'
						},
						success: function () {
							Routeform.getForm().reset();
							Ext.getCmp('btnDelete').disable();
							Ext.getCmp('route_id').setValue('0');
							RouteStore.load();
							Ext.getCmp('route_name').focus(true, 500);
						}
					});
				}
			}

			var varWidth = (screen.width - RouteWindow.width) / 2;
			RouteWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			RouteWindow.show();

			if (flgAdd == "true") {
				Ext.getCmp('btnSave').enable();
			} else {
				Ext.getCmp('btnSave').disable();
			}

			Ext.getCmp('route_name').focus(true, 1000);

		});
	</script>
</div>

<?php require "_footer.php"; ?>