<?php
require "_header.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>'],
			['Subject Name:<span class="comp">*</span>'],
			['Subject Code:<span class="comp">*</span>'],
			['Short Name:<span class="comp">*</span>']
		];
		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 Subject_Fields = [
			{ name: 'subject_id', type: 'int' },
			{ name: 'branch_id', type: 'int' },
			{ name: 'year_id', type: 'int' },
			{ name: 'branchyear_id', type: 'int' },
			{ name: 'semester_id', type: 'int' },
			{ name: 'subject_active', type: 'int' },
			{ name: 'branch_name' },
			{ name: 'year_name' },
			{ name: 'branchyear_name' },
			{ name: 'semester_name' },
			{ name: 'subject_name' },
			{ name: 'subject_code' },
			{ name: 'short_name' },
			{ name: 'theory_hrs' },
			{ name: 'tutorial_hrs' },
			{ name: 'practical_hrs' },
			{ name: 'subject_practical' },
			{ name: 'subjecet_category' }
		]

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

		var SubjectStore = new Ext.data.JsonStore({
			url: 'data/subject-grid-data.php',
			root: 'data',
			fields: Subject_Fields,
			autoLoad: true
		});



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

		}

		Ext.onReady(function () {
			Ext.QuickTips.init();
			var colModel = new Ext.grid.ColumnModel([
				{
					header: "Course Name",
					width: 100,
					dataIndex: 'branch_name',
					sortable: true
				},
				{
					header: "Batch Year",
					width: 50,
					dataIndex: 'year_name',
					sortable: true
				},
				{
					header: "Course Year",
					width: 50,
					dataIndex: 'branchyear_name',
					sortable: true
				},
				{
					header: "Semester Name",
					width: 50,
					dataIndex: 'semester_name',
					sortable: true
				},

				{
					header: "Subject Name",
					width: 200,
					dataIndex: 'subject_name',
					sortable: true
				}, {
					header: "Subject Code",
					width: 80,
					dataIndex: 'subject_code',
					sortable: true
				}, {
					header: "Theory Hr",
					width: 40,
					dataIndex: 'theory_hrs',
					sortable: true
				}, {
					header: "Tutorial Hr",
					width: 40,
					dataIndex: 'tutorial_hrs',
					sortable: true
				}, {
					header: "Prac. Hr",
					width: 40,
					dataIndex: 'practical_hrs',
					sortable: true,
				},
				{
					header: "Active Status",
					width: 50,
					dataIndex: 'subject_active',
					sortable: true,
					renderer: function (value, metaData, record, rowIndex, colIndex, store) {
						var returnString = "";
						if (value == 1)
							returnString += "Yes";
						else
							returnString += "No";
						return returnString;
					},
				}
			]);


			var grid = new Ext.grid.GridPanel({
				store: SubjectStore,
				cm: colModel,
				columnLines: true,
				loadMask: true,
				stripeRows: true,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp("subjectform").getForm().loadRecord(rec);
							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: 285,
				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 subjectform = new Ext.FormPanel({
				id: 'subjectform',
				frame: true,
				url: 'save-subject.php',
				labelAlign: 'left',
				autoHeight: true,
				bodyStyle: 'padding:5px',
				layout: 'column',
				items: [{
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .32,
					labelWidth: 90,
					items: [
						{
							xtype: 'combo',
							name: 'branch_name',
							id: 'branch_name',
							hiddenName: 'branch_hidden',
							fieldLabel: labelArray[0],
							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);
									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: 163,
							allowBlank: true,
							store: batchyearstore,
							listeners: {
								'select': function (cmb, rec, idx) {
									//Ext.getCmp('year_disp').setValue(this.getRawValue());


								}
							}
						}, {
							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: 163,
							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: 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: 'textarea',
							labelSeparator: '',
							fieldLabel: labelArray[4],
							name: 'subject_name',
							id: 'subject_name',
							width: 163,
							tabIndex: 3,
							allowBlank: false
						}, {
							xtype: 'textfield',
							labelSeparator: '',
							fieldLabel: labelArray[5],
							name: 'subject_code',
							id: 'subject_code',
							width: 163,
							tabIndex: 4,
							allowBlank: false
						}, {
							xtype: 'textfield',
							labelSeparator: '',
							fieldLabel: labelArray[6],
							name: 'short_name',
							id: 'short_name',
							tabIndex: 5,
							width: 163,
							allowBlank: false
						}, {
							xtype: 'numberfield',
							labelSeparator: '',
							fieldLabel: 'Theory Hours:<span class="comp"></span>',
							name: 'theory_hrs',
							id: 'theory_hrs',
							tabIndex: 6,
							width: 163,
							allowBlank: false,
							value: "0",
							autoCreate: { tag: 'input', type: 'text', maxlength: '2' }
						}, {
							xtype: 'numberfield',
							labelSeparator: '',
							fieldLabel: 'Tutorial Hours:<span class="comp"></span>',
							name: 'tutorial_hrs',
							id: 'tutorial_hrs',
							tabIndex: 7,
							width: 163,
							allowBlank: false,
							value: "0",
							autoCreate: { tag: 'input', type: 'text', maxlength: '2' }
						}, {
							xtype: 'numberfield',
							labelSeparator: '',
							fieldLabel: 'Practical Hours:<span class="comp"></span>',
							name: 'practical_hrs',
							id: 'practical_hrs',
							tabIndex: 8,
							width: 163,
							value: "0",
							allowBlank: false,
							autoCreate: { tag: 'input', type: 'text', maxlength: '2' }
						}, {
							xtype: 'checkbox',
							name: 'subject_practical',
							id: 'subject_practical',
							labelSeparator: '',
							//tabIndex:9,
							//hidden:true,				
							checked: false,
							fieldLabel: 'Practical:<span class="comp">*</span>',
							allowBlank: true
						}, {
							xtype: 'checkbox',
							name: 'subject_active',
							id: 'subject_active',
							//allowBlank:false,
							//tabIndex:6,
							labelSeparator: '',
							checked: true,
							fieldLabel: 'Active:<span class="comp">*</span>'
						}, {
							xtype: 'textfield',
							name: 'subject_id',
							id: 'subject_id',
							value: '0',
							hidden: true
						}]
				}, {
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .68,
					items: [grid]
				}]
			});

			var SubjectWindow = new Ext.Window({
				title: 'Subject Master',
				width: 880,
				autoHeight: true,
				frame: true,
				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: subjectform,
				buttons: [
					{
						text: 'Save',
						name: 'btnSave',
						iconCls: 'icon-save',
						tabIndex: 9,
						id: 'btnSave',
						handler: function () {

							var branch_id = Ext.getCmp('branch_name').getValue();
							var year_id = Ext.getCmp('year_name').getValue();
							var branchyear_id = Ext.getCmp('branchyear_name').getValue();
							var semester_id = Ext.getCmp('semester_name').getValue();
							var subject_name = Ext.getCmp('subject_name').getValue();
							var subject_id = Ext.getCmp('subject_id').getValue();
							var subject_code = Ext.getCmp('subject_code').getValue();

							if (subject_name != "") {
								Ext.Ajax.request({
									loadMask: true,
									sync: true,
									url: 'data/check-duplication.php',
									params: {
										action: 'subject',
										branch_id: branch_id,
										year_id: year_id,
										branchyear_id: branchyear_id,
										semester_id: semester_id,

										subject_id: subject_id,
										subject_code: subject_code
									},
									success: function (resp) {
										resbool = Ext.decode(resp.responseText).success;
										if (resbool == 'false') {
											Ext.MessageBox.show({
												title: 'Error',
												msg: 'enter data is already exists.',
												buttons: Ext.MessageBox.OK,
												icon: Ext.MessageBox.ERROR
											});
											Ext.getCmp('subject_name').reset();
											Ext.getCmp('subject_code').reset();
											Ext.getCmp('short_name').reset();
										}
										else if (resbool == 'true') {
											subjectform.getForm().submit({
												waitMsg: 'Saving...',
												success: function () {
													grid.getSelectionModel().clearSelections();
													Ext.getCmp('btnDelete').disable();
													SubjectStore.load();

													Ext.getCmp('branch_name').reset();
													Ext.getCmp('year_name').reset();
													Ext.getCmp('branchyear_name').reset();
													Ext.getCmp('semester_name').reset();
													Ext.getCmp('subject_name').reset();
													Ext.getCmp('subject_code').reset();
													Ext.getCmp('short_name').reset();
													Ext.getCmp('theory_hrs').reset();
													Ext.getCmp('tutorial_hrs').reset();
													Ext.getCmp('practical_hrs').reset();
													Ext.getCmp('subject_name').focus(true, 500);
												},
												failure: function () {
													Ext.MessageBox.show({
														title: 'Error',
														msg: 'Please fill up complete form.',
														buttons: Ext.MessageBox.OK,
														icon: Ext.MessageBox.ERROR
													});
												}
											});
										}
									}
								});
							}
							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: 10,
						name: 'btnDelete',
						handler: function () {
							Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
						}
					},
					{
						text: 'Reset',
						tabIndex: 11,
						name: 'btnreset',
						iconCls: 'icon-reset',
						id: 'btnreset',
						handler: function () {
							subjectform.getForm().reset();
							SubjectStore.load();
							Ext.getCmp('btnDelete').disable();

							if (flgAdd == "true") {
								Ext.getCmp('btnSave').enable();
							}
						}
					},
					{
						text: 'Close',
						name: 'btnclose',
						tabIndex: 12,
						iconCls: 'icon-close',
						id: 'btnclose',
						handler: function () {
							SubjectWindow.close();
						}
					}
				]
			});

			function deleterecord(btn) {
				if (btn == 'yes') {
					subjectform.getForm().submit({
						waitMsg: 'Saving...',
						params: { act: 'del' },
						success: function () {
							Ext.getCmp('branch_name').reset();
							Ext.getCmp('year_name').reset();
							Ext.getCmp('branchyear_name').reset();
							Ext.getCmp('semester_name').reset();
							Ext.getCmp('subject_name').reset();

							Ext.getCmp('subject_code').reset();
							Ext.getCmp('short_name').reset();
							Ext.getCmp('btnDelete').disable();
							Ext.getCmp('subject_id').setValue('0');
							SubjectStore.load();
							Ext.getCmp('subject_name').focus(true, 500);
						}
					});
				}
			}

			var varWidth = (screen.width - SubjectWindow.width) / 2;
			SubjectWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			SubjectWindow.show();


			if (flgAdd == "true") {
				Ext.getCmp('btnSave').enable();
			}
			else {
				Ext.getCmp('btnSave').disable();
			}

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