<?php

require "_header.php";
//require "data/combo_document.php";
require "data/combo_student.php";
?>
<div id="main-content">
	<script type="text/javascript">
		var document_Fields = [
			{ name: 'document_id', type: 'int' },
			{ name: 'document_name' },
			{ name: 'branch_name' },

		]
		var branchstore = new Ext.data.ArrayStore({
			fields: ['branch_id', 'branch_name'],
			data: Ext.student.document
		});

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


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

		Ext.onReady(function () {
			Ext.QuickTips.init();
			var colModel = new Ext.grid.ColumnModel([
				{
					header: "Document Name",
					width: 250,
					dataIndex: 'document_name',
					sortable: true
				},
				{
					header: "Document Type",
					width: 250,
					dataIndex: 'branch_name',
					sortable: true,

				},

			]);


			var grid = new Ext.grid.GridPanel({
				store: Document,
				cm: colModel,
				columnLines: true,
				stripeRows: true,
				loadMask: true,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp("Documentform").getForm().loadRecord(rec);
							Ext.getCmp('document_name').setValue(rec.data.document_name);
							Ext.getCmp('branch_id').setValue(rec.data.branch_name);
							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: 350,
				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 Documentform = new Ext.FormPanel({
				id: 'Documentform',
				frame: true,
				url: 'save-document.php',
				labelAlign: 'left',
				items: [
					{
						xtype: 'textfield',
						fieldLabel: 'Document Name ',
						name: 'document_name',
						id: 'document_name',
						tabIndex: 1,
						width: 180,
						allowBlank: false
					},
					{
						xtype: 'combo',
						name: 'branch_id',
						id: 'branch_id',
						hiddenName: 'branch_hidden',
						fieldLabel: 'Document Type',
						forceSelection: true,
						displayField: 'branch_name', // Display field
						valueField: 'branch_id',     // Value field
						width: 180,
						emptyText: 'Type',
						allowBlank: true,
						triggerAction: 'all',
						mode: 'local',
						store: branchstore
					},
					{
						xtype: 'textfield',
						name: 'document_id',
						id: 'document_id',
						value: '0',
						hidden: true
					}, { height: 10 },
					grid
				]
			});


			var DocumentWindow = new Ext.Window({
				title: 'Document Master',
				width: 550,
				height: 500,
				frame: true,
				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: Documentform,
				buttons: [
					{
						text: 'Save',
						name: 'btnSave',
						tabIndex: 2,
						iconCls: 'icon-save',
						id: 'btnSave',
						handler: function () {
							var document_name = Ext.getCmp('document_name').getValue();
							var branch_id = Ext.getCmp('branch_id').getValue();
							var document_id = Ext.getCmp('document_id').getValue();

							if (document_name != '' && branch_id != '') {
								Documentform.getForm().submit({
									waitMsg: 'Saving...',
									success: function (form, action) {
										// Handle successful save
										grid.getSelectionModel().clearSelections();
										Ext.getCmp('btnDelete').disable();
										Document.load();
										Documentform.getForm().reset();
										Ext.getCmp('document_name').focus(true, 500);
									},
									failure: function (form, action) {
										// Handle failure
										Ext.MessageBox.show({
											title: 'Error',
											msg: action.result ? action.result.message : 'Failed to save the record. Please try again.',
											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: 10,
						name: 'btnDelete',
						handler: function () {
							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 () {
							Documentform.getForm().reset();
							grid.getSelectionModel().clearSelections();
							Ext.getCmp('btnDelete').disable();
							Ext.getCmp('document_name').focus(true, 500);
							if (flgAdd == "true") {
								Ext.getCmp('btnSave').enable();
							}
						}
					},
					{
						text: 'Close',
						name: 'btnclose',
						tabIndex: 5,
						iconCls: 'icon-close',
						id: 'btnclose',
						handler: function () {
							DocumentWindow.close();
						}
					}
				]
			});

			function deleterecord(btn) {
				if (btn == 'yes') {
					Documentform.getForm().submit({
						waitMsg: 'Saving...',
						params: { act: 'del' },
						success: function () {
							Ext.getCmp('document_id').reset();
							Ext.getCmp('document_name').reset();
							//Ext.getCmp('branch_name').reset();
							Ext.getCmp('branch_id').reset();
							Ext.getCmp('btnDelete').disable();
							Document.load();
							Ext.getCmp('document_name').focus(true, 500);
						}
					});
				}
			}

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

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

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

		});
	</script>
</div>

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