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

error_reporting(1);
?>
<style>
	.excel_help {}

	.excel_help li {
		font-size: 13px;
		color: #009;
		padding-top: 5px;
		margin-top: 5px;
		margin-bottom: 10px;
	}

	.upload {
		text-align: center;
		background-color: #09F;
		border: 0 none;
		border-radius: 10px;
		color: #ffffff;
		cursor: pointer;
		display: block;
		font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
		font-size: 16px;
		height: auto;
		margin: 0 auto;
		outline: medium none;
		padding: 8px;
		position: relative;
		text-align: center;
		text-decoration: none;
		text-transform: capitalize !important;

	}
</style>
<script language="javascript" type="text/javascript">
	Ext.onReady(function() {
		Ext.QuickTips.init();

		Ext.apply(Ext.form.VTypes, {
			fileUpload: function(val, field) {
				var fileName = /^.*\.(xls|xlsx|XLS|XLSX)$/i;
				return fileName.test(val);
			},
			fileUploadText: 'File must be in .xls,.xlsx format'
		});

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

		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.student.semester
			});

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

		
		
		var newPic = new Ext.form.FileUploadField({
			fieldLabel: 'File <span class="comp"></span>',
			name: 'enrollment_upload',
			id: 'enrollment_upload',
			allowBlank: true,
			vtype: 'fileUpload',
			buttonText: 'Select Excel File',
			tabIndex: 3,
			width: 175,
			listeners: {
				fileselected: stdUploadFileSelected
			}
		});
		

		var Recordentry = new Ext.FormPanel({
			id: 'Recordentry',
			url: 'save_upload_student_master.php',
			method: 'post',
			fileUpload: true,
			//standardSubmit:true,
			frame: true,
			autoHeight: true,
			layout: 'form',
			items: [{
					xtype: 'combo',
					name: 'branch_id',
					id: 'branch_id',
					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_id').reset();
							Ext.getCmp('year_id').reset();
							Ext.getCmp('branchyear_id').reset();

							semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
							yearstore.filter('branch_id', cmb.getValue(), false, true, true);
							var semesterCombo = Ext.getCmp('semester_id');
							if (cmb.getValue() == 3 || cmb.getValue() == 5) {
								semesterCombo.enable();
							} else {
								semesterCombo.disable();
								semesterCombo.reset();
							}
						}
					}
				}, {
					xtype: 'combo',
					name: 'year_id',
					id: 'year_id',
					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_id',
					id: 'branchyear_id',
					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) {
						
							if (Ext.getCmp('branch_id').getValue() != "") {
								Ext.getCmp('semester_id').reset();
							} else {
								alert('Select Branch First')
								this.reset();
							}
						}
					}
				}, {
					xtype: 'combo',
					name: 'semester_id',
					id: 'semester_id',
					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) {
						
							if (Ext.getCmp('branch_id').getValue() != "") {
								Ext.getCmp('branchyear_id').reset();
							} else {
								alert('Select Branch First')
								this.reset();
							}
						}
					}
				},
				newPic, {
					xtype: 'box',
					autoEl: {
						tag: 'div',
						html: '<br/><div class="excel_help"><ul><li>Select Only Excel File </li><li>Donwload data format file te check sample of excel file</li><li>Remove heade row from Excel file</li><li>Admission Date & Birthdate Should be YYYY-MM-DD Formate</li></ul><a class="upload" href="upload-format-file.xls"> Data Format File </a></div>'
					}
				}
			]
		});

		function stdUploadFileSelected(fileBrowser, fileName) {
			var ext = fileName.split('.').pop();
			if (ext != 'xls' && ext != 'xlsx' && ext != 'XLS' && ext != 'XLSX') {
				Ext.MessageBox.show({
					title: 'Error',
					msg: 'Only Excle File Allowed',
					buttons: Ext.MessageBox.OK,
					icon: Ext.MessageBox.ERROR
				});
				Ext.getCmp('enrollment_upload').reset();

			} else {



			}

		}

		var FormWindow = new Ext.Window({
			title: 'Upload Student Information',
			width: 400,
			height: 400,
		
			modal: true,
			shadow: false,
			border: false,
			closeAction: 'close',
			collapsible: true,
			items: Recordentry,
			buttonAlign: 'center',
			buttons: [{
					text: 'Upload',
					iconCls: 'icon-save',
					tabIndex: 60,
					handler: function() {
						Recordentry.getForm().submit({
							waitMsg: 'Saving...',
							timeout: 1200000, // 10min
							success: function(fp, o) {
								Recordentry.getForm().reset();
								Ext.getCmp('enrollment_upload').focus(true, 500);
								Ext.MessageBox.show({
									title: 'Info',
									msg: o.result.msg,
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.INFO
								});

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


					}
				}, {
					text: 'Reset',
					iconCls: 'icon-reset',
					tabIndex: 62,
					handler: function() {
						RecordStore.load();
						Recordentry.getForm().reset();
						Ext.getCmp('enrollment_upload').focus(true, 500);
					}
				},
				{
					text: 'Close',
					iconCls: 'icon-close',
					tabIndex: 63,
					handler: function() {
						FormWindow.close();
					}
				}
			]

		});

		var varWidth = (window.innerWidth - FormWindow.width) / 2;
		var varHeight = (window.innerHeight - FormWindow.height) / 4;

		// Set the position of the window
		FormWindow.setPosition(varWidth, varHeight);

		// Show the window
		FormWindow.show();

		// Adjust the window position on resize to maintain centering
		Ext.EventManager.onWindowResize(function() {
			var varWidth = (window.innerWidth - FormWindow.width) / 2;
			var varHeight = (window.innerHeight - FormWindow.height) / 4;
			FormWindow.setPosition(varWidth, varHeight);
		});

		Ext.getCmp('enrollment_upload').focus(true, 500);
	});
</script>
<?php require "_footer.php"; ?>