<?php require "_header.php"; ?>
<div id="main-content">
	<script type="text/javascript">
		var Head_Fields = [
			{ name: 'head_id', type: 'int' },
			{ name: 'fees_type' },
			{ name: 'head_name' }
		]

		var labelArray;
		labelArray = [
			['Head Name:<span class="comp">*</span>']
		];
		if(flgDelete == "true") flgDelete1 = true;
			 else flgDelete1 = false;
		if(flgAdd == "true") flgAdd1 = true;
			 else flgAdd1 = false;

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

		var feetypestore = new Ext.data.ArrayStore({
			fields: ['type_value'],
			data: [['Regular'], ['Hostel']]
			//, ['Library'], ['Stationary'], ['Transport']]
		});

		Ext.onReady(function () {
			Ext.QuickTips.init();
			var colModel = new Ext.grid.ColumnModel([{
				header: "Fees Type",
				width: 85,
				dataIndex: 'fees_type',
				sortable: true
			}, {
				header: "Head Name",
				width: 210,
				dataIndex: 'head_name',
				sortable: true
			}]);

			var grid = new Ext.grid.GridPanel({
				store: HeadStore,
				cm: colModel,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp("headform").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.');
							}
							var oldHeadName = rec.get('head_name');
							Ext.getCmp('old_head_name').setValue(oldHeadName);

							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 headform = new Ext.FormPanel({
				id: 'headform',
				frame: true,
				url: 'save-feeshead.php',
				labelAlign: 'left',
				bodyStyle: 'padding:5px',
				layout: 'column',
				items: [
					{
						columnWidth: 1,
						xtype: 'fieldset',
						labelWidth: 85,
						defaults: { border: false },
						autoHeight: true,
						bodyStyle: Ext.isIE ? 'padding:0 0 5px 1px;' : 'padding:5px 1px;',
						border: false,
						items: [
							{
								xtype: 'combo',
								name: 'fees_type',
								labelSeparator: '',
								allowBlank: false,
								id: 'fees_type',
								fieldLabel: 'Fee Type:<span class="comp">*</span>',
								forceSelection: true,
								tabIndex: 1,
								displayField: 'type_value',
								valueField: 'type_value',
								width: 163,
								triggerAction: 'all',
								mode: 'local',
								store: feetypestore
							},
							{
								xtype: 'textfield',
								labelSeparator: '',
								fieldLabel: labelArray[0],
								name: 'head_name',
								width: 163,
								id: 'head_name',
								tabIndex: 2,
								allowBlank: false
							}, {
								xtype: 'textfield',
								name: 'old_head_name',
								id: 'old_head_name',
								hidden: true
							}, { height: 5 },
							grid, {
								xtype: 'textfield',
								name: 'head_id',
								id: 'head_id',
								value: '0',
								hidden: true
							}
						]
					}
				]
			});


			var HeadWindow = new Ext.Window({
				title: 'Fees Head Master',
				width: 350,
				height: 360,
				frame: true,
				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: headform,
				buttons: [
					{
						text: 'Save',
						name: 'btnSave',
						tabIndex: 3,
						iconCls: 'icon-save',
						id: 'btnSave',
						handler: function () {


							var head_name = Ext.getCmp('head_name').getValue();
							var head_id = Ext.getCmp('head_id').getValue();

							if (head_name != "") {
								Ext.Ajax.request({
									loadMask: true,
									sync: true,
									url: 'data/check-duplication.php',
									params: { action: 'head', head_name: head_name, head_id: head_id },
									success: function (resp) {
										resbool = Ext.decode(resp.responseText).success;
										if (resbool == 'false') {
											Ext.MessageBox.show({
												title: 'Error',
												msg: 'head name is already exists.',
												buttons: Ext.MessageBox.OK,
												icon: Ext.MessageBox.ERROR
											});
											headform.getForm().reset();
										}
										else if (resbool == 'true') {
											headform.getForm().submit({
												waitMsg: 'Saving...',
												success: function () {
													grid.getSelectionModel().clearSelections();
													Ext.getCmp('btnDelete').disable();
													HeadStore.load();
													headform.getForm().reset();
													Ext.getCmp('fees_type').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: 4,
						name: 'btnDelete',
						handler: function () {
							var head_id = Ext.getCmp('head_id').getValue();
							Ext.Ajax.request({
								loadMask: true,
								sync: true,
								url: 'data/getRelatedRecord.php',
								params: { action: 'head', head_id: head_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: 5,
						iconCls: 'icon-reset',
						id: 'btnreset',
						handler: function () {
							headform.getForm().reset();
							grid.getSelectionModel().clearSelections();
							Ext.getCmp('btnDelete').disable();
							Ext.getCmp('head_name').focus(true, 500);
							if (flgAdd == "true") {
								Ext.getCmp('btnSave').enable();
							}
						}
					},
					{
						text: 'Close',
						name: 'btnclose',
						tabIndex: 6,
						iconCls: 'icon-close',
						id: 'btnclose',
						handler: function () {
							HeadWindow.close();
						}
					}
				]
			});

			function deleterecord(btn) {
				if (btn == 'yes') {

					//Ext.getCmp('mode').setValue('delete');
					headform.getForm().submit({
										waitMsg: 'Loading...',
										params: { act: 'del' },
										success: function () {
											headform.getForm().reset();
												//Ext.getCmp('type_name').disable();
												Ext.getCmp('btnSave').disable();
												Ext.getCmp('btnDelete').disable();
												Ext.getCmp('head_id').setValue('0');
												
												HeadStore.load();
												Ext.getCmp('head_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
												  });									
										}
									});


					
				}
			}

			var varWidth = (screen.width - HeadWindow.width) / 2;
			HeadWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			HeadWindow.show();
			Ext.getCmp('fees_type').focus(true, 500);
			if (flgAdd == "true") {
				Ext.getCmp('btnSave').enable();
			} else {
				Ext.getCmp('btnSave').disable();
			}

		});
	</script>
</div>
<?php require "_footer.php"; ?>