<?php 
	require "_header.php";
	require "data/combo_semester.php";
?>
<div id="main-content">
  <link rel="stylesheet" type="text/css" href="css/ColumnHeaderGroup.css" />
<style >
.ext-strict .x-grid3-cell-inner, .ext-strict .x-grid3-hd-inner {
    text-align: center;
    width: auto;
}
</style>
<script type="text/javascript" src="js/ColumnHeaderGroup.js"></script>
<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 branch = new Ext.form.ComboBox({
	name:'branch_name',
	id:'branch_name',
	hiddenName:'branch_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[0] ,
	displayField:'branch_name',
	valueField: 'branch_id',
	editable:true,
	triggerAction:'all',
	tabIndex:1,
	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,
	triggerAction:'all',
	//tabIndex:3,
	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',
	tabIndex:3,
	valueField: 'exam_id',
	editable:true,
	triggerAction:'all',
	mode:'local',
	//width: 100,
	allowBlank:false,
	store: examstore,
    listeners:{'select': function(cmb, rec, idx){
		LoadData();
	}}
});

var total_mark = new Ext.form.NumberField({
	fieldLabel :labelArray[4],
	labelSeparator:'',	
	name: 'total_marks',
	id:'total_marks',
	allowBlank:false
});

var passing_mark = new Ext.form.NumberField({
	fieldLabel :labelArray[5],
	labelSeparator:'',	
	name: 'passing_marks',
	id:'passing_marks',
	allowBlank:false
});

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 Setup_Fields = [
	{name:'subject_id',type:'int'},
	{name:'total_marks',type:'string'},
	{name:'passing_marks',type:'string'},
	{name:'total_marks_pr',type:'string'},
	{name:'passing_marks_pr',type:'string'},
	{name:'total_marks_pr_ese',type:'int'},
	{name:'passing_marks_pr_ese',type:'int'},
	{name:'total_marks_pr_pa',type:'int'},
	{name:'passing_marks_pr_pa',type:'int'},
	{name:'subject_name'},
	{name:'subject_code'}
]

var store = new Ext.data.JsonStore({
	url: 'data/examsetup-grid-data.php',
	totalProperty: 'count',
	idProperty: 'subject_id',
	successProperty: 'success',
	fields: Setup_Fields,
	root: 'data',
	writer: new Ext.data.JsonWriter(),
	autoSave: false,
	batch:true
});
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();

	if(branch_name != '' && semester_name != '' && exam_name != ''){	
		store.load({params:{
			branch_name:branch_name,
			semester_name:semester_name,
			exam_name:exam_name	
		}});
	}
} 
Ext.onReady(function(){
	Ext.QuickTips.init();
	
	var grid1 = new Ext.grid.EditorGridPanel({
	store: store,
	 plugins: [new Ext.ux.grid.ColumnHeaderGroup({
        rows: [
            [
				{header: 'Sr No.'},
				{rowspan:2},
                {rowspan:2,header: 'Subject Name'},
                {rowspan:2,header: 'Subject code'},
				{colspan:2, header: 'Theory'},
				{colspan:4, header: 'Practical'},
             ],[
				{},
				{},
                {},
                {},
				{},
				{},
				{colspan: 2, header: 'Viva/Practical exam/Performance (ESE)'},
				{colspan: 2, header: 'TW/Report writing/Seminar (PA)'},
             ]
        ]
		
    })],
	columnLines: true,
	columns: [
		new Ext.ux.grid.PagingRowNumberer(),
		{
			dataIndex: 'subject_id',
			header: 'Subject ID',
			id: 'subject_id',
			hidden: true
		},{
			dataIndex: 'subject_name',
			header: '',
			sortable:true,
			id: 'subject_name',
			width:215
		},{
			dataIndex: 'subject_code',
			header: '',
			sortable:true,
			id: 'subject_code',
			width:90
		},{
			header     : 'Total Marks',
			sortable : true,
			width:55,
			dataIndex: 'total_marks',
			sortable:true,
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank: false,
				maxLength:3,
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '3'}
			}
		},{
			header     : 'Passing Marks',
			width    : 75,
			sortable : true,
			dataIndex: 'passing_marks',
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank:false,
				maxLength:3,
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '3'}
			}
		},{
			header     : 'Total Marks',
			sortable : true,
			width:55,
			dataIndex: 'total_marks_pr_ese',
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank:false,
				maxLength:3,
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '3'}
			}
		},{
			header     : 'Passing Marks',
			width    : 75,
			sortable : true,
			dataIndex: 'passing_marks_pr_ese',
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank:false,
				maxLength:3,
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '3'}
			}
		},{
			header     : 'Total Marks ',
			sortable : true,
			width:55,
			dataIndex: 'total_marks_pr_pa',
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank:false,
				maxLength:3,
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '3'}
			}
		},{
			header     : 'Passing Marks',
			width    : 75,
			sortable : true,
			dataIndex: 'passing_marks_pr_pa',
			editor: {
				xtype: 'numberfield',
				style:'text-align:left',
				allowBlank:false,
				maxLength:3,
				allowNegative: false,
				allowBlank: false,
				autoCreate: {tag: 'input', type: 'text',  maxlength: '3'}
			}
		}],
		height:400,
		layout: 'fit',
		clicksToEdit:1,
		loadMask: true,   
		selModel: new Ext.grid.RowSelectionModel({singleSelect:true}),
		 viewConfig: {
            stripeRows: true
        }
	});
	grid1.on('render', function() {
	grid1.tip = new Ext.ToolTip({
		view: grid1.getView(),
		target: grid1.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({'beforeload': {
	fn: function(store, options){
		options.params || (options.params = {});
		Ext.apply(options.params, {
			pageNo: this.pageNumber		
		});
	},
	scope: this
}});
store.on({'load': {fn: function(){
	if(store.getCount() <= 0){
		Ext.MessageBox.show({
			title: 'Error',
			msg: 'There is no Subject found in this Selection.',
			buttons: Ext.MessageBox.OK,
			icon:Ext.MessageBox.ERROR
		});
	} else{
		grid1.startEditing(0,3);
	}
}}});	

/*Ext.override(Ext.Editor, {
	onBlur: function() {
		if (this.allowBlur === true && this.editing) {
			this.completeEdit();
		}
	}
});*/
/* END EDITABLE GRID */



	var ExamForm = new Ext.FormPanel({
		id:'ExamForm',
    	labelWidth: 70,
       	url: 'save-examsetup.php',
		frame:true,
		title: '',
        bodyStyle:'padding:6px 6px 0',
        layout:'form',
		defaultType: 'textfield',
        items :[{
			xtype:'container',
			autoEl:{},
			border:false,
			layout:'column',
			items:[{
				layout:'column',
				columnWidth:1,
				labelWidth: 60,
				items:[{
						layout:'form',
						columnWidth:0.33,
						items:[branch]
					},{
						layout:'form',
						columnWidth:0.33,
						items:[semester]
					},{
						layout:'form',
						columnWidth:0.33,
						items:[exam]
					}]
			},{
				xtype:'container',
				autoEl:{},
				layout:'form',
				columnWidth:1,
				items:[grid1]
			}]
		}]
 	});
	
	var ExamWindow = new Ext.Window({
        title:'Exam Setup',
        height:500,
        width:800,
		modal:true,
        layout:'fit',
		border:false,
		closeAction:'close',
		collapsible:'true',
		items:ExamForm,
		buttonAlign:'center',
	    buttonAlign:'center',
	    buttons:[
			{
				text: 'Save', 
				name:'btnSave',
				tabIndex:4,
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
					grid1.stopEditing();	
					if(grid1.store.getCount()>=1){
						var arr_subjectid = " ";
						var arr_totalmark = " ";
						var arr_passingmark = " ";
						var arr_totalmark_pr = " ";
						var arr_passingmark_pr = " ";
						var arr_totalmark_pr_ese = " ";
						var arr_passingmark_pr_ese = " ";
						var arr_totalmark_pr_pa = " ";
						var arr_passingmark_pr_pa = " ";
						var  len = grid1.store.getCount();
						for(var i = 0;i<len; i++){
							
							record = grid1.store.getAt(i);
							subject_id = record.data.subject_id;
							totalmark = record.data.total_marks;
							passingmark = record.data.passing_marks;
							totalmark_pr = record.data.total_marks_pr;
							passingmark_pr = record.data.passing_marks_pr;
							totalmark_pr_ese = record.data.total_marks_pr_ese;
							passingmark_pr_ese = record.data.passing_marks_pr_ese;
							totalmark_pr_pa = record.data.total_marks_pr_pa ;
							passingmark_pr_pa  = record.data.passing_marks_pr_pa ;
						
							arr_subjectid+= subject_id+",";	
							arr_totalmark+=  totalmark+",";	
							arr_passingmark+= passingmark+",";	
							arr_totalmark_pr+= totalmark_pr+",";	
							arr_passingmark_pr+=passingmark_pr+",";	
							arr_totalmark_pr_ese+=totalmark_pr_ese+",";	
							arr_passingmark_pr_ese+=passingmark_pr_ese+",";	
							arr_totalmark_pr_pa+= totalmark_pr_pa+",";	
							arr_passingmark_pr_pa+= passingmark_pr_pa+",";	
							
							/*if(arr_subjectid != " "){
								arr_subjectid = arr_subjectid + "," + subject_id;	
							} else{
								arr_subjectid = subject_id;	
							}
							
							if(arr_totalmark != " "){
								arr_totalmark = arr_totalmark + "," + totalmark;	
							} else{
								arr_totalmark = totalmark;	
							}
							
							if(arr_passingmark != " "){
								arr_passingmark = arr_passingmark + "," + passingmark;	
							} else{
								arr_passingmark = passingmark;	
							}
							
							if(arr_totalmark_pr != " "){
								arr_totalmark_pr = arr_totalmark_pr + "," + totalmark_pr;	
							} else{
								arr_totalmark_pr = totalmark_pr;	
							}
							
							if(arr_passingmark_pr != " "){
								arr_passingmark_pr = arr_passingmark_pr + "," + passingmark_pr;	
							} else{
								arr_passingmark_pr = passingmark_pr;	
							}
							/////////////dd /////////////////////
							if(arr_totalmark_pr_ese != " "){
								arr_totalmark_pr_ese = arr_totalmark_pr_ese + "," + totalmark_pr_ese;	
							} else{
								arr_totalmark_pr_ese = totalmark_pr_ese;	
							}
							
							if(arr_passingmark_pr_ese != " "){
								arr_passingmark_pr_ese = arr_passingmark_pr_ese + "," + passingmark_pr_ese;	
							} else{
								arr_passingmark_pr_ese = passingmark_pr_ese;	
							}
							
							
							if(arr_totalmark_pr_pa != " "){
								arr_totalmark_pr_pa = arr_totalmark_pr_pa + "," + totalmark_pr_pa;	
							} else{
								arr_totalmark_pr_pa = totalmark_pr_pa;	
							}
							
							if(arr_passingmark_pr_pa != " "){
								arr_passingmark_pr_pa = arr_passingmark_pr_pa + "," + passingmark_pr_pa;	
							} else{
								arr_passingmark_pr_pa = passingmark_pr_pa;	
							}*/
							
						}
						ExamForm.getForm().submit({
							params:{subject_id:arr_subjectid,totalmark:arr_totalmark,passingmark:arr_passingmark,
							totalmark_pr:arr_totalmark_pr,passingmark_pr:arr_passingmark_pr,
							totalmark_pr_ese:arr_totalmark_pr_ese,passingmark_pr_ese:arr_passingmark_pr_ese,
							totalmark_pr_pa:arr_totalmark_pr_pa,passingmark_pr_pa:arr_passingmark_pr_pa,
							},	
							waitMsg: 'Saving...',		
							success: function () {		
								Ext.MessageBox.show({
									title: 'Message',
									msg: 'Data has been saved..',
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.INFO,
									
								});
								store.load();
								//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: 'Please enter valid form no.',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.ERROR
						});
					}
				}	
			},
			{
				text: 'Delete',
				disabled:true,
				id: 'btnDelete',
				tabIndex:5,
				iconCls:'remove',
				name: 'btnDelete',
				handler: function(){
					Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
				}
			},
			{
				text: 'Reset', 
				name:'btnreset',
				tabIndex:6,
				iconCls:'icon-reset',
				id:'btnreset',
				handler: function (){
					ExamForm.getForm().reset();
					grid1.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:7,
				iconCls:'icon-close',
				id:'btnclose',
				handler: function (){
					ExamWindow.close();
				}
			}
		]
	});
	
	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();
	Ext.getCmp('branch_name').focus(true,1000);
});
</script>
</div>
<?php require "_footer.php";?>