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

require "includeGridFilters.php";
$username = $_SESSION["VedantnrUserName"];

?>
<div id="main-content">
	<script type="text/javascript">
		var username = '<?php echo $username; ?>';
	</script>
	<script src="js/login-confirm.js" type="text/javascript"></script>
	<script type="text/javascript" language="javascript">
		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 () {
			Ext.QuickTips.init();
			loadingMask.show();
			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 categorystore = new Ext.data.ArrayStore({
				fields: ['category'],
				data: [['GENERAL'], ['OBC'], ['SC'], ['ST'], ['MINORITY']]
			});
			var genderstore = new Ext.data.ArrayStore({
				fields: ['gender'],
				data: [['Male'], ['Female']]
			});
			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 caststore = new Ext.data.ArrayStore({
				fields: ['student_cast'],
				data: Ext.student.cast
			});
			var admbystore = new Ext.data.ArrayStore({
				fields: ['student_admby'],
				data: [['ACPC'], ['MQ'], ['VACENT QUOTA'], ['NRI'], ['Other']]
			});

			var categorystore = new Ext.data.ArrayStore({
				fields: ['category'],
				data: [['GENERAL'], ['VICHARIT VIMUKTI'], ['OBC'], ['SC'], ['ST'], ['MINORITY'], ['EWS']]
			});


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

			function tfwdata(value) {
				if (value == 1) {
					return '<b>Yes</b>';
				}
				else {
					return 'No';
				}

			}

			function leavedata(value) {
				//	alert(value);
				if (value == 'r') {
					return '<b><span class="comp">Reshuffling</span></b>';
				}
				else if (value == 's') {
					return '<b><span class="comp">Suspend</span></b>';
				} else if (value == 'd') {
					return '<b><span class="comp">Detain</span></b>';
				} else if (value == 'c') {
					return '<b><span class="comp">Cancel</span></b>';
				}
				else if (value == 'a') {
					return '<b><span class="comp">Alumini</span></b>';
				}
				else {
					return '<b>Active</b>';
				}

			}

			store = new Ext.data.JsonStore({
				autoDestroy: true,
				url: 'data/student-search-data.php',
				remoteSort: false,
				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: '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: 'tfw_scheme'
				},
				{
					name: 'leave_type'
				},
				{
					name: 'student_mobileparents'
				},
				{
					name: 'student_mobilestudents'
				},

				{
					name: 'year_id'
				},
				{
					name: 'year_name'
				}
				]
			});

			var createColModel = function (finish, start) {
				var columns = [
					sm2,
					new Ext.grid.RowNumberer(),
					{
						dataIndex: 'student_id',
						header: 'Student ID',
						width: 80,
						hidden: true,
						filterable: true
					}, {
						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_gturegno',
						header: 'Enrollment No',
						width: 90,
						filterable: true
					}, {
						dataIndex: 'branch_name',
						header: 'Course',
						width: 70
					}, {
						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_admdate',
						header: 'Admis. Date',
						width: 70,
						renderer: formatDate
					}, <?php if ($_SESSION["VedantnrUserRole"] != 'Faculty' && $_SESSION["VedantnrUserRole"] != 'Hod') { ?> {
							dataIndex: 'student_birthdate',
							header: 'Birth Date',
							width: 65,
							renderer: formatDate
						}, <?php } ?> {
						dataIndex: 'student_gender',
						header: 'Gen',
						id: 'Gender',
						width: 40
					}, {
						dataIndex: 'student_category',
						header: 'Category',
						width: 60
					}, {
						dataIndex: 'student_admby',
						header: 'Adm Type',
						width: 60
					}, {
						dataIndex: 'tfw_scheme',
						header: 'TFW Sch.',
						renderer: tfwdata,
						width: 60
					}, {
						dataIndex: 'leave_type',
						header: 'Leave Type',
						renderer: leavedata,
						width: 68
					}, {
						dataIndex: 'student_mobileparents',
						header: 'Par. Mo.',
						hidden: true,
						width: 80
					}, {
						dataIndex: 'student_mobilestudents',
						header: 'Stud. Mo.',
						hidden: true,
						width: 80
					}

				];

				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: false,
				enableColLock: false,
				columnLines: true,
				stripeRows: true,
				height: 370,
				width: 1030,
				layout: 'fit',
				selModel: new Ext.grid.RowSelectionModel({ singleSelect: false }),
				listeners: {
					render: {
						fn: function () {
							if (Ext.getCmp('branch_id').getValue() != "") {

								store.reload({
									params: {
										'branch_id': Ext.getCmp('branch_id').getValue(),
										'branchyear_id': Ext.getCmp('branchyear_id').getValue(),
										'semester_id': Ext.getCmp('semester_id').getValue(),
										start: 0,
										limit: 10
									}
								});
							}
						}
					}
				},
				sm: sm2,

				tbar: [
					{
						text: 'Delete Student',
						id: 'btnDelete',
						handler: confirmDeleteStudents,
						iconCls: 'remove',
						ref: '../removeButton',
						disabled: true
					}
				]

			});
			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);
						}
					}
				});
			}



			var studententry = new Ext.FormPanel({
				frame: true,
				autoHeight: true,
				url: 'report-student-search-result.php',
				frame: true,
				standardSubmit: true,
				layout: 'column',
				labelWidth: 90,
				items: [{
					columnWidth: 0.33,
					layout: 'form',
					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: 150
						, selectOnFocus: true,
						listeners: {
							'select': function (cmb, rec, idx) {
								Ext.getCmp('branch_disp').setValue(this.getRawValue());
								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: '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: 150,
						listeners: {
							'select': function (cmb, rec, idx) {
								Ext.getCmp('semester_disp').setValue(this.getRawValue());
								if (Ext.getCmp('branch_id').getValue() != "") {
									
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'student_cast',
						id: 'student_cast',
						fieldLabel: 'Cast ',
						displayField: 'student_cast',
						width: 150
						, triggerAction: 'all'
						, mode: 'local',
						store: caststore,
						editable: true

					},
					{
						xtype: 'combo',
						name: 'student_admby',
						id: 'student_admby',
						fieldLabel: 'Adm Type ',
						forceSelection: true,
						displayField: 'student_admby',
						valueField: 'student_admby',
						width: 150,
						triggerAction: 'all',
						mode: 'local',
						store: admbystore
					},]
				},
				{
					columnWidth: 0.33,
					layout: 'form',
					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: 150,
						listeners: {
							'select': function (cmb, rec, idx) {
								Ext.getCmp('year_disp').setValue(this.getRawValue());
							}}
					}, {
						xtype: 'datefield',
						name: 'student_birthdate',
						id: 'student_birthdate',
						width: 150,
						format: JSDateFormat,
						fieldLabel: 'Birth Date ',
						vtype: 'daterange'
					}, {
						xtype: 'combo',
						name: 'student_category',
						id: 'student_category',
						fieldLabel: 'Category ',
						forceSelection: true,
						displayField: 'category',
						valueField: 'category',
						width: 150,
						triggerAction: 'all',
						mode: 'local',
						store: categorystore
					}]
				},
				{
					columnWidth: 0.33,
					layout: 'form',
					items: [{
						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: 150,
						listeners: {
							'select': function (cmb, rec, idx) {
								Ext.getCmp('branchyear_disp').setValue(this.getRawValue());
								if (Ext.getCmp('branch_id').getValue() != "") {
								
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'student_gender',
						id: 'student_gender',
						fieldLabel: 'Gender ',
						forceSelection: true,
						displayField: 'gender',
						valueField: 'gender',
						width: 150,
						triggerAction: 'all',
						mode: 'local',
						store: genderstore
					}, {
						xtype: 'textfield',
						name: 'student_gturegno',
						fieldLabel: 'Enrollment No ',
						id: 'student_gturegno',
						width: 150,
						allowBlank: true,
						autoCreate: { tag: 'input', type: 'text', maxlength: '1000' }
					}, {
					xtype: 'displayfield',
					name: 'branch_disp',
                    id: 'branch_disp',
                   hidden:true,
				},
				{
					xtype: 'displayfield',
					name: 'year_disp',
                    id: 'year_disp',
                   hidden:true,
				},
				{
					xtype: 'displayfield',
					name: 'branchyear_disp',
                    id: 'branchyear_disp',
                   hidden:true,
				},
				{
					xtype: 'displayfield',
					name: 'semester_disp',
                    id: 'semester_disp',
                   hidden:true,
				}]
				},
				{
					columnWidth: 0.27,
					labelWidth: 90,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						name: 'student_surname',
						id: 'student_surname',
						fieldLabel: 'Student Name ',
						width: 160,
						emptyText: 'Surname'
					}]
				}, {
					columnWidth: 0.20,
					labelWidth: 1,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						name: 'student_firstname',
						id: 'student_firstname',
						width: 180,
						emptyText: 'First Name'
					}]
				}, {
					columnWidth: 0.19,
					labelWidth: 1,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						name: 'student_lastname',
						id: 'student_lastname',
						width: 170,
						emptyText: 'Last Name'
					}]
				},
				{
					columnWidth: 0.10,
					labelWidth: 50,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'checkbox',
						name: 'cancel',
						id: 'cancel',
						checked: false,
						fieldLabel: 'Cancel',
						allowBlank: true
					},
					]
				},
				{
					columnWidth: 0.10,
					labelWidth: 50,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'checkbox',
						name: 'detain',
						id: 'detain',
						checked: false,
						fieldLabel: 'Detain',
						allowBlank: true
					},
					]
				},
				{
					columnWidth: 0.10,
					labelWidth: 50,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'checkbox',
						name: 'suspend',
						id: 'suspend',
						checked: false,
						fieldLabel: 'Suspend',
						allowBlank: true
					},
					]
				},
				{
					columnWidth: 0.10,
					labelWidth: 50,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'checkbox',
						name: 'alumni',
						id: 'alumni',
						checked: false,
						fieldLabel: 'Alumni',
						allowBlank: true
					},
					]
				},
				{
					columnWidth: 0.10,
					labelWidth: 50,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'checkbox',
						name: 'active',
						id: 'active',
						checked: false,
						fieldLabel: 'Active',
						allowBlank: true
					},
					]
				},
				{
					columnWidth: 0.09,
					labelWidth: 60,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'checkbox',
						name: 'chkTFW',
						id: 'chkTFW',
						fieldLabel: '(TFW)Sch.',
						allowBlank: true
					}]
				}
					, {
					columnWidth: 0.07,
					labelWidth: 1,
					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();
							var cast = Ext.getCmp('student_cast').getValue();
							var birthdate = Ext.getCmp('student_birthdate').getValue();
							var gender = Ext.getCmp('student_gender').getValue();
							var gturegno = Ext.getCmp('student_gturegno').getValue();
							var surname = Ext.getCmp('student_surname').getValue();
							var firstname = Ext.getCmp('student_firstname').getValue();
							var lastname = Ext.getCmp('student_lastname').getValue();
							var chkTFW = Ext.getCmp('chkTFW').getValue();

							var cancel = Ext.getCmp('cancel').getValue();
							var detain = Ext.getCmp('detain').getValue();
							var suspend = Ext.getCmp('suspend').getValue();
							var alumni = Ext.getCmp('alumni').getValue();
							var active = Ext.getCmp('active').getValue();
							var student_admby = Ext.getCmp('student_admby').getValue();
							var student_category = Ext.getCmp('student_category').getValue();


							if (barnch != "" || semester != "" || cast != "" || birthdate != "" || gender != ""
								|| gturegno != "" || surname != "" || firstname != "" || lastname != ""
								|| chkTFW != false || cancel != false || detain != false || suspend != false || alumni != false || active != false || student_admby != "" || student_category != "") {
								store.reload({
									params: {
										barnch: barnch,
										year: year,
										branchyear: branchyear,
										semester: semester,
										cast: cast,
										birthdate: birthdate,
										gender: gender,
										gturegno: gturegno,
										surname: surname,
										firstname: firstname,
										lastname: lastname,
										chkTFW: chkTFW,

										cancel: cancel,
										detain: detain,
										suspend: suspend,
										alumni: alumni,
										active: active,
										student_admby: student_admby,
										student_category: student_category
									}
								});
							}
							else {
								alert("Select At list one Criteriya");
							}
						}
					}]
				},
				{
					columnWidth: 0.07,
					labelWidth: 1,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'button',
						text: 'Reset',
						iconCls: 'icon-reset',
						handler: function () {
							store.removeAll();
							studententry.getForm().reset();
							Ext.getCmp('branch_id').focus(true, 500);

						}
					}]
				},
				{
					columnWidth: 0.10,
					labelWidth: 1,
					layout: 'form',
					items: [{ height: 5 }, {
						xtype: 'button',
						text: 'View Report',
						iconCls: 'icon-view',
						handler: function (b, e) {

							var branch_hidden = studententry.getForm().findField('branch_hidden').getValue();
							var year_hidden = studententry.getForm().findField('year_hidden').getValue();
							var branchyear_hidden = studententry.getForm().findField('branchyear_hidden').getValue();
							var semester_hidden = studententry.getForm().findField('semester_hidden').getValue();


							var branch_disp=studententry.getForm().findField('branch_disp').getValue();
						var year_disp=studententry.getForm().findField('year_disp').getValue();
						var branchyear_disp=studententry.getForm().findField('branchyear_disp').getValue();
						var semester_disp=studententry.getForm().findField('semester_disp').getValue();

						var redirect = 'report-student-search-result.php?branch_hidden=' + branch_hidden +
							'&year_hidden=' + year_hidden + '&branchyear_hidden=' +
							branchyear_hidden + '&semester_hidden=' + semester_hidden 
							+ '&branch_disp='+branch_disp + '&year_disp='+year_disp + '&branchyear_disp='+branchyear_disp 
							+ '&semester_disp='+semester_disp ;
							window.location = redirect;
						}
					}]
				}
				]

			});

			var StudentWindow = new Ext.Window({
				title: 'Student Search',
				width: 1050,
				height: 460,
				modal: true,
				autoHeight: true,
				shadow: false,
				border: false,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: [studententry, grid],
				buttons: [{
					text: 'View Report', iconCls: 'new-tab',
					handler: function (b, e) {

						var branchyear_hidden = studententry.Ext.getCmp('branchyear_hidden').setValue(Ext.getCmp('branchyear_hidden').getRawValue());
						var year_hidden = studententry.Ext.getCmp('year_hidden').setValue(Ext.getCmp('year_hidden').getRawValue());
						var semester_hidden = studententry.Ext.getCmp('semester_hidden').setValue(Ext.getCmp('semester_hidden').getRawValue());
						var branch_hidden = studententry.Ext.getCmp('branch_hidden').setValue(Ext.getCmp('branch_hidden').getRawValue());

						var redirect = 'report-student-search-result.php?branch_hidden=' + branch_hidden +
							'&year_hidden=' + year_hidden + '&branchyear_hidden=' +
							branchyear_hidden + '&semester_hidden=' + semester_hidden;
						window.location = redirect;
					}
				},]

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

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

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