<?php 
	require "_header.php";
	require "data/combo_semester.php";
?>
<div id="main-content">
<script type="text/javascript">
var labelArray;
labelArray = [
	['Branch:<span class="comp">*</span>'],
	['Semester:<span class="comp">*</span>'],
	['Subject:<span class="comp">*</span>'],
	['Exam:<span class="comp">*</span>'],
	['Total Mark:<span class="comp">*</span>'],
	['Passing Mark:<span class="comp">*</span>']
];

var branchstore = new Ext.data.ArrayStore({		
	fields: ['branch_id','branch_name'],
	data:Ext.semester.branch
});

var semesterstore = new Ext.data.ArrayStore({		
	fields: ['semester_id','semester_name'],
	data:Ext.semester.semester
});

var subjectstore = new Ext.data.ArrayStore({		
	fields: ['subject_id','subject_name'],
	data:Ext.semester.subject
});

var examstore = new Ext.data.ArrayStore({		
	fields: ['exam_id','exam_name'],
	data:Ext.semester.exam
});

var daytimestore = new Ext.data.ArrayStore({
	fields: ['day_time'],
	data: [['Morning'],['Afternoon'],['Evening']]
});

var branch = new Ext.form.ComboBox({
	name:'branch_name',
	id:'branch_name',
	tabIndex:1,
	hiddenName:'branch_hidden',
	labelSeparator:'',	
	fieldLabel:labelArray[0] ,
	displayField:'branch_name',
	valueField:'branch_id',
	editable:true,
	triggerAction:'all',
	mode:'local',
	//width: 100,
	allowBlank:false,
	store:branchstore,
    listeners:{'select': function(cmb, rec, idx){
		LoadData();
	}}
});

var semester = new Ext.form.ComboBox({
	name:'semester_name',
	id:'semester_name',
	hiddenName:'semester_hidden',
	labelSeparator:'',	
	tabIndex:2,
	fieldLabel:labelArray[1] ,
	displayField:'semester_name',
	valueField:'semester_id',
	editable:true,
	triggerAction:'all',
	mode:'local',
	//width: 100,
	allowBlank:false,
	store:semesterstore,
    listeners:{'select': function(cmb, rec, idx){
		LoadData();
	}}
});

var subject = new Ext.form.ComboBox({
	name: 'subject_name',
	id:'subject_name',
	hiddenName:'subject_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[2] ,
	displayField:'subject_name',
	valueField: 'subject_id',
	editable:true,
	//tabIndex:3,
	triggerAction:'all',
	mode:'local',
	width: 127,
	allowBlank:false,
	store: subjectstore,
    listeners:{'select': function(cmb, rec, idx){
		LoadData();
	}}
});

var exam = new Ext.form.ComboBox({
	name: 'exam_name',
	id:'exam_name',
	hiddenName:'exam_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[3] ,
	displayField:'exam_name',
	valueField: 'exam_id',
	editable:true,
	tabIndex:3,
	triggerAction:'all',
	mode:'local',
	//width: 100,
	allowBlank:false,
	store: examstore,
    listeners:{'select': function(cmb, rec, idx){
		LoadData();
	}}
});

var firsttest = new Ext.form.Radio({	
	hideLabel: true,
	name: 'test',
	id: 'firsttest',
	tabIndex:4,
	inputValue: 'f',
	boxLabel:'First Test',
    handler:function(){
		LoadData();
	}
});

var retest = new Ext.form.Radio({	
	hideLabel: true,
	name: 'test',
	id: 'retest',
	tabIndex:5,
	inputValue: 'r',
	boxLabel:'Retest',
    handler:function(){
		LoadData();
	}
});

/* START EDITABLE GRID */
Ext.ns('Ext.ux.grid');
Ext.ux.grid.PagingRowNumberer = Ext.extend(Ext.grid.RowNumberer,{
	renderer: function(v, p, record, rowIndex, colIndex, store) {
		if (this.rowspan) {
			p.cellAttr = 'rowspan="' + this.rowspan + '"';
		}
		var so = store.lastOptions;
		var sop = so ? so.params: null;
		return ((sop && sop.start) ? sop.start: 0) + rowIndex + 1;
	}
});
var xg = Ext.grid;

var Schedule_Fields = [
	{name:'subject_id',type:'int'},
	{name:'daydate',type:'date'},
	{name:'daytime',type:'string'},
	{name:'subject_name',type:'string'}
]

var store = new Ext.data.JsonStore({
	url: 'data/examschedule-grid-data.php',
	totalProperty: 'count',
	idProperty: 'subject_id',
	successProperty: 'success',
	fields: Schedule_Fields,
	root: 'data',
	writer: new Ext.data.JsonWriter(),
	autoSave: false,
	batch:true
});

function daterandrrer(value)
{
	if(value!="" && value!=null){
		return value.format('d/m/Y');
	} else{
		return value;
	}
}

var grid = new Ext.grid.EditorGridPanel({
	store: store,
	clicksToEdit:1,
	loadMask:true,
	columnLines:true,
	stripeRows:true,
	columns: [
		new Ext.ux.grid.PagingRowNumberer(),
		{
			dataIndex: 'subject_id',
			header: 'Subject ID',
			id: 'subject_id',
			hidden: true
		},{
			dataIndex: 'subject_name',
			header: 'Subject Name',
			sortable:true,
			id: 'subject_name',
			width:185
		},{
			dataIndex:'daydate',
			header:'Date',
			sortable:true,
			width:80, 
			renderer:daterandrrer,
			editor: {
				xtype:'datefield',
				id:'daydate',
				name:'daydate',
				allowBlank:false,
				vtype:'daterange'//,minValue:new Date()			
			}
		},{
			dataIndex:'daytime',
			header:'Time',
			sortable:true,
			width:80, 
			editor: {
				xtype:'timefield',
				name: 'start_time',
				id:'start_time',
				emptyText:'From',
				labelSeparator:'',
				editable:false,
				width: 80,
				minValue: '6:00am',
                maxValue: '9:00pm',
				allowBlank:false
				
				/*xtype:'combo',
				name:'daytime',
				allowBlank:false,
				id:'daytime',
				forceSelection:true,
				tabIndex:5,
				displayField:'day_time',
				valueField:'day_time',
				width:163,
				triggerAction:'all',
				mode:'local',
				store:daytimestore*/
			}
		}
	],
	height:250,
	layout: 'fit',
	loadMask: true,   
	selModel: new Ext.grid.RowSelectionModel({singleSelect:true})
});

/* END EDITABLE GRID */

function LoadData(){
	var branch_name = Ext.getCmp('branch_name').getValue();
	var semester_name = Ext.getCmp('semester_name').getValue();
	var exam_name = Ext.getCmp('exam_name').getValue();
	
	var firsttest = Ext.getCmp('firsttest').getValue();
	var retest = Ext.getCmp('retest').getValue();
	var test_type = '';
	
	if(firsttest == true){
		test_type = 'f';
	} else if(retest == true){
		test_type = 'r';
	}

	if(branch_name != '' && semester_name != '' && exam_name != '' && test_type != ''){	
		store.load({params:{
			branch_name:branch_name,
			semester_name:semester_name,
			exam_name:exam_name,
			test_type:test_type
		}});
	}
}

Ext.onReady(function(){
	Ext.QuickTips.init();
	
	var ExamForm = new Ext.FormPanel({
		id:'ExamForm',
    	labelWidth: 70,
        url: 'save-examschedule.php',
		frame:true,
		title: '',
        bodyStyle:'padding:6px 6px 0',
        layout:'form',
		defaultType: 'textfield',
        items :[{
			xtype:'container',
			autoEl:{},
			border:false,
			layout:'column',
			anchor:'100%',
			defaultType:'field',
			items:[{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.40,
				items:[branch,semester,exam,firsttest,retest]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:.60,
				items:[grid]
			}]
		}]
 	});
	
	var ExamWindow = new Ext.Window({
        title:'Exam Schedule',
        height:340,
        width:700,
		modal:true,
        layout:'fit',
		border:false,
		closeAction:'close',
		collapsible:'true',
		items:ExamForm,
		buttonAlign:'center',
	    buttons:[
			{
				text: 'Save', 
				name:'btnSave',
				tabIndex:6,
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
					grid.stopEditing();	
					
					if(Ext.getCmp('firsttest').getValue() == true || Ext.getCmp('retest').getValue() == true){
						if(grid.store.getCount()>=1){
							var arr_subjectid = " ";
							var arr_daydate = " ";
							var arr_daytime = " ";
							for(var i = 0, len = grid.store.getCount(); i < len; i++){
								record = grid.store.getAt(i);
								subject_id = record.data.subject_id;
								daydate = record.data.daydate;
								
								if(daydate != ''){
									
									var month = daydate.getMonth() + 1;
									var day = daydate.getDate();
									var year = daydate.getFullYear();
									daydate = day + "/" + month + "/" +  year;
								}
								daytime = record.data.daytime;
								
								if(arr_subjectid != " "){
									arr_subjectid = arr_subjectid + "," + subject_id;	
								} else{
									arr_subjectid = subject_id;	
								}
								
								if(arr_daydate != " "){
									arr_daydate = arr_daydate + "," + daydate;	
								} else{
									arr_daydate = daydate;	
								}
								
								if(arr_daytime != " "){
									arr_daytime = arr_daytime + "," + daytime;	
								} else{
									arr_daytime = daytime;	
								}
							}
							ExamForm.getForm().submit({
								params:{subject_id:arr_subjectid,daydate:arr_daydate,daytime:arr_daytime},	
								waitMsg: 'Saving...',		
								success: function () {		
									Ext.MessageBox.show({
										title: 'Message',
										msg: 'Data has been saved..',
										buttons: Ext.MessageBox.OK,
										icon:Ext.MessageBox.INFO
									});
									ExamForm.getForm().reset();
									store.removeAll();
								},
								failure: function () {	
									Ext.MessageBox.show({
										title: 'Error',
										msg: 'Please fill complete form!',
										buttons: Ext.MessageBox.OK,
										icon:Ext.MessageBox.ERROR
									});
									if(formMode=='edit'){
										studententry.getForm().findField('sectionid_id').disable(); 
										studententry.getForm().findField('grno').disable();
									}
								}
							});
						}
					} else{
						Ext.MessageBox.show({
							title: 'Error',
							msg: 'Select First Test or Retest option!',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.ERROR
						});
					}
				}	
			},{
				text: 'Reset', 
				name:'btnreset',
				tabIndex:7,
				iconCls:'icon-reset',
				id:'btnreset',
				handler: function (){
					ExamForm.getForm().reset();
					grid.getSelectionModel().clearSelections();
					//Ext.getCmp('btnDelete').disable();
					Ext.getCmp('branch_name').focus(true,500);
					if(flgAdd == "true"){
									Ext.getCmp('btnSave').enable();
					 }
				}
			},{
				text:'Close', 
				name:'btnclose',
				tabIndex:8,
				iconCls:'icon-close',
				id:'btnclose',
				handler: function (){
					ExamWindow.close();
				}
			}
		]
	});
	Ext.getCmp('branch_name').focus(true,1000);	
	function deleterecord(btn){
    	if(btn=='yes'){
	   		ExamForm.getForm().submit({
				waitMsg: 'Saving...',
				params:{act:'del'},
				success: function () {	
					ExamForm.getForm().reset();
					//Ext.getCmp('btnDelete').disable();
					Ext.getCmp('setup_id').setValue('0');	
					ExamsetupStore.load();		
					Ext.getCmp('branch_name').focus(true,500);							
				}
			});
	 	}  
  	}

	var varWidth =  (screen.width - ExamWindow.width)/2;
	ExamWindow.setPosition(varWidth,((screen.height/4)-85));
	ExamWindow.show();
	
});
</script>
<div id="abcd"></div>
</div>
<?php require "_footer.php";?>