<?php
require "_header.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
require "includeGridFilters.php";
?>
<div id="main-content">
	<style type="text/css">
		label.myBold {
			font-weight: bold;
			color: #15428B;
			font-size: 10px;
		}
	</style>
	<script type="text/javascript" src="jquery/ux/CheckColumn.js"></script>
	<script language="javascript" src="js/add-tt-group.js"></script>
	<script language="javascript" type="text/javascript">
		Ext.onReady(function () {
			Ext.QuickTips.init();
			var abstudentSmsId = "";
			var dt = new Date();
			var xg = Ext.grid;

			var checkColumn = new Ext.grid.CheckColumn({
				header: 'In Batch',
				id: 'in_batch',
				dataIndex: 'in_batch',
				width: 55,
				editor: { xtype: 'checkbox', allowBlank: false }
			});
			var labelArray;
			labelArray = [
				['Course '],
				['Batch Year '],
				['Course Year '],
				['Semester '],
				['Student Name '],
				['Name of Document '],
				['No of Document '],
				['Document Recive Date ']
			];

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

			store = new Ext.data.JsonStore({
				url: 'data/getstudent-batch.php',
				root: 'data',
				storeId: 'student_id',
				totalProperty: 'total',
				fields: [{
					name: 'student_id',
					type: 'int'
				}, {
					name: 'student_name',
					type: 'string'
				}, {
					name: 'student_gturegno',
					type: 'string'
				}, {
					name: 'in_batch'
				},
				{
					name: 'shift',
				}, {
					name: 'student_mobilestudents'
				}, {
					name: 'student_mobileparents'
				}]
			});

			var colModel = new Ext.grid.ColumnModel([
				new Ext.grid.RowNumberer(),
				{
					dataIndex: 'student_id',
					header: 'Student ID',
					id: 'student_id',
					hidden: true
				}, {
					dataIndex: 'student_gturegno',
					header: 'Enrollment No',
					sortable: true,
					width: 100
				}, {
					dataIndex: 'student_name',
					header: 'Student Name',
					sortable: true,
					width: 300,
					id: 'SurName'
				}, {
					dataIndex: 'student_mobilestudents',
					header: 'Mobile Student',
					sortable: true,
					hidden: true,
					width: 90
				}, {
					dataIndex: 'student_mobileparents',
					header: 'Mobile Parents',
					sortable: true,
					width: 100,
					hidden: true,
					id: 'student_mobileparents'
				}, checkColumn]);


			var grid = new Ext.grid.GridPanel({
				store: store,
				cm: colModel,
				loadMask: true,
				enableColLock: false,
				height: 300,
				plugins: checkColumn,
				clicksToEdit: 1,
				layout: 'fit',
				columnLines: true
			});


			var PresentForm = new Ext.FormPanel({
				method: 'post',
				frame: true,
				url: 'save-student-batch.php',
				standardSubmit: false,
				layout: 'column',
				items: [{
					columnWidth: 0.5,
					layout: 'form',
					labelWidth: 75,
					items: [{
						xtype: 'combo',
						name: 'branch_name',
						id: 'branch_name',
						hiddenName: 'branch_hidden',
						fieldLabel: labelArray[0],
						displayField: 'branch_name',
						valueField: 'branch_id',
						width: 150,
						tabIndex: 1,
						triggerAction: 'all',
						mode: 'local',
						allowBlank: true,
						store: branchstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								Ext.getCmp('year_name').reset();
								Ext.getCmp('semester_name').reset();
								Ext.getCmp('branchyear_name').reset();
								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: 'branchyear_name',
						id: 'branchyear_name',
						hiddenName: 'branchyear_hidden',
						fieldLabel: labelArray[2],
						displayField: 'branchyear_name',
						valueField: 'branchyear_id',
						lastQuery: '',
						triggerAction: 'all',
						mode: 'local',
						width: 150,
						tabIndex: 3,
						allowBlank: true,
						store: yearstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								 (Ext.getCmp('branch_name').getValue() != "") 
							}
						}
					},{
						columnWidth: 1,
						layout: 'form',
						labelWidth: 75,
						items: [{
							xtype: 'container',
							autoEl: {},
							border: false,
							layout: 'column',
							anchor: '100%',
							defaultType: 'field',
							items: [{
								xtype: 'container',
								autoEl: {},
								layout: 'form',
								columnWidth: .9,
								items: [{
									xtype: 'combo',
									name: 'batch_id',
									id: 'batch_id',
									hiddenName: 'batch_idhidden',
									allowBlank: false,
									fieldLabel: 'Batch :<span class="comp">*</span>',
									forceSelection: true,
									tabIndex: 5,
									labelSeparator: '',
									displayField: 'batch_name',
									valueField: 'batch_id',
									width: 150,
									triggerAction: 'all',
									mode: 'local',
									store: BatchStore
								}]
							}, {
								xtype: 'container',
								autoEl: {},
								layout: 'form',
								columnWidth: .1,
								labelWidth: 80,
								items: [{
									xtype: 'button',
									tabIndex: 6,
									iconCls: 'icon-add',
									handler: function () {
										AddBatchWindow.show();
									}

								}]
							}]
						}]
					}]
				}, {
					columnWidth: 0.5,
					layout: 'form',
					labelWidth: 85,
					items: [{
						xtype: 'combo',
						name: 'year_name',
						id: 'year_name',
						hiddenName: 'year_hidden',
						fieldLabel: labelArray[1],
						displayField: 'year_name',
						valueField: 'year_id',
						lastQuery: '',
						triggerAction: 'all',
						mode: 'local',
						width: 150,
						tabIndex: 2,
						allowBlank: true,
						store: batchyearstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								 (Ext.getCmp('branch_name').getValue() != "") 
							}
						}
					},
					{
						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: 150,
						tabIndex: 4,
						allowBlank: true,
						store: semesterstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								if (Ext.getCmp('branch_name').getValue() != "") {
							} else {
								this.reset();
							}
							}
						}
					}, {
						columnWidth: 0.5,
						layout: 'column',
						labelWidth: 85,
						style: 'padding:5px 0px',
						items: [{
							columnWidth: 0.69,
							layout: 'form',
							style: 'padding:0px 0px 0px 90px',
							labelWidth: 85,
							items: [{
								xtype: 'button',
								text: 'View',
								iconCls: 'icon-view',
								width: 70,
								tabIndex: 7,
								handler: function () {

									var batch_id = Ext.getCmp('batch_id').getValue();
									var varyear = Ext.getCmp('year_name').getValue();
									var varbranch = Ext.getCmp('branch_name').getValue();
									var varbranchyear = Ext.getCmp('branchyear_name').getValue();
									var varsemester = Ext.getCmp('semester_name').getValue();

									if (batch_id != "" && varbranch != "" && varyear != "" ) {
										grid.stopEditing();
										store.load({ params: { 'branch_id': varbranch, 'year_id': varyear,'branchyear_id': varbranchyear,'semester_id': varsemester, 'batch_id': batch_id } });
									} 
								
									else if (batch_id != "" && varbranch != "" && varyear != "" && varbranchyear !="" && varsemester !="") {
										grid.stopEditing();
										store.load({ params: { 'branch_id': varbranch, 'year_id': varyear,'branchyear_id': varbranchyear,'semester_id': varsemester, 'batch_id': batch_id } });
									} 
									else {
										Ext.MessageBox.show({
											title: 'Error',
											msg: 'Please select all field first',
											buttons: Ext.MessageBox.OK,
											icon: Ext.MessageBox.ERROR
										});
									}
								}
							}]
						}, {
							columnWidth: 0.29,
							layout: 'form',
							labelWidth: 70,
							items: [{
								xtype: 'button',
								iconCls: 'icon-reset',
								text: 'Reset',
								width: 70,
								tabIndex: 8,
								name: 'btnReset',
								id: 'btnReset',
								handler: function () {
									store.removeAll();
									PresentForm.getForm().reset();
									Ext.getCmp('branch_name').focus(true, 500);
								}
							}]
						}]
					}]
				}, {
					columnWidth: 1,
					layout: 'form',
					items: [{ height: 5 }, grid]
				}]
			});

			store.on({
				'load': {
					fn: function (store, options) {
						if (store.getCount() <= 0) {
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'There is no Student found in this Selection',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						}
					}
				}
			});


			PresentForm.getForm().on({
				actioncomplete: function (form, action) {
					if (action.type == 'submit') {
						if (action.result.success == "true") {
							Ext.MessageBox.show({
								title: 'Message',
								msg: action.result.msg,
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.INFO
							});
							store.removeAll();
							PresentForm.getForm().reset();
							Ext.getCmp('branch_name').focus(true, 1000);
						} else if (action.result.success == "false") {
							Ext.MessageBox.show({
								title: 'Error',
								msg: action.result.msg,
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
							//store.removeAll();		 
							//PresentForm.getForm().reset();
							//Ext.getCmp('branch_name').focus(true,1000);		
						}
					}
				}
			});

			var PresentWindow = new Ext.Window({
				title: 'Student Batch Entry',
				width: 580,
				height: 470,
				modal: true,
				border: false,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: PresentForm,
				buttonAlign: 'center',
				buttons: [{
					text: 'Save',
					iconCls: 'icon-save',
					name: 'btnSave',
					tabIndex: 9,
					id: 'btnSave',
					handler: function () {
						var stcount = grid.store.getCount();
						if (stcount >= 1) {
							var InbranchStudentids = "";
							var j = 0;
							var k = 0;
							for (var i = 0, len = grid.store.getCount(); i < len; i++) {
								record = grid.store.getAt(i);
								if (record.data.in_batch == true) {
									InbranchStudentids += record.data.student_id + ",";
								}
							}

							PresentForm.getForm().submit({
								params: { InbranchStudentids: InbranchStudentids },
								waitMsg: 'Saving...',
								success: function () {
									store.removeAll();
									PresentForm.getForm().reset();
									Ext.getCmp('branch_name').focus(true, 1000);
								},
								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 click on view button to see the students record.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						}
					}
				}, {
					text: 'Close',
					tabIndex: 10,
					iconCls: 'icon-close',
					name: 'btncancle',
					id: 'btncancle',
					handler: function () {
						PresentWindow.close();
					}
				}]
			});
			var varWidth = (screen.width - PresentWindow.width) / 2;
			PresentWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			PresentWindow.show();
			Ext.getCmp('branch_name').focus(true, 1000);

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