<?php
require "_header.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";

?>
<div id="main-content">
	<script type="text/javascript" language="javascript">
		Ext.onReady(function () {
			Ext.QuickTips.init();


			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.semester.semester
			});
			var yearstore = new Ext.data.ArrayStore({
				fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
				data: Ext.year.year
			});

			function formatDate(value) {

				var temp = value.dateFormat('d/m/Y');
				if (temp == "28/02/1900") {
					return '';
				}
				else {
					return temp;
				}
			}

			store = new Ext.data.JsonStore({
				autoDestroy: true,
				url: 'data/student-gtu-data.php',
				remoteSort: true,
				storeId: 'myStore',
				idProperty: 'student_id',
				root: 'data',
				totalProperty: 'total',
				fields: [{
					name: 'student_id',
					type: 'int'
				}, {
					name: 'student_gturegno'
				}, {
					name: 'branchyear_name'
				},
				{
					name: 'semester_name'
				}, {
					name: 'branch_name'
				},
				{
					name: 'year_name'
				}, {
					name: 'studentname'
				}]
			});

			var createColModel = function (finish, start) {
				var columns = [
					new Ext.grid.RowNumberer(),
					{
						dataIndex: 'student_id',
						header: 'Student ID',
						width: 80,
						hidden: true,
						filterable: true
					}, {
						dataIndex: 'branch_name',
						header: 'Course',
						width: 200
					}, {
						dataIndex: 'year_name',
						header: 'Batch Year',
						width: 60
					},
					{
						dataIndex: 'branchyear_name',
						header: 'Course Year',
						width: 60
					}, {
						dataIndex: 'semester_name',
						header: 'Semester',
						width: 60
					}, {
						dataIndex: 'studentname',
						header: 'Student Name',
						id: 'SurName',
						width: 220
					}, {
						dataIndex: 'student_gturegno',
						header: 'Enrollment No',
						width: 170,
						editor: {
							xtype: 'textfield',
							style: 'text-align:left',
							allowNegative: false,
							allowBlank: false
						}
					}];

				return new Ext.grid.ColumnModel({
					columns: columns.slice(start || 0, finish),
					defaults: {
						sortable: true
					}
				});
			};
			var grid = new Ext.grid.EditorGridPanel({
				id: 'grid',
				name: 'grid',
				store: store,
				clicksToEdit: 1,
				colModel: createColModel(40),
				loadMask: true,
				frame: false,
				enableColLock: false,
				columnLines: true,
				stripeRows: true,
				height: 370,
				width: 900,
				layout: 'fit',
				selModel: new Ext.grid.RowSelectionModel({ singleSelect: false }),

			});

			var studententry = new Ext.FormPanel({
				autoHeight: true,
				url: 'save-updated-gtuno.php',
				frame: true,
				standardSubmit: false,
				layout: 'column',
				items: [{
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .50,
					labelWidth: 90,
					items: [{
						xtype: 'combo',
						name: 'branch_id',
						hiddenName: 'branch_hidden',
						id: 'branch_id',
						fieldLabel: 'Course <span class="comp">*</span>',
						forceSelection: true,
						displayField: 'branch_name',
						valueField: 'branch_id'
						, triggerAction: 'all'
						, mode: 'local'
						, store: branchstore
						, width: 250
						, tabIndex: 1
						, selectOnFocus: true,
						listeners: {
							'select': function (cmb, rec, idx) {
								semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
								yearstore.filter('branch_id', cmb.getValue(), false, true, true);
								var semesterCombo = Ext.getCmp('semester_id');
								if (cmb.getValue() == 3 || cmb.getValue() == 5) {
									semesterCombo.enable();
								} else {
									semesterCombo.disable();
									semesterCombo.reset();
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'branchyear_id',
						hiddenName: 'branchyear_hidden',
						id: 'branchyear_id',
						fieldLabel: 'Course Year ',
						lastQuery: '',
						displayField: 'branchyear_name',
						valueField: 'branchyear_id'
						, triggerAction: 'all'
						, mode: 'local'
						, store: yearstore
						, width: 250
						, tabIndex: 3
						, listeners: {
							'select': function (cmb, rec, idx) {
								if (Ext.getCmp('branch_id').getValue() != "") {
									Ext.getCmp('semester_id').reset();
								}
							}
						}
					}]
				},
				{
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .50,
					labelWidth: 90,
					items: [{
						xtype: 'combo',
						name: 'year_id',
						hiddenName: 'year_hidden',
						id: 'year_id',
						fieldLabel: 'Batch Year ',
						lastQuery: '',
						displayField: 'year_name',
						valueField: 'year_id'
						, triggerAction: 'all'
						, mode: 'local'
						, store: batchyearstore
						, width: 250
						, tabIndex: 2
					}, {
						xtype: 'combo',
						name: 'semester_id',
						hiddenName: 'semester_hidden',
						id: 'semester_id',
						fieldLabel: 'Semester ',
						lastQuery: '',
						displayField: 'semester_name',
						valueField: 'semester_id'
						, triggerAction: 'all'
						, mode: 'local',
						disabled: true
						, store: semesterstore
						, width: 250
						, tabIndex: 4
						, listeners: {
							'select': function (cmb, rec, idx) {
								if (Ext.getCmp('branch_id').getValue() != "") {
									Ext.getCmp('branchyear_id').reset();
								}
							}
						}
					}]
				}
					, {
					columnWidth: 0.07,
					labelWidth: 10,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'button',
						text: 'Search',
						iconCls: 'icon-search',
						handler: function () {
							var barnch = Ext.getCmp('branch_id').getValue();
							var year = Ext.getCmp('year_id').getValue();
							var branchyear = Ext.getCmp('branchyear_id').getValue();
							var semester = Ext.getCmp('semester_id').getValue();
							if (barnch != "" || semester != "" || branchyear != "") {
								store.reload({
									params: {
										barnch: barnch,
										year: year,
										branchyear: branchyear,
										semester: semester,
									}
								});
							}
							else {
								alert("Select All Criteriya");
							}
						}
					}]
				}]

			});

			var StudentWindow = new Ext.Window({
				title: 'Update Student Enrollment No',
				width: 880,
				height: 500,
				modal: true,
				shadow: false,
				border: false,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: [studententry, grid],
				buttonAlign: 'center',
				buttons: [{
					text: 'Save',
					iconCls: 'icon-save',
					name: 'btnSave',
					tabIndex: 5,
					id: 'btnSave',
					handler: function () {
						grid.stopEditing();
						if (grid.store.getCount() >= 1) {
							var arr_studentid = " ";
							var arr_gtu_no = " ";
							var student_name = " ";
							var flg = 0;
							var at = "";
							for (var i = 0, len = grid.store.getCount(); i < len; i++) {
								record = grid.store.getAt(i);
								student_id = record.data.student_id;
								gtu_no = record.data.student_gturegno;

								if (gtu_no === "") {

									flg = 1;
									student_name = record.data.studentname;
									at = i;
									break;
								}
								else {
									arr_studentid += student_id + ",";
									arr_gtu_no += gtu_no + ",";
								}

							}
							if (flg == 0) {
								studententry.getForm().submit({
									params: { student_id: arr_studentid, gtu_no: arr_gtu_no },
									waitMsg: 'Saving...',
									success: function () {
										Ext.MessageBox.show({
											title: 'Message',
											msg: 'Data has been saved..',
											buttons: Ext.MessageBox.OK,
											icon: Ext.MessageBox.INFO
										});
										studententry.getForm().reset();
										store.removeAll();
									},
									failure: function () {
										Ext.MessageBox.show({
											title: 'Error',
											msg: 'Please fill complete form!',
											buttons: Ext.MessageBox.OK,
											icon: Ext.MessageBox.ERROR
										});
									}
								});
							}
							else {
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please Enter Gtu No For Student <span class="comp">' + student_name + '</span>.',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.ERROR
								});
								grid.getSelectionModel().selectRow(at);
							}
						} else {
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Please enter valid form no.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						}
					}
				}, {
					text: 'Reset',
					name: 'btnreset',
					iconCls: 'icon-reset',
					tabIndex: 7,
					id: 'btnreset',
					handler: function () {
						store.removeAll();
							studententry.getForm().reset();
							Ext.getCmp('branch_id').focus(true, 500);
					}
				}, {
					text: 'Close',
					name: 'btnclose',
					tabIndex: 8,
					id: 'btnclose',
					iconCls: 'icon-close',
					handler: function () {
						StudentWindow.close();
					}
				}]

			});

			var varWidth = (screen.width - StudentWindow.width) / 2;
			StudentWindow.setPosition(varWidth, ((screen.height / 4) - 160));
			StudentWindow.show();
			Ext.getCmp('branch_id').focus(true, 500);

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