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

?>
<div id="main-content">
	<script type="text/javascript">


		var labelArray;
		labelArray = [
			['Course<span class="comp">*</span> '],
			['Batch Year<span class="comp">*</span> '],
			['Course Year<span class="comp">*</span> '],
			['Semester<span class="comp">*</span> '],
			['Lecture '],
			['Start Time '],
			['End Time ']
		];


		var Lecture_Fields = [
			{ name: 'lecture_id', type: 'int' },
			{ name: 'lecture_name' }
		]

		if (flgDelete == "true") flgDelete1 = true;
		else flgDelete1 = false;
		if (flgAdd == "true") flgAdd1 = true;
		else flgAdd1 = false;
		var flgalert = 0;


		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 TimeStore = new Ext.data.ArrayStore({
			fields: ['time_id', 'time_start_name'],
			data: Ext.student.time
		});
		var TimeStore1 = new Ext.data.ArrayStore({
			fields: ['time_id', 'time_end_name'],
			data: Ext.student.time1
		});

		var Lecture_Fields1 = [
			{ name: 'lecture_id', type: 'int' },
			{ name: 'lecture_name', type: 'string' }
		]
		var LectureStore1 = new Ext.data.JsonStore({
			url: 'data/lecture-grid-data.php',
			root: 'data',
			fields: Lecture_Fields1,
			autoLoad: true
		});


		var Lecture_Fields = [
			{ name: 'semester_allocate_time_id', type: 'int' },
			{ name: 'lecture_id', type: 'int' },
			{ name: 'time_start_name', type: 'string' },
			{ name: 'time_end_name', type: 'string' },
			{ name: 'branch_id', type: 'int' },
			{ name: 'branch_name', type: 'string' },
			{ name: 'year_id', type: 'int' },
			{ name: 'year_name', type: 'string' },
			{ name: 'branchyear_id', type: 'int' },
			{ name: 'branchyear_name', type: 'string' },
			{ name: 'semester_id', type: 'int' },
			{ name: 'semester_name', type: 'string' },
			{ name: 'lec_name', type: 'string' },
			
		]
		var LectureStore = new Ext.data.JsonStore({
			url: 'data/smester-allocate-grid-data.php',
			root: 'data',
			fields: Lecture_Fields,
			autoLoad: true
		});


		Ext.onReady(function () {
			Ext.QuickTips.init();
			var colModel = new Ext.grid.ColumnModel([
				{
					header: "Course",
					width: 80,
					dataIndex: 'branch_name',
					sortable: true
				},
				{
					header: "Batch Year",
					width: 80,
					dataIndex: 'year_name',
					sortable: true
				},
				{
					header: "Course Year",
					width: 80,
					dataIndex: 'branchyear_name',
					sortable: true
				},
				{
					header: "Semester",
					width: 60,
					dataIndex: 'semester_name',
					sortable: true
				},
				
				{
					header: "Lecture",
					width: 60,
					dataIndex: 'lec_name',
					sortable: true
				},
				{
					header: "Start Time",
					width: 60,
					dataIndex: 'time_start_name',
					sortable: true
				},
				{
					header: "End Time",
					width: 60,
					dataIndex: 'time_end_name',
					sortable: true
				}

			]);


			var grid = new Ext.grid.GridPanel({
				store: LectureStore,
				cm: colModel,
				columnLines: true,
				stripeRows: true,
				loadMask: true,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp("timeallocateform").getForm().loadRecord(rec);
							Ext.getCmp('lecture_name').setValue(rec.data.lec_name);
							Ext.getCmp('branch_name').setValue(rec.data.branch_name);
							Ext.getCmp('year_name').setValue(rec.data.year_name);
							Ext.getCmp('branchyear_name').setValue(rec.data.branchyear_name);
							Ext.getCmp('semester_name').setValue(rec.data.semester_name);
						

							if (flgEdit == "true") {
								Ext.getCmp('btnSave').enable();
							}
							else {
								Ext.getCmp('btnSave').disable();
								alert('You do not have access to edit the record.');
							}

							if (flgDelete == "true") {
								Ext.getCmp('btnDelete').enable();
							}
						}
					}
				}),
				height: 410,
				border: true
			});

			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 = "Please click here to edit record.";
						}
					}
				});
			});

			var timeallocateform = new Ext.FormPanel({
				id: 'timeallocateform',
				frame: true,
				url: 'save-semester_allocate-time.php',
				labelAlign: 'left',
				columnWidth: .5,
				layout: 'form',
				labelWidth: 120,
				items: [
					{
							xtype: 'combo',
							name: 'branch_name',
							id: 'branch_name',
							hiddenName: 'branch_hidden',
							fieldLabel: labelArray[0],
							displayField: 'branch_name',
							valueField: 'branch_id',
							width: 200,
							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);
									var semesterCombo = Ext.getCmp('semester_name');
									if (cmb.getValue() == 3 || cmb.getValue() == 5) {
										semesterCombo.enable();
									} else {
										semesterCombo.disable();
										semesterCombo.reset();
									}
								}
							}
						},
						{
							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: 200,
							allowBlank: true,
							store: batchyearstore,
							
						}, {
							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: 200,
							allowBlank: true,
							store: yearstore,
							listeners: {
								'select': function (cmb, rec, idx) {
									//Ext.getCmp('branchyear_disp').setValue(this.getRawValue());
									if (Ext.getCmp('branch_name').getValue() != "") {

									} else {
										alert('Select Branch First')
										this.reset();
									}
								}
							}
						}, {
							xtype: 'combo',
							name: 'semester_name',
							id: 'semester_name',
							hiddenName: 'semester_hidden',
							fieldLabel: labelArray[3],
							displayField: 'semester_name',
							valueField: 'semester_id',
							lastQuery: '',
							disabled: true,
							triggerAction: 'all',
							mode: 'local',
							width: 200,
							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[4],
						hiddenName: 'hidden_lecture_id',
						name: 'lecture_name',
						id: 'lecture_name',
						tabIndex: 4,
						displayField: 'lecture_name',
						valueField: 'lecture_id',
						forceSelection: true,
						width: 200,
						triggerAction: 'all',
						mode: 'local',
						dataIndex: 'lecture_name',
						allowBlank: true,
						store: LectureStore1
					},
					{
						xtype: 'combo',
						fieldLabel: labelArray[5],
						hiddenName: 'time_start_name',
						name: 'start_time',
						id: 'start_time',
						tabIndex: 5,
						displayField: 'time_start_name',
						valueField: 'time_start_name',
						forceSelection: true,
						width: 200,
						triggerAction: 'all',
						mode: 'local',
						dataIndex: 'start_time',
						allowBlank: true,
						store: TimeStore
					},
					{
						xtype: 'combo',
						fieldLabel: labelArray[6],
						hiddenName: 'time_end_name',
						name: 'time_end_name',
						id: 'time_end_name',
						tabIndex: 6,
						displayField: 'time_end_name',
						valueField: 'time_end_name',
						forceSelection: true,
						width: 200,
						triggerAction: 'all',
						mode: 'local',
						dataIndex: 'end_time',
						allowBlank: true,
						store: TimeStore1
					},
					{
						xtype: 'textfield',
						name: 'semester_allocate_time_id',
						id: 'semester_allocate_time_id',
						value: '0',
						hidden: true
					}, { height: 10 },
					grid
				]
			});


			var LectureWindow = new Ext.Window({
				title: 'Lecture Time  Allocate ',
				width: 500,
				height: 550,
				frame: true,
				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: timeallocateform,
				buttons: [
					{
						text: 'Save',
						name: 'btnSave',
						tabIndex: 7,
						iconCls: 'icon-save',
						id: 'btnSave',
						handler: function () {


							var lecture_name = Ext.getCmp('lecture_name').getValue();
							var semester_allocate_time_id = Ext.getCmp('semester_allocate_time_id').getValue();
							if (lecture_name != '') {
								timeallocateform.getForm().submit({
									waitMsg: 'Saving...',
									success: function () {
										grid.getSelectionModel().clearSelections();
										Ext.getCmp('btnDelete').disable();
										LectureStore.load();
										timeallocateform.getForm().reset();
							
									},

								});
							}
							else {
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Please fill up complete form.',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.ERROR
								});
							}

						}
					},
					{
						text: 'Delete',
						iconCls: 'remove',
						disabled: true,
						id: 'btnDelete',
						tabIndex: 8,
						name: 'btnDelete',
						handler: function () {
							var semester_allocate_time_id = Ext.getCmp('semester_allocate_time_id').getValue();
							Ext.Ajax.request({
								loadMask: true,
								sync: true,
								url: 'data/getRelatedRecord.php',
								params: { action: 'Lecture', semester_allocate_time_id: semester_allocate_time_id },
								success: function (resp) {
									var str = "";
									resbool = resp.responseText;
									if (resbool != 0) {
										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.?', deleterecord);
									}
								}
							});
						}
					},
					{
						text: 'Reset',
						name: 'btnreset',
						tabIndex: 9,
						iconCls: 'icon-reset',
						id: 'btnreset',
						handler: function () {
							timeallocateform.getForm().reset();
							grid.getSelectionModel().clearSelections();
							Ext.getCmp('btnDelete').disable();
						
							if (flgAdd == "true") {
								Ext.getCmp('btnSave').enable();
							}
						}
					},
					{
						text: 'Close',
						name: 'btnclose',
						tabIndex: 5,
						iconCls: 'icon-close',
						id: 'btnclose',
						handler: function () {
							LectureWindow.close();
						}
					}
				]
			});

			function deleterecord(btn) {
				if (btn == 'yes') {
					timeallocateform.getForm().submit({
						waitMsg: 'Saving...',
						params: { act: 'del' },
						success: function () {
							timeallocateform.getForm().reset();
							Ext.getCmp('btnDelete').disable();
							Ext.getCmp('semester_allocate_time_id').setValue('0');
							LectureStore.load();
						
						}
					});
				}
			}

			var varWidth = (screen.width - LectureWindow.width) / 2;
			LectureWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			LectureWindow.show();
		});
	</script>
</div>

<?php require "_footer.php"; ?>