<?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"];
?>
<!-- start custom CSS/JS -->
<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">
		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() {

				Ext.MessageBox.confirm('Confirmation', ' Are you sure Do you want to Add This Student To College.?', function (btn) {
					if (btn == 'yes') {

						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: 'backtocollege.php',
							params: { action: 'student', student_id: encoded_array },
							success: function (resp) {
								var str = "";
								resbool = resp.responseText;
								if (resbool != "") {

									Ext.MessageBox.show({
										title: 'info',
										msg: 'Student Transfer to College Successfully',
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.info
									});
									store.reload({
										params: {
											'branch_id': Ext.getCmp('branch_id').getValue(),
											start: 0,
											limit: 10
										}
									});
								}
								else {

								}
							}
						});

					}

				});

			}

			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/suspended-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: '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: 'leave_type'
				},
				{
					name: 'student_mobilestudents'
				},
				{
					name: 'student_mobileparents'
				}
				]
			});


			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);
						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: '',
				forceSelection: true,
				triggerAction: 'all',
				//emptyText: 'Select a Smester...',
				selectOnFocus: true,
				width: 100,
				'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: 'list',
					dataIndex: 'section_name',
					labelField: 'section_name',
					store: branchstore,
					phpMode: true
				}, {
					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' }],
					phpMode: true
				}, {
					type: 'date',
					dataIndex: 'student_admdate'
				}, {
					type: 'date',
					dataIndex: 'student_birthdate'
				}, {
					type: 'list',
					dataIndex: 'student_admby',
					options: ['ACPDC', 'M.Q', 'V.Q'],
					phpMode: true
				}, {
					type: 'list',
					dataIndex: 'leave_type',
					options: [{ id: 's', text: 'Suspended' },
					{ id: 'r', text: 'Resufling' }],
					phpMode: true
				}]
			});

			var createColModel = function (finish, start) {
				var columns = [
					sm2,
					{
						dataIndex: 'student_id',
						header: 'Student ID',
						width: 80,
						hidden: true,
						filterable: true
					}, {
						dataIndex: 'student_gturegno',
						header: 'Enrollment No',
						width: 90,
						filterable: true
					}, {
						type: 'string',
						header: 'Batch Year',
						dataIndex: 'year_name'
					}, {
						type: 'string',
						header: 'Course Year',
						dataIndex: 'branchyear_name'

					}, {
						dataIndex: 'semester_name',
						header: 'Semester',
						width: 60
					}, {
						dataIndex: 'studentname',
						header: 'Student Name',
						id: 'SurName',
						filter: { type: 'string' },
						width: 190
					}, {
						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_mobilestudents',
						header: 'Mob.Student',
						width: 80,
						hidden: true,
						filter: {
						}
					}, {
						dataIndex: 'student_mobileparents',
						header: 'Mob.Parents',
						width: 80,
						hidden: true,
						filter: {
						}
					}, {
						dataIndex: 'leave_type',
						header: 'Leave Type',
						width: 80,
						renderer: function (v, params, record) {
							if (v == 'r') {
								return 'Resufling';
							}
							else if (v == 's') {
								return 'Suspended';

							} else {
								return '';

							}
						}
					}];


				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() != "") {
								store.reload({
									params: {
										'branch_id': Ext.getCmp('branch_id').getValue(),
										'semester_id': Ext.getCmp('semester_id').getValue(),
										start: 0,
										limit: 10
									}
								});
							}
						}
					}
				},
				plugins: [filters],
				sm: sm2,
				tbar: [{ 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 },
				{
					text: 'Back To College',
					id: 'btnDelete',
					handler: confirmDeleteStudents,
					ref: '../removeButton',
					disabled: true
				}
				],
				bbar: new Ext.PagingToolbar({
					store: store,
					pageSize: 10,
					displayInfo: true,
					plugins: [filters]
				})
			});
			grid.getBottomToolbar().add([
				'->', {
					text: 'Clear Filter Data',
					handler: function () {
						grid.filters.clearFilters();
					}
				}
			]);


			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()
						});
					},
					scope: this
				}
			});


			var StudentWindow = new Ext.Window({
				title: 'Suspended OR Resufling Student List',
				height: 415,
				width: 900,
				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 (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"; ?>