<?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>
	<link rel="stylesheet" type="text/css" href="css/Ext.css">
	<script type="text/javascript" src="js/Ext.js"></script>
	<script type="text/javascript" src="js/Ext_002.js"></script>
	<script language="javascript" type="text/javascript">
		Ext.override(Ext.ux.form.LovCombo, {
			beforeBlur: Ext.emptyFn
		});
		Ext.onReady(function () {
			Ext.QuickTips.init()
			var labelArray;
			labelArray = [
				['Day <span class="comp">*</span>'],
				['Start Time '],
				['Course <span class="comp">*</span>'],
				['Batch Year <span class="comp">*</span>'],
				['Course Year <span class="comp">*</span>'],
				['Semester <span class="comp">*</span>'],
				['Employee '],
				['Subject '],
				['Room No '],
				['Lecture ']
			];
			var abstudentSmsId = "";
			var dt = new Date();
			var xg = Ext.grid;

			var checkColumn = new Ext.grid.CheckColumn({
				header: 'Present',
				id: 'day_present',
				dataIndex: 'day_present',
				width: 55,
				editor: { xtype: 'checkbox', allowBlank: false }
			});
			
			var TimeStore = new Ext.data.ArrayStore({
				fields: ['time_id', 'time_start_name'],
				data: Ext.semester.time
			});

			var RoomStore = new Ext.data.ArrayStore({
				fields: ['room_id', 'room_name'],
				data: Ext.semester.room
			});

			var shiftstore = new Ext.data.ArrayStore({
				fields: ['shift'],
				data: [['Shift-1'], ['Shift-2']]
			});
			var daystore = new Ext.data.ArrayStore({
				fields: ['day_name'],
				data: [['Monday'], ['Tuesday'], ['Wednesday'], ['Thursday'], ['Friday'], ['Saturday']]
			});

			var employeestore = new Ext.data.ArrayStore({
				fields: ['employee_id', 'employee_name', 'branch_name'],
				data: Ext.semester.employee
			});

			/*var lectypestore = new Ext.data.ArrayStore({
				fields: ['lectype_id', 'lectype_name'],
				data: Ext.semester.lectype
			});*/

			var LectureStore = new Ext.data.ArrayStore({
				fields: ['lec_id', 'lec_name'],
				data: Ext.semester.lec
			});

			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 subjectstore = new Ext.data.JsonStore({
				url:'data/get-subject.php',
   			 	root: 'data',
				totalProperty: 'total',
				fields: ['subject_id','subject_name'] 
				});
				subjectstore.load({params:{task:'getall'}});

			var yearstore = new Ext.data.ArrayStore({
				fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
				data: Ext.year.year
			});

		
			store = new Ext.data.JsonStore({
				url: 'data/grid_subject_allocation1.php',
				root: 'data',
				storeId: 'student_id',
				totalProperty: 'total',
				fields: [{ name: 'work_allotment_subject_id' }, 
				{ name: 'college_id' }, 
				{ name: 'branch_id' },
				{ name: 'semester_id' }, 
				{ name: 'year_id' },
				{ name: 'branchyear_id' }, 
				{ name: 'subject_id' }, 
				{ name: 'day_name' },  
			
				{ name: 'time_name' },
				{ name: 'employee_id' }, 
				{ name: 'year_name' },
				{ name: 'branch_name' }, 
				{ name: 'branchyear_name' },
				{ name: 'semester_name' }, 
				{ name: 'subject_name' }, 
				{ name: 'employee_name' }, 
				{ name: 'room_id' },
				{ name: 'time_id' }, 
				{ name: 'room_name' }, 
				{ name: 'lec_id' },
				{ name: 'lec_name' }
				]
			});

			var colModel = new Ext.grid.ColumnModel([
				new Ext.grid.RowNumberer(),
				{
					dataIndex: 'work_allotment_subject_id',
					header: 'Subject Allocation ID',
					id: 'work_allotment_subject_id',
					hidden: true
				}, {
					dataIndex: 'branch_name',
					header: 'Course',
					sortable: true,
					width: 80
				},
				{
					dataIndex: 'year_name',
					header: 'Batch Year',
					sortable: true,
					width: 80
				},
				{
					dataIndex: 'branchyear_name',
					header: 'Course Year',
					sortable: true,
					width: 80
				}, {
					dataIndex: 'semester_name',
					header: 'Semester',
					sortable: true,
					width: 70,
				},  {
					dataIndex: 'day_name',
					header: 'Day',
					sortable: true,
					width: 70
				},
				{
					dataIndex: 'room_name',
					header: 'Room',
					sortable: true,
					width: 70
				}
				, {
					dataIndex: 'time_name',
					header: 'Time',
					sortable: true,
					width: 80
				},
				{
					dataIndex: 'lec_name',
					header: 'Lecture',
					sortable: true,
					width: 80
				}
				, {
					dataIndex: 'subject_name',
					header: 'Subject',
					sortable: true,
					width: 160
				}, {
					dataIndex: 'employee_name',
					header: 'Faculty Name',
					sortable: true,
					width: 160
				}]);

			store.load();

			var grid = new Ext.grid.GridPanel({
				xtype: 'grid',
				ds: store,
				cm: colModel,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp('btnDelete').enable();
							Ext.getCmp("PresentForm").getForm().loadRecord(rec);
							Ext.getCmp('branch_name').setRawValue(rec.data.branch_name);
							Ext.getCmp('branch_name').setValue(rec.data.branch_id);
							Ext.getCmp('year_name').setRawValue(rec.data.year_name);
							Ext.getCmp('year_name').setValue(rec.data.year_id);
							Ext.getCmp('branchyear_name').setRawValue(rec.data.branchyear_name);
							Ext.getCmp('branchyear_name').setValue(rec.data.branchyear_id);
							Ext.getCmp('semester_name').setRawValue(rec.data.semester_name);
							Ext.getCmp('semester_name').setValue(rec.data.semester_id);
							Ext.getCmp('subject_name').setValue(rec.data.subject_id);
							Ext.getCmp('subject_name').setRawValue(rec.data.subject_name);
							Ext.getCmp('room_id').setValue(rec.data.room_id);
							//Ext.getCmp('room_name').setValue(rec.data.room_name);
							Ext.getCmp('time_id').setValue(rec.data.time_id);
							Ext.getCmp('lec_id').setValue(rec.data.lec_id);
							
						}
					}
				}),
				height: 370,
				width: 890,
				border: true,
				loadMask: true
			});

			grid.on('render', function () {
				grid.tip = new Ext.ToolTip({
					view: grid.getView(),
					target: grid.getView().mainBody,
					delegate: '.x-grid3-row',
					trackMouse: true,
					loadMask: true,
					renderTo: document.body,
					listeners: {
						beforeshow: function updateTipBody(tip) {
							tip.body.dom.innerHTML = "Please Click Here To Edit Records.";
						}
					}
				});
			});


			Ext.override(Ext.Editor, {
				onBlur: function () {
					if (this.allowBlur === true && this.editing) {
						this.completeEdit();
					}
				}
			});

			var PresentForm = new Ext.FormPanel({
				method: 'post',
				frame: true,
				id: 'PresentForm',
				name: 'PresentForm',
				url: 'save-work-subject-allocation.php',
				standardSubmit: false,
				layout: 'column',
				items: [{
					columnWidth: 0.33,
					layout: 'form',
					labelWidth: 75,
					items: [{
						xtype: 'combo',
						name: 'day_name',
						id: 'day_name',
						fieldLabel: labelArray[0],
						forceSelection: true,
						displayField: 'day_name',
						valueField: 'day_name',
						lastQuery: ''
						, allowBlank: true
						, triggerAction: 'all'
						, mode: 'local'
						, store: daystore
						, width: 163
						, selectOnFocus: true,
						tabIndex: 1,

					},
					{
						xtype: 'combo',
						name: 'year_name',
						id: 'year_name',
						hiddenName: 'year_hidden',
						fieldLabel: labelArray[3],
						displayField: 'year_name',
						valueField: 'year_id',
						lastQuery: '',
						triggerAction: 'all',
						mode: 'local',
						width: 163,
						allowBlank: true,
						store: batchyearstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								//Ext.getCmp('year_disp').setValue(this.getRawValue());


							}
						}
					},
					/*{
						xtype: 'combo',
						name: 'lectype',
						id: 'lectype',
						hiddenName: 'lectype_hid',
						fieldLabel: labelArray[6],
						displayField: 'lectype_name',
						valueField: 'lectype_id',
						editable: true,
						triggerAction: 'all',
						mode: 'local',
						tabIndex: 8,
						width: 163,
						allowBlank: true,
						store: lectypestore

					},*/
					{
						xtype: 'combo',
						name: 'employee_id',
						id: 'employee_id',
						fieldLabel: labelArray[6],
						forceSelection: true,
						displayField: 'employee_name',
						valueField: 'employee_id',
						hiddenName: 'employee_id_hid'
						, lastQuery: ''
						, allowBlank: true
						, triggerAction: 'all'
						, mode: 'local'
						, store: employeestore
						, width: 163
						, selectOnFocus: true,
						tabIndex: 6,
						listeners: {
							'select': function (cmb, rec, idx) {

								if (Ext.getCmp('branch_name').getValue() != "") {

								} else {
									alert('Select Branch First')
									this.reset();
								}
							}
						}
					},

					{
						xtype: 'combo',
						fieldLabel: labelArray[9],
						hiddenName: 'hidden_lecture_id',
						name: 'lec_id',
						id: 'lec_id',
						tabIndex: 10,
						displayField: 'lec_name',
						valueField: 'lec_id',
						forceSelection: true,
						width: 163,
						//	readOnly:true,
						triggerAction: 'all',
						mode: 'local',
						lastQuery: '',
						dataIndex: 'lec_name',
						allowBlank: false,
						store: LectureStore
					},

					{
						xtype: 'hidden',
						id: 'work_allotment_subject_id',
						name: 'work_allotment_subject_id'


					}]
				}, {
					columnWidth: 0.34,
					layout: 'form',
					labelWidth: 85,
					items: [{
						xtype: 'combo',
						fieldLabel: labelArray[1],
						hiddenName: 'time_id',
						name: 'time_id',
						id: 'time_id',
						tabIndex: 2,
						displayField: 'time_start_name',
						valueField: 'time_id',
						forceSelection: true,
						width: 163,
						triggerAction: 'all',
						mode: 'local',
						lastQuery: '',
						dataIndex: 'start_time',
						allowBlank: true,
						store: TimeStore
					}, {
						xtype: 'combo',
						name: 'branchyear_name',
						id: 'branchyear_name',
						hiddenName: 'branchyear_hidden',
						fieldLabel: labelArray[4],
						displayField: 'branchyear_name',
						valueField: 'branchyear_id',
						lastQuery: '',
						triggerAction: 'all',
						mode: 'local',
						width: 163,
						allowBlank: true,
						store: yearstore,
						listeners: {
							'select': function (cmb, rec, idx) {

								if (Ext.getCmp('branch_name').getValue() != "") {

								} else {
									alert('Select Branch First')
									this.reset();
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'subject_name',
						id: 'subject_name',
						hiddenName: 'subject_hidden',
						fieldLabel: labelArray[7],
						displayField:'subject_name',
						valueField: 'subject_id',
						triggerAction: 'all',
						mode: 'local',
						tabIndex: 7,
						width: 163,
						allowBlank: true,
						store:subjectstore,
						listeners: {
						'select': function (cmb, rec, idx) {
							if (Ext.getCmp('branch_name').getValue() != "") {
							
							} else {
								alert('Select Branch First')
								this.reset();
							}
						}
					}
					}]
				},
				{
					columnWidth: 0.33,
					layout: 'form',
					labelWidth: 75,
					items: [{
						xtype: 'combo',
						name: 'branch_name',
						id: 'branch_name',
						hiddenName: 'branch_hidden',
						fieldLabel: labelArray[2],
						displayField: 'branch_name',
						valueField: 'branch_id',
						width: 163,
						triggerAction: 'all',
						mode: 'local',
						allowBlank: true,
						store: branchstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								Ext.getCmp('semester_name').reset();
								Ext.getCmp('year_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);
						//		employeestore.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: 'semester_name',
						id: 'semester_name',
						hiddenName: 'semester_hidden',
						fieldLabel: labelArray[5],
						displayField: 'semester_name',
						valueField: 'semester_id',
						lastQuery: '',
						disabled: true,
						triggerAction: 'all',
						mode: 'local',
						width: 163,
						allowBlank: true,
						store: semesterstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								//	Ext.getCmp('semester_disp').setValue(this.getRawValue());
								if (Ext.getCmp('branch_name').getValue() != "") {

								} else {
									alert('Select Branch First')
									this.reset();
								}
							}
						}
					}, {
						xtype: 'combo',
						fieldLabel: labelArray[8],
						hiddenName: 'hidden_room_id',
						name: 'room_id',
						id: 'room_id',
						tabIndex: 10,
						displayField: 'room_name',
						valueField: 'room_id',
						forceSelection: true,
						width: 163,
						triggerAction: 'all',
						mode: 'local',
						lastQuery: '',
						dataIndex: 'room_name',
						allowBlank: true,
						store: RoomStore
					},

					]

				}, {
					columnWidth: 1,
					layout: 'form',
					items: [{ height: 5 }, grid]
				}
				]
			});


			function deleterecord(btn) {
				if (btn == 'yes') {
					PresentForm.getForm().submit({
						waitMsg: 'Deleting...',
						params: { act: 'del' },
						success: function () {
							Ext.getCmp('btnDelete').disable();
							store.load();
							PresentForm.getForm().reset();
							Ext.getCmp('btnSave').enable();
						
						}
					});
				}
			}


			var PresentWindow = new Ext.Window({
				title: 'Employee Subject Allocation',
				width: 930,
				height: 530,
				modal: true,
				border: false,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: PresentForm,
				buttonAlign: 'center',
				buttons: [{
					xtype: 'button',
					iconCls: 'icon-save',
					text: 'Save',
					width: 70,
					tabIndex: 9,
					name: 'btnSave',
					id: 'btnSave',
					handler: function () {

						PresentForm.getForm().submit({
							waitMsg: 'Saving...',
							success: function () {
								Ext.MessageBox.show({
									title: 'Message',
									msg: 'Data has been saved',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.INFO

								});

								Ext.getCmp('btnDelete').disable();
								PresentForm.getForm().reset();
								Ext.getCmp('btnSave').enable();
								grid.getSelectionModel().clearSelections();
								Ext.getCmp('branch_name').focus(true, 1000);
								PresentForm.getForm().reset();
								store.load();

							}
						})

					}
				} <?php if ($_SESSION["VedantnrUserRole"] == "PowerUser") { ?>
						, {
						xtype: 'button',
						id: 'btnDelete',
						name: 'btnDelete',
						text: 'Delete',
						iconCls: 'remove',
						width: 70,
						tabIndex: 10,
						handler: function () {
							Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
						}
					}<?php } ?>, {
					xtype: 'button',
					iconCls: 'icon-reset',
					text: 'Reset',
					width: 70,
					tabIndex: 11,
					name: 'btnReset',
					id: 'btnReset',
					handler: function () {
						PresentForm.getForm().reset();
						Ext.getCmp('btnSave').enable();
						grid.getSelectionModel().clearSelections();
						Ext.getCmp('branch_name').focus(true, 500);
						store.load();
					}
				}, {
					xtype: 'button',
					id: 'btnClose',
					text: 'Close',
					name: 'btnClose',
					iconCls: 'icon-close',
					width: 70,
					tabIndex: 11,
					handler: function () {
						PresentWindow.close();
					}
				}]
			});

			Ext.getCmp('employee_id').on('blur', function () {
				var employee_id = Ext.getCmp('employee_id').getValue();
				var day_name = Ext.getCmp('day_name').getValue();
				var time_id = Ext.getCmp('time_id').getValue();
				if (employee_id != "") {
					Ext.Ajax.request({
						loadMask: true,
						sync: true,
						url: 'data/check-duplication.php',
						params: { action: 'timetable', employee_id: employee_id, day_name: day_name, time_id: time_id },
						success: function (resp) {
							resbool = Ext.decode(resp.responseText).success;
							if (resbool == 'false') {
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Employee Already Allocation Lecture.',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.ERROR
								});
								PresentForm.getForm().reset();
								Ext.getCmp('employee_id').reset();
							}
						}
					});
				}
			});

			Ext.getCmp('room_id').on('blur', function () {
				var time_id = Ext.getCmp('time_id').getValue();
				var day_name = Ext.getCmp('day_name').getValue();
				var room_id = Ext.getCmp('room_id').getValue();
				if (room_id != "") {
					Ext.Ajax.request({
						loadMask: true,
						sync: true,
						url: 'data/check-duplication.php',
						params: { action: 'timetable1', time_id: time_id, day_name: day_name, room_id: room_id },
						success: function (resp) {
							resbool = Ext.decode(resp.responseText).success;
							if (resbool == 'false') {
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Room  Already Allocation Same Time',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.ERROR
								});
								PresentForm.getForm().reset();
								Ext.getCmp('employee_id').reset();
							}
						}
					});
				}
			});
			PresentForm.getForm().findField('time_id').on('blur', function () {

				var time = PresentForm.getForm().findField('time_id').getValue();

				//	alert(time);

				if (time == 1) {
					Ext.getCmp('lec_id').setValue('1st Lec');
				}
				else if (time == 5) {
					Ext.getCmp('lec_id').setValue('2nd Lec');
				}
				else if (time == 7) {
					Ext.getCmp('lec_id').setValue('BREAK');
				}
				else if (time == 8) {
					Ext.getCmp('lec_id').setValue('3rd Lec');
				}
				else if (time == 9) {
					Ext.getCmp('lec_id').setValue('4th Lec');
				}
				else if (time == 10) {
					Ext.getCmp('lec_id').setValue('BREAK');
				}
				else if (time == 11) {
					Ext.getCmp('lec_id').setValue('5th Lec');
				}
				else if (time == 12) {
					Ext.getCmp('lec_id').setValue('6th Lec');
				}

			});

			var varWidth = (screen.width - PresentWindow.width) / 2;
			PresentWindow.setPosition(varWidth, ((screen.height / 4) - 170));
			PresentWindow.show();
			Ext.getCmp('branch_name').focus(true, 1000);

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