<?php
require "_header.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
?>
<div id="main-content">
<script language="javascript" src="js/add-tt-scholarship.js"></script>
	<script type="text/javascript">
		var labelArray;
		labelArray = [
			['Enrollment Reg No '],
			['Course '],
			['Batch Year '],
			['Semester '],
			['Course Year '],
			['Student Name '],
			['Scholarship Name '],
		];

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

		var batchyearstore = new Ext.data.ArrayStore({
			fields: ['year_id', 'year_name'],
			data: Ext.semester.yearname
		});

		var semesterstore = new Ext.data.ArrayStore({
			fields: ['branch_id', 'semester_id', 'semester_name'],
			data: Ext.student.semester
		});

		var yearstore = new Ext.data.ArrayStore({
			fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
			data: Ext.year.year
		});

		var studentstore = new Ext.data.JsonStore({
			url: 'data/getstudent-feesrecive.php',
			root: 'data',
			totalProperty: 'total',
			fields: [
				{ name: 'student_id', type: 'int' },
				{ name: 'student_name', type: 'string' },
				{ name: 'student_gturegno', type: 'int' }
			]
		});

		var scholarshipstore = new Ext.data.JsonStore({
			url: 'data/get-student-scholarship.php',
			totalProperty: 'count',
			idProperty: 'scholarship_id',
			successProperty: 'success',
			fields: [
				{ name: 'scholarship_id', type: 'int' },
				{ name: 'scholarship_name', type: 'string' },
				{ name: 'financial_year', type: 'string' },
				{ name: 'educational_year', type: 'string' },
				{ name: 'formfill_date', type: 'date' },
				{ name: 'scholarshipaprove_date', type: 'date' },
				{ name: 'schiarshiprecive_date', type: 'date' },
				{ name: 'schiarshiprecive_price', type: 'float' },
				{ name: 'collegerecive_date', type: 'date' },
				{ name: 'collegerecive_price', type: 'float' }
			],
			root: 'data',
			writer: new Ext.data.JsonWriter(),
			autoSave: true
		});

		Ext.onReady(function () {
			Ext.QuickTips.init();

			studentstore.load(); // Assuming students need to be loaded initially

			scholarshipstore.load({
				callback: function (records, operation, success) {
					if (!success) {
						Ext.MessageBox.show({
							title: 'Error',
							msg: 'Failed to load Student Scholarships',
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.ERROR
						});
					} else {
						if (scholarshipstore.getCount() <= 0) {
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'There is no Student Scholarship found',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						} else {
							grid.startEditing(0, 4);
						}
					}
				}
			});

			var grid = new Ext.grid.EditorGridPanel({
				store: scholarshipstore,
				columns: [
					new Ext.grid.RowNumberer(),
					{
						dataIndex: 'scholarship_name',
						header: 'Scholarship Name',
						sortable: true,
						editor: new Ext.form.TextField(),
						width: 100
					},
					{
						dataIndex: 'financial_year',
						header: 'Financial Year',
						sortable: true,
						editor: new Ext.form.TextField(),
						width: 100
					},
					{
						dataIndex: 'educational_year',
						header: 'Educational Year',
						sortable: true,
						editor: new Ext.form.TextField(),
						width: 150
					},
					{
						dataIndex: 'formfill_date',
						header: 'Formfill Date',
						sortable: true,
						renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
						editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
						width: 100
					},
					{
						dataIndex: 'scholarshipaprove_date',
						header: 'Scholarship Approve Date',
						sortable: true,
						renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
						editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
						width: 100
					},
					{
						dataIndex: 'schiarshiprecive_date',
						header: 'Scholarship Recive Date',
						sortable: true,
						renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
						editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
						width: 100
					},
					{
						dataIndex: 'schiarshiprecive_price',
						header: 'Scholarship Recive Price',
						sortable: true,
						editor: new Ext.form.NumberField({ allowDecimals: true, decimalPrecision: 2 }),
						width: 120
					},
					{
						dataIndex: 'collegerecive_date',
						header: 'College Recive Date',
						sortable: true,
						renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
						editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
						width: 100
					},
					{
						dataIndex: 'collegerecive_price',
						header: 'College Recive Price',
						sortable: true,
						editor: new Ext.form.NumberField({ allowDecimals: true, decimalPrecision: 2 }),
						width: 120
					}
				],
				height: 500,
				width: 1000,
				layout: 'fit',
				loadMask: true,
				stripeRows: true,
				columnLines: true,
				autoScroll: true,
				selModel: new Ext.grid.RowSelectionModel({ singleSelect: true })
			});

			var ScholarshipForm = new Ext.FormPanel({
				labelWidth: 80,
				url: 'save-studentscholarship.php',
				frame: true,
				autoHeight: true,
				standardSubmit: true,
				bodyStyle: 'padding:6px 6px 0',
				layout: 'form',
				items: [
					{
						layout: 'column',
						items: [
							{
								columnWidth: .50, layout: 'form', labelWidth: 56,
								items: [
									{
										xtype: 'combo',
										name: 'branch_name',
										id: 'branch_name',
										hiddenName: 'branch_hidden',
										labelSeparator: '',
										fieldLabel: labelArray[1],
										displayField: 'branch_name',
										valueField: 'branch_id',

										width: 180,
										triggerAction: 'all',
										mode: 'local',
										allowBlank: true,
										store: branchstore,
										listeners: {
											'select': function (cmb, rec, idx) {
												Ext.getCmp('semester_name').reset();
												Ext.getCmp('branchyear_name').reset();
												Ext.getCmp('student_id').reset();
												studentstore.removeAll();
												semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
												yearstore.filter('branch_id', cmb.getValue(), false, true, true);
												var semesterCombo = Ext.getCmp('semester_name');
												if (cmb.getValue() == 3 || cmb.getValue() == 5) {
													semesterCombo.enable();
												} else {
													semesterCombo.disable();
													semesterCombo.reset();
												}
											}
										}
									},
									{
										xtype: 'combo',
										name: 'year_name',
										id: 'year_name',
										hiddenName: 'year_hidden',
										labelSeparator: '',
										fieldLabel: labelArray[2],
										displayField: 'year_name',
										valueField: 'year_name',

										lastQuery: '',
										triggerAction: 'all',
										mode: 'local',
										width: 180,
										allowBlank: true,
										store: batchyearstore,

									}, {
										xtype: 'combo',
										name: 'student_id',
										id: 'student_id',
										hiddenName: 'student_hidden',
										labelSeparator: '',
										fieldLabel: labelArray[5],
										displayField: 'student_name',
										valueField: 'student_id',

										width: 180,
										triggerAction: 'all',
										mode: 'local',
										allowBlank: true,
										store: studentstore,
										listeners: {
											'select': function (cmb, rec, idx) {
												scholarshipstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'semester_id': Ext.getCmp('semester_name').getValue(), 'branchyear_id': Ext.getCmp('branchyear_name').getValue(), 'student_id': Ext.getCmp('student_id').getValue() } });
												if (Ext.getCmp('branch_name').getValue() != "" && Ext.getCmp('semester_name').getValue() != "") {
												}
												else if (Ext.getCmp('branch_name').getValue() != "" && Ext.getCmp('branchyear_name').getValue() != "") {
												} else {
													alert('Select Branch')
													this.reset();
												}
											}
										}
									}
								]
							},
							{
								columnWidth: .50, layout: 'form', labelWidth: 70,
								items: [
									{
										xtype: 'combo',
										name: 'branchyear_name',
										id: 'branchyear_name',
										hiddenName: 'branchyear_hidden',
										labelSeparator: '',
										fieldLabel: labelArray[4],
										displayField: 'branchyear_name',
										valueField: 'branchyear_id',
										lastQuery: '',
										triggerAction: 'all',
										mode: 'local',
										width: 180,
										allowBlank: true,
										store: yearstore,
										listeners: {
											'select': function (cmb, rec, idx) {
												if (Ext.getCmp('branch_name').getValue() != "") {

													Ext.getCmp('semester_name').reset();
													Ext.getCmp('student_id').reset();
													studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'branchyear_id': Ext.getCmp('branchyear_name').getValue() } });

												} else {
													alert('Select Branch First')
													this.reset();
												}

											}
										}
									}, {
										xtype: 'combo',
										name: 'semester_name',
										id: 'semester_name',
										hiddenName: 'semester_hidden',
										fieldLabel: labelArray[3],
										displayField: 'semester_name',
										valueField: 'semester_id',
										disabled: true,
										lastQuery: '',
										triggerAction: 'all',
										mode: 'local',
										width: 180,
										allowBlank: true,
										store: semesterstore,
										listeners: {
											'select': function (cmb, rec, idx) {

												if (Ext.getCmp('branch_name').getValue() != "") {
													Ext.getCmp('branchyear_name').reset();
													Ext.getCmp('student_id').reset();
													studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'semester_id': Ext.getCmp('semester_name').getValue() } });

												} else {
													alert('Select Branch First')
													this.reset();
												}

											}
										}
									},{
										columnWidth: 0.2,
										layout: 'column',
										style: 'padding:5px 0px',
										items: [{
											columnWidth: 0.5,
											layout: 'form',
											style: 'padding:0px 0px 0px 90px',
											items: [{
												xtype: 'button',
												text: 'Add New Scholarship',
												iconCls: 'icon-add',
												handler: function () {
													AddScholarshipWindow.show();
												}

											}]
										}, {
											columnWidth: 0.2,
											layout: 'form',
											labelWidth: 70,
											items: [{
												xtype: 'button',
												text: 'Reset',
												iconCls: 'icon-reset',
												style: 'padding:0px 0px 0px 0px',
												handler: function () {
													ScholarshipForm.getForm().reset();
													scholarshipstore.removeAll();
													ScholarshipForm.getForm().findField('branch_name').focus(true, 500);
												}
											}]
										}]
									}
								]
							}
						]
					}, {
						columnWidth: 1,
						layout: 'fit',
						items: [grid]
					}
				],

			});
			var ScholarshipWindow = new Ext.Window({
				title: 'Student Scholarship',

				width: 1050,
				height: 460,

				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,

				items: ScholarshipForm,
				buttons: [{
					text: 'Save', name: 'btnSave', id: 'btnSave',
					handler: function () {
						var branch_name = subentry.getForm().findField('branch_name').getValue();
						var semester_name = subentry.getForm().findField('semester_name').getValue();
						var branchyear_name = subentry.getForm().findField('branchyear_name').getValue();
						var year_name = subentry.getForm().findField('year_name').getValue();
						var student_id = subentry.getForm().findField('student_id').getValue();


						ScholarshipForm.getForm().submit({
							waitMsg: 'Saving...',
							success: function () {
								Ext.MessageBox.show({
									title: 'Message',
									msg: 'Data has been saved.',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.INFO
								});

								Ext.getCmp('btnDel').disable();
								Ext.getCmp('btnSave').disable();
								ScholarshipForm.getForm().findField('scholarship_name').disable();


								Store.load();
								ScholarshipForm.getForm().reset();
								grid.getSelectionModel().clearSelections();
							},
							failure: function () {
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please fill up complete form.',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.ERROR
								});
							}
						});
					}
				},
				{
					text: 'Delete',
					name: 'btnDel',
					iconCls: 'remove',
					id: 'btnDel',

					handler: function () {
						ScholarshipForm.getForm().findField('mode').setValue('delete');
						if (ScholarshipForm.getForm().findField('scholarship_name').getValue() == '') {
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Please select a record.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						}
						else {
							Ext.MessageBox.confirm('Confirmation', 'Do u want to Delete this record?', deletesubject);
						}
					}
				},
				{
					text: 'Close', name: 'btnclose', id: 'btnclose',
					handler: function () {

						ScholarshipWindow.close();
					}
				}]
			});

			var varWidth = (screen.width - ScholarshipWindow.width) / 2;
			ScholarshipWindow.setPosition(varWidth, ((screen.height / 4) - 110));

			ScholarshipWindow.show();

			//if(flgAdd == "true"){
			Ext.getCmp('btnSave').enable();
			//} else{
			//Ext.getCmp('btnSave').disable();
			//}
			Ext.getCmp('branch_name').focus(true, 1000);
		});
	</script>
	<div id="abcd"></div>
</div>
<?php require "_footer.php"; ?>