<?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" src="jquery/ux/CheckColumn.js"></script>
	<script type="text/javascript">
		var labelArray;

		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 branch = new Ext.form.ComboBox({
			name: 'branch_name',
			id: 'branch_name',
			hiddenName: 'branch_hidden',
			fieldLabel: 'Course <span class="comp"></span>',
			displayField: 'branch_name',
			valueField: 'branch_id',
			editable: true,
			triggerAction: 'all',
			tabIndex: 1,
			width: 163,
			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();
					}
				}
			}
		});

		var year = new Ext.form.ComboBox({
			name: 'year_name',
			id: 'year_name',
			hiddenName: 'year_hidden',
			fieldLabel: 'Batch Year <span class="comp"></span>',
			displayField: 'year_name',
			valueField: 'year_id',
			editable: true,
			triggerAction: 'all',
			tabIndex: 2,
			width: 163,
			mode: 'local',
			allowBlank: true,
			store: batchyearstore,
			listeners: {
				'select': function (cmb, rec, idx) {

				}
			}
		});


		var branchyear = new Ext.form.ComboBox({
			name: 'branchyear_name',
			id: 'branchyear_name',
			hiddenName: 'branchyear_hidden',
			fieldLabel: 'Course Year <span class="comp"></span>',
			displayField: 'branchyear_name',
			valueField: 'branchyear_id',
			editable: true,
			triggerAction: 'all',
			tabIndex: 3,
			width: 163,
			mode: 'local',
			allowBlank: true,
			store: yearstore,
			listeners: {
				'select': function (cmb, rec, idx) {
					var branch = Ext.getCmp('branch_name').getValue();
				}
			}
		});

		var semester = new Ext.form.ComboBox({
			name: 'semester_name',
			id: 'semester_name',
			hiddenName: 'semester_hidden',
			labelSeparator: '',
			fieldLabel: 'Semester <span class="comp"></span>',
			displayField: 'semester_name',
			valueField: 'semester_id',
			editable: true,
			tabIndex: 4,
			width: 100,
			lastQuery: '',
			triggerAction: 'all',
			mode: 'local',
			allowBlank: true,
			store: semesterstore,
			listeners: {
				'select': function (cmb, rec, idx) {
					var branch = Ext.getCmp('branch_name').getValue();
				}
			}
		});




		/* START EDITABLE GRID */

		store = new Ext.data.JsonStore({
			url: 'data/getstudent-unamepass.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: 'user_name',
				type: 'string'
			}, {
				name: 'user_password',
				type: 'string'
			}]
		});

		var grid = new Ext.grid.EditorGridPanel({
			store: store,
			clicksToEdit: 1,
			columns: [
				new Ext.grid.RowNumberer(),
				{
					dataIndex: 'student_id',
					header: 'Student ID',
					id: 'student_id',
					hidden: true
				}, {
					dataIndex: 'student_name',
					header: 'Student Name',
					sortable: true,
					id: 'student_name',
					width: 200
				}, {
					dataIndex: 'student_gturegno',
					header: 'Enrollment No',
					sortable: true,
					id: 'student_gturegno',
					width: 125
				}, {
					dataIndex: 'user_name',
					header: 'UserName',
					sortable: true,
					id: 'user_name',
					width: 100,
					editor: {
						xtype: 'textfield',
						style: 'text-align:left',
						allowBlank: true,
						autoCreate: { tag: 'input', type: 'text', maxlength: '50' }
					}
				}, {
					dataIndex: 'user_password',
					header: 'Password',
					sortable: true,
					id: 'user_password',
					width: 100,
					editor: {
						xtype: 'textfield',
						style: 'text-align:left',
						allowBlank: true,
						autoCreate: { tag: 'input', type: 'text', maxlength: '50' }
					}
				}
			],
			height: 320,
			width: 580,
			layout: 'fit',
			sm: new Ext.grid.RowSelectionModel({
				singleSelect: true
			}),
			loadMask: true,
			columnLines: true,
			stripeRows: 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 = "Click on Rollno Column To Edit Rollno.";
					}
				}
			});
		});

		store.on({
			'load': {
				fn: function () {
					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
						});
					} else {
						grid.startEditing(0, 3);
					}
				}
			}
		});



		/* END EDITABLE GRID */




		Ext.onReady(function () {
			Ext.QuickTips.init();



			var studentpassForm = new Ext.FormPanel({
				id: 'studentpassForm',
				labelWidth: 65,
				url: 'save-studentpassword.php',
				frame: true,
				title: '',
				bodyStyle: 'padding:6px 6px',
				layout: 'form',
				defaultType: 'textfield',
				items: [{
					xtype: 'container',
					autoEl: {},
					border: false,
					layout: 'column',
					anchor: '100%',
					defaultType: 'field',
					items: [{
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						labelWidth: 50,
						columnWidth: .4,
						items: [branch]
					},
					{
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						columnWidth: .4,
						items: [year]
					},
					{
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						columnWidth: .4,
						items: [branchyear]
					}, {
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						columnWidth: .4,
						items: [semester]
					}, {
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						columnWidth: .2,
						items: [{
							xtype: 'button',
							text: 'Auto Generate',
							iconCls: 'icon-view',
							handler: function () {
								var branch_name = Ext.getCmp('branch_name').getValue();
								var semester_name = Ext.getCmp('semester_name').getValue();
								var year_name = Ext.getCmp('year_name').getValue();
								var branchyear_name = Ext.getCmp('branchyear_name').getValue();
								if (branch_name != '' && year_name!='' &&semester_name != '') {
									store.load({
										params: {
											branch_name: branch_name,
											year_name: year_name,
											semester_name: semester_name,
											autogenerate: 1
										}
									});

								}
								else if (branch_name != '' && year_name!='' &&branchyear_name != '') {
									store.load({
										params: {
											branch_name: branch_name,
											year_name: year_name,
											branchyear_name: branchyear_name,
											autogenerate: 1
										}
									});

								}
								else {
									alert("Select branch first!");
									store.removeAll();
								}


							}

						}]
					}, {
						xtype: 'container',
						autoEl: {},
						layout: 'form',
						style: { padding: '5px 0px 0px 0px' },
						columnWidth: 1,
						items: [grid]
					}]
				}]
			});

			var StudentpassWindow = new Ext.Window({
				title: 'Student Username Entry',
				height: 455,
				width: 620,
				modal: true,
				layout: 'fit',
				border: false,
				closeAction: 'close',
				collapsible: 'true',
				items: studentpassForm,
				buttonAlign: 'center',
				buttons: [{
					text: 'Save',
					iconCls: 'icon-save',
					name: 'btnSave',
					tabIndex: 5,
					id: 'btnSave',
					handler: function () {
						grid.stopEditing();
						if (grid.store.getCount() >= 1) {
							var arr_studentid = " ";
							var arr_username = " ";
							var student_name = " ";
							var arr_password = " ";
							var at = "";
							for (var i = 0, len = grid.store.getCount(); i < len; i++) {
								record = grid.store.getAt(i);
								student_id = record.data.student_id;
								username = record.data.user_name;
								password = record.data.user_password;

								arr_studentid += student_id + ",";
								arr_username += username + ",";
								arr_password += password + ",";


							}

							studentpassForm.getForm().submit({
								params: { student_id: arr_studentid, username: arr_username, password: arr_password },
								waitMsg: 'Saving...',
								success: function () {
									Ext.MessageBox.show({
										title: 'Message',
										msg: 'Students username and password has been generated!',
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.INFO
									});
									studentpassForm.getForm().reset();
									store.removeAll();
								},
								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 enter valid form no.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						}
					}
				}, {
					text: 'Reset',
					name: 'btnreset',
					iconCls: 'icon-reset',
					tabIndex: 7,
					id: 'btnreset',
					handler: function () {
						studentpassForm.getForm().reset();
						grid.getSelectionModel().clearSelections();
						//Ext.getCmp('btnDelete').disable();
						Ext.getCmp('branch_name').focus(true, 500);
						store.removeAll();
						if (flgAdd == "true") {
							Ext.getCmp('btnSave').enable();
						}
					}
				}, {
					text: 'Close',
					name: 'btnclose',
					tabIndex: 8,
					id: 'btnclose',
					iconCls: 'icon-close',
					handler: function () {
						StudentpassWindow.close();
					}
				}]
			});

			var varWidth = (screen.width - StudentpassWindow.width) / 2;
			StudentpassWindow.setPosition(varWidth, ((screen.height / 4) - 85));
			StudentpassWindow.show();
			Ext.getCmp('branch_name').focus(true, 1000);
		});
	</script>
	<div id="abcd"></div>
</div>
<?php require "_footer.php"; ?>