<?php
require "_header.php";
require "includeGridFilters.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
$username = $_SESSION["VedantnrUserName"];
?>
<div id="main-content">
	<!-- start custom CSS/JS -->
	<script type="text/javascript">
		var username = '<?php echo $username; ?>';
	</script>
	<script src="js/login-confirm.js" type="text/javascript"></script>
	<script>
		var loadingMask = new Ext.LoadMask(Ext.getBody(), { msg: "Loading data, please wait...", id: "loadingMask" });

		function deletedata() {
			var grid = Ext.getCmp('grid');
			var selections = grid.selModel.getSelections();
			var prez = [];
			for (i = 0; i < grid.selModel.getCount(); i++) {
				prez.push(selections[i].json.student_id);
			}
			var encoded_array = Ext.encode(prez);
			Ext.Ajax.request({
				waitMsg: 'Please Wait',
				url: 'data/student-grid-delete.php',
				params: {
					task: "DELETESTU",
					ids: encoded_array
				},

				success: function (response) {
					var result = eval(response.responseText);
					switch (result) {
						case 1:
							store.reload();
							break;
						default:
							Ext.MessageBox.alert('Warning', 'Could not delete the entire selection.');
							break;
					}
				},
				failure: function (response) {
					var result = response.responseText;
					Ext.MessageBox.alert('error', 'could not connect to the database. retry later');
				}
			});
		}
		Ext.onReady(function () {
			loadingMask.show();
			Ext.QuickTips.init();
			var xg = Ext.grid;
			var sm2 = new xg.CheckboxSelectionModel({
				listeners: {
					selectionchange: function (sm) {
						if (sm.getCount()) {
							if (flgDelete == "true") {
								grid.removeButton.enable();
							}
						} else {
							grid.removeButton.disable();
						}
					}
				}
			});

			var url = {
				local: 'grid-filter.json',
				remote: 'data/student-grid-filter.php'
			};
			var encode = false;
			var local = false;

			function confirmDeleteStudents() {

				var selections = grid.selModel.getSelections();
				var prez = [];
				for (i = 0; i < grid.selModel.getCount(); i++) {
					prez.push(selections[i].json.student_id);
				}
				var encoded_array = Ext.encode(prez);

				Ext.Ajax.request({
					loadMask: true,
					sync: true,
					url: 'data/getRelatedRecord.php',
					params: { action: 'student', student_id: encoded_array },
					success: function (resp) {
						var str = "";
						resbool = resp.responseText;
						if (resbool != "") {
							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.?', deleteStudents);
						}
					}
				});
			}

			function deleteStudents(btn) {
				if (btn == 'yes') {
					loginWindow.show();
				}
			}

			function formatDate(value) {
			if(value!=null){
				var temp = value.dateFormat('d/m/Y');
				if (temp == "28/02/1900") {
					return '';
				}
				else {
					return temp;
				}
			}
			}
			var sec;
			store = new Ext.data.JsonStore({
				autoDestroy: true,
				url: 'data/student-grid-filter.php',
				remoteSort: true,
				storeId: 'myStore',
				idProperty: 'student_id',
				root: 'data',
				totalProperty: 'total',
				fields: [{
					name: 'student_id',
					type: 'int'
				}, {
					name: 'student_gturegno'
				},
				{
					name: 'year_name'
				}, {
					name: 'branchyear_name'
				}, {
					name: 'semester_name'
				}, {
					name: 'studentname'
				}, {
					name: 'student_gender'
				}, {
					name: 'student_admdate',
					type: 'date',
					dateFormat: 'Y-m-d'
				}, {
					name: 'student_birthdate',
					type: 'date',
					dateFormat: 'Y-m-d'
				}, {
					name: 'student_category'
				},
				{
					name: 'student_admby'
				},
				{
					name: 'student_mobileother'
				},
				{
					name: 'student_fathermobile'
				}
				]
			});
			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
			});
			var BranchCombo = {
				xtype: 'combo',
				store: branchstore,
				id: 'branch_id',
				displayField: 'branch_name',
				valueField: 'branch_id',
				editable: false,
				mode: 'local',
				forceSelection: true,
				triggerAction: 'all',
				//emptyText: 'Select a Branch...',
				selectOnFocus: true,
				width: 220,
				listeners: {
					'select': function (cmb, rec, idx) {
						sec = this.getValue(),
							Ext.getCmp('semester_id').reset();
						Ext.getCmp('branchyear_id').reset();
						Ext.getCmp('year_id').reset();
						batchyearstore.filter('branch_id', cmb.getValue(), false, true, true);
						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();
							}
						store.load({
							params: {
								'branch_id': this.getValue(),
								start: 0, limit: 10
							}
						});
					}

				}
			}
			var BatchyearCombo = {
				xtype: 'combo',
				store: batchyearstore,
				id: 'year_id',
				displayField: 'year_name',
				valueField: 'year_id',
				editable: false,
				mode: 'local',
				forceSelection: true,
				triggerAction: 'all',
				//emptyText: 'Select a Branch...',
				selectOnFocus: true,
				width: 100,
				listeners: {
					'select': function (cmb, rec, idx) {
						if (Ext.getCmp('branch_id').getValue() != "") {

							store.load({
								params:
								{
									'branch_id': Ext.getCmp('branch_id').getValue(),
									'year_id': this.getValue(),
									start: 0, limit: 10
								}
							}
							);
						} else {
							alert('Select Course First')
							this.reset();
						}

					}
				}
			}

			var YearCombo = {
				xtype: 'combo',
				name: 'branchyear_id',
				id: 'branchyear_id',
				forceSelection: true,
				displayField: 'branchyear_name',
				valueField: 'branchyear_id',
				lastQuery: '',
				allowBlank: false,
				triggerAction: 'all',
				mode: 'local',
				store: yearstore,
				width: 100,
				selectOnFocus: true,
				listeners: {
					'select': function (cmb, rec, idx) {
						if (Ext.getCmp('branch_id').getValue() != "") {
							store.load({
								params:
								{
									'branch_id': Ext.getCmp('branch_id').getValue(),
									'branchyear_id': this.getValue(),
									start: 0, limit: 10
								}
							}
							);
						} else {
							alert('Select Course First')
							this.reset();
						}

					}
				}
			}
			var SemesterCombo = {
				xtype: 'combo',
				store: semesterstore,
				id: 'semester_id',
				displayField: 'semester_name',
				valueField: 'semester_id',
				editable: false,
				mode: 'local',
				lastQuery: '',
				disabled: true,
				forceSelection: true,
				triggerAction: 'all',
				//emptyText: 'Select a Smester...',
				selectOnFocus: true,
				width: 100,
				listeners: {
					'select': function (cmb, rec, idx) {
						if (Ext.getCmp('branch_id').getValue() != "") {
							store.load({
								params: {
									'branch_id': Ext.getCmp('branch_id').getValue(),
									'semester_id': this.getValue(),
									start: 0, limit: 10
								}
							});
						} else {
							alert('Select Branch First')
							this.reset();
						}

					}
				}
			}

			var filters = new Ext.ux.grid.GridFilters({
				encode: encode,
				local: false,
				filters: [{
					type: 'numeric',
					dataIndex: 'student_id'
				}, {
					type: 'string',
					dataIndex: 'student_gturegno'
				}, {
					type: 'string',
					dataIndex: 'year_name',
					labelField: 'year_name'
				}, {
					type: 'string',
					dataIndex: 'branchyear_name',
					labelField: 'branchyear_name'
				}, {
					type: 'string',
					dataIndex: 'semester_name',
					labelField: 'semester_name'
				},
				{
					type: 'string',
					dataIndex: 'studentname',
					phpMode: true
				}, {
					type: 'list',
					dataIndex: 'student_active',
					options: ['Yes', 'No'],
					phpMode: true
				}, {
					type: 'list',
					dataIndex: 'student_gender',
					options: ['Male', 'Female'],
					phpMode: true
				}, {
					type: 'list',
					dataIndex: 'student_category',
					options: [{ id: 'GENERAL', text: 'GENERAL' },
					{ id: 'OBC', text: 'OBC' },
					{ id: 'SC', text: 'SC' },
					{ id: 'ST', text: 'ST' },
					{ id: 'MINORITY', text: 'MINORITY' },
					{ id: 'VICHARIT VIMUKTI', text: 'VICHARIT VIMUKTI' },
					{ id: 'EWS', text: 'EWS' }],

					phpMode: true
				}, {
					type: 'date',
					dataIndex: 'student_admdate'
				}
					, {
					type: 'date',
					dataIndex: 'student_birthdate'
				}
					,
				{
					type: 'list',
					dataIndex: 'student_admby',
					options: ['ACPC', 'MQ', 'VACENT QUOTA', 'NRI', 'Other'],
					phpMode: true
				}]
			});

			var createColModel = function (finish, start) {
				var columns = [
					sm2,
					{
						header: "Edit",
						id: 'btnEdit',
						width: 30,
						renderer: function (value, metaData, record, rowIndex, colIndex, store) {
							return '<a href="addedit-student.php?ID=' + record.get('student_id') + '">Edit</a>';
						}
					},
					{
						dataIndex: 'student_id',
						header: 'Student ID',
						width: 80,
						hidden: true,
						filterable: true
					}, {
						dataIndex: 'student_gturegno',
						header: 'Enrollment No',
						width: 90,
						filterable: true
					},
					{
						dataIndex: 'year_name',
						header: 'Batch Year',
						width: 40
					}, {
						dataIndex: 'branchyear_name',
						header: 'Course Year',
						width: 40
					}, {
						dataIndex: 'semester_name',
						header: 'Semester',
						width: 60
					}, {
						dataIndex: 'studentname',
						header: 'Student Name',
						id: 'SurName',
						filter: { type: 'string' },
						width: 210
					}, {
						dataIndex: 'student_admdate',
						header: 'Admis. Date',
						width: 70,
						renderer: formatDate,
						filter: {
						}
					},
					{
						dataIndex: 'student_birthdate',
						header: 'Birth Date',
						width: 70,
						renderer: formatDate,
						filter: {
						}
					}, {
						dataIndex: 'student_gender',
						header: 'Gen',
						id: 'Gender',
						width: 40
					}, {
						dataIndex: 'student_category',
						header: 'Category',
						width: 65,
						filter: {
						}
					}, {
						dataIndex: 'student_admby',
						header: 'Adm By',
						width: 50,
						filter: {
						}
					}, {
						dataIndex: 'student_mobileother',
						header: 'Mob.Student',
						width: 80,
						filter: {
						}
					}, {
						dataIndex: 'student_fathermobile',
						header: 'Mob.Parents',
						width: 80,
						filter: {
						}
					}];

				return new Ext.grid.ColumnModel({
					columns: columns.slice(start || 0, finish),
					defaults: {
						sortable: true
					}
				});
			};
			var grid = new Ext.grid.GridPanel({
				id: 'grid',
				name: 'grid',
				store: store,
				colModel: createColModel(40),
				loadMask: true,
				frame: true,
				enableColLock: false,
				columnLines: true,
				height: 460,
				width: 900,
				layout: 'fit',
				selModel: new Ext.grid.RowSelectionModel({ singleSelect: false }),
				listeners: {
					render: {
						fn: function () {
							if (Ext.getCmp('branch_id').getValue() != "") {
								//alert(Ext.getCmp('branchyear_id').getValue());
								store.reload({
									params: {
										'branch_id': Ext.getCmp('branch_id').getValue(),
										'semester_id': Ext.getCmp('semester_id').getValue(),
										'branchyear_id': Ext.getCmp('branchyear_id').getValue(),
										'year_id': Ext.getCmp('year_id').getValue(),
										start: 0,
										limit: 10
									}
								});
							}
						}
					}
				},
				plugins: [filters],
				sm: sm2,
				tbar: [{
					iconCls: 'icon-add',
					text: 'Add Student',
					id: 'btnAdd',
					handler: function () {
						location.href = 'addedit-student.php';
					}
				}, '-',
				{
					text: 'Delete Student',
					id: 'btnDelete',
					handler: confirmDeleteStudents,
					iconCls: 'remove',
					ref: '../removeButton',
					disabled: true
				}, ' | ', { xtype: 'tbtext', text: 'Course:' }, ' ', BranchCombo, { width: 3 },

				{ xtype: 'tbtext', text: 'Batch(Year) :' }, ' ', BatchyearCombo, { width: 3 },
				{ xtype: 'tbtext', text: 'Course Year:' }, ' ', YearCombo, { width: 3 },
				{ xtype: 'tbtext', text: 'Semester:' }, ' ', SemesterCombo, { width: 3 },
				],
				bbar: new Ext.PagingToolbar({
					store: store,
					pageSize: 10,
					displayInfo: true,
					plugins: [filters]
				})
			});

			grid.getBottomToolbar().add([
				'->', {
					text: 'Clear Filter Data',
					handler: function () {
						grid.filters.clearFilters();
					}
				}
			]);
			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 = "Click on Edit Link To Edit Record.";
						}
					}
				});
			});

			store.on({
				'beforeload': {
					fn: function (store, options) {
						options.params || (options.params = {});
						Ext.apply(options.params, {
							pageNo: this.pageNumber,
							branch_id: Ext.getCmp('branch_id').getValue(),
							semester_id: Ext.getCmp('semester_id').getValue(),
							branchyear_id: Ext.getCmp('branchyear_id').getValue(),
							year_id: Ext.getCmp('year_id').getValue()
						});
					},
					scope: this
				}
			});


			var StudentWindow = new Ext.Window({
				title: 'View Student Information',
				height: 415,
				width: 1000,
				layout: 'fit',
				modal: true,
				border: false,
				closeAction: 'close',
				collapsible: 'true',
				items: grid
			})

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


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

			if (flgEdit == "false") {
				grid.getColumnModel().setHidden(1, true);
			}
			Ext.getCmp('branch_id').focus(true, 1000);
		});
	</script>
	<!-- end custom CSS/JS -->
	<div id="abcd" align="center"></div>
</div>
<?php require "_footer.php"; ?>