<?php require "_header.php"; ?>
<div id="main-content">
	<script type="text/javascript">
		var Branch_Fields = [
			{ name: 'branch_id', type: 'int' },
			{ name: 'branch_name' }
		]
		var labelArray;
		labelArray = [
			['Course Name:<span class="comp">*</span>']
		];
		var BranchStore = new Ext.data.JsonStore({
			url: 'data/branch-grid-data.php',
			root: 'data',
			fields: Branch_Fields,
			idProperty: 'branch_id',
			autoLoad: true
		});

		Ext.onReady(function () {
			Ext.QuickTips.init();
			var colModel = new Ext.grid.ColumnModel([{
				header: "Course Name",
				width: 280,
				dataIndex: 'branch_name', // Match with the field name
				sortable: true
			}]);
			var grid = new Ext.grid.GridPanel({
				store: BranchStore,
				cm: colModel,
				renderTo: 'main-content',
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp("branchform").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 branchform = new Ext.FormPanel({
				id: 'branchform',
				frame: true,
				url: 'save-branch.php',
				labelAlign: 'left',
				bodyStyle: 'padding:5px',
				layout: 'column',
				items: [{
					columnWidth: 1,
					xtype: 'fieldset',
					labelWidth: 85,
					defaults: { border: false },
					defaultType: 'textfield',
					autoHeight: true,
					bodyStyle: Ext.isIE ? 'padding:0 0 5px 1px;' : 'padding:5px 1px;',
					border: false,
					style: {
						"margin-left": "5px"
					},
					items: [
						{
						xtype: 'textfield',
						labelSeparator: '',
						fieldLabel: labelArray[0],
						name: 'branch_name',
						id: 'branch_name',
						tabIndex: 1,
						allowBlank: false
					}, {
						xtype: 'textfield',
						name: 'branch_id',
						id: 'branch_id',
						value: '0',
						hidden: true
					}, grid
				]
				}]
			});

			var BranchWindow = new Ext.Window({
				title: 'Couse Master',
				width: 350,
				height: 340,
				frame: true,
				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: branchform,
				buttons: [{
					text: 'Save',
					name: 'btnSave',
					tabIndex: 2,
					iconCls: 'icon-save',
					id: 'btnSave',
					handler: function () {
						var branch_name = Ext.getCmp('branch_name').getValue();
						var branch_id = Ext.getCmp('branch_id').getValue();
						if (branch_name != "") {
							Ext.Ajax.request({
								loadMask: true,
								sync: true,
								url: 'data/check-duplication.php',
								params: { action: 'branch', branch_name: branch_name, branch_id: branch_id },
								success: function (resp) {
									resbool = Ext.decode(resp.responseText).success;
									if (resbool == 'false') {
										Ext.MessageBox.show({
											title: 'Error',
											msg: 'enter data is already exists.',
											buttons: Ext.MessageBox.OK,
											icon: Ext.MessageBox.ERROR
										});
										Ext.getCmp('branch_name').reset();
									} else if (resbool == 'true') {
										branchform.getForm().submit({
											waitMsg: 'Saving...',
											success: function () {
												grid.getSelectionModel().clearSelections();
												Ext.getCmp('btnDelete').disable();
												BranchStore.load();
												branchform.getForm().reset();
												Ext.getCmp('branch_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',
					tabIndex: 3,
					disabled: true,
					id: 'btnDelete',
					name: 'btnDelete',
					handler: function () {
						var branch_id = Ext.getCmp('branch_id').getValue();
						Ext.Ajax.request({
							loadMask: true,
							sync: true,
							url: 'data/getRelatedRecord.php',
							params: { action: 'branch', branch_id: branch_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 () {
						branchform.getForm().reset();
						grid.getSelectionModel().clearSelections();
						Ext.getCmp('btnDelete').disable();
						Ext.getCmp('branch_name').focus(true, 500);
						if (flgAdd == "true") {
							Ext.getCmp('btnSave').enable();
						}
					}
				}, {
					text: 'Close',
					name: 'btnclose',
					tabIndex: 5,
					iconCls: 'icon-close',
					id: 'btnclose',
					handler: function () {
						BranchWindow.close();
					}
				}
				]
			});

			function deleterecord(btn) {
				if (btn == 'yes') {
					branchform.getForm().submit({
						waitMsg: 'Saving...',
						params: { act: 'del' },
						success: function () {
							branchform.getForm().reset();
							Ext.getCmp('btnDelete').disable();
							Ext.getCmp('branch_id').setValue('0');
							BranchStore.load();
							Ext.getCmp('branch_name').focus(true, 500);
						}
					});
				}
			}
			var varWidth = (screen.width - BranchWindow.width) / 2;
			BranchWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			BranchWindow.show();
			if (flgAdd == "true") {
				Ext.getCmp('btnSave').enable();
			} else {
				Ext.getCmp('btnSave').disable();
			}
			Ext.getCmp('branch_name').focus(true, 1000);
		});
	</script>
</div>
<?php require "_footer.php"; ?>