<?php 
	require "_header.php"; 
?>
<div id="main-content">
<script type="text/javascript">
var Exam_Fields = [
	{name:'exam_id',type: 'int'},
	{name:'exam_name'}	
]

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

var labelArray;
labelArray = [  
	['Exam Name:<span class="comp">*</span>']
];

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

Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	{ 
		header: "Exam Name",
		width: 280,
		dataIndex: 'exam_name',
		sortable: true
  	}
	]);

	
	var grid = new Ext.grid.GridPanel({
   		store: ExamStore,
        cm: colModel,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                	Ext.getCmp("examform").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: 245,
		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 examform = new Ext.FormPanel({	
		id: 'examform',
		frame: true,
		url: 'save-exam.php',
    	labelAlign: 'left',
  		bodyStyle:'padding:5px',
   		layout: 'column',
    	items: [
			{
				columnWidth: 1,
				xtype: 'fieldset',
				labelWidth: 100,
				defaults: {border:false},
				defaultType: 'textfield',
				autoHeight: true,
				bodyStyle: Ext.isIE ? 'padding:0 0 5px 1px;' : 'padding:5px 1px;',
				border: false,
				style: {
					"margin-left": "5px"
				},
            	items: [
					{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :labelArray[0] ,
						name: 'exam_name',
						id: 'exam_name',
						tabIndex:1,
						width:200,
						allowBlank:false
					},
					{
						xtype: 'textfield',
						name: 'exam_id',
						id:'exam_id',
						value:'0',
						hidden:true
					},
					grid
				]
			}
        ]
    });
	

	var ExamWindow = new Ext.Window({
		title: 'Exam Master',
		width:350,		
		height:375,	
		frame: true,
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: examform,
		buttons:[
			{
				text: 'Save', 
				name:'btnSave',
				tabIndex:2,
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
					
					
					var exam_name = Ext.getCmp('exam_name').getValue();
					var exam_id = Ext.getCmp('exam_id').getValue();
					
					if(exam_name!=""){			
						Ext.Ajax.request({
							loadMask: true,
							sync : true,
							url: 'data/check-duplication.php',
							params: {action:'exam',exam_name:exam_name,exam_id:exam_id},
							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('exam_name').reset();
								}	
								else if(resbool=='true'){
									examform.getForm().submit({
										waitMsg: 'Saving...',
										success: function () {	
											grid.getSelectionModel().clearSelections();
											Ext.getCmp('btnDelete').disable();	
											ExamStore.load();
											examform.getForm().reset();	
											Ext.getCmp('exam_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:3,
				name: 'btnDelete',
				handler: function(){
					/*var exam_id=Ext.getCmp('exam_id').getValue();
					Ext.Ajax.request({
						loadMask: true,
						sync : true,
						url: 'data/getRelatedRecord.php',
						params: {action:'semester',exam_id:exam_id},
						success: function(resp){
							var str = "";
							resbool=resp.responseText;
							if(resbool != 0){
								str = "It has related record in " + resbool +"Please Delete This Record First";
								Ext.MessageBox.show({
									title: 'Error',
									msg: str,
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});			
							}
							else{*/
								Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
							/*}
						}				
					});*/
				}
			},
			{
				text: 'Reset', 
				name:'btnreset',
				tabIndex:4,
				iconCls:'icon-reset',
				id:'btnreset',
				handler: function (){
					examform.getForm().reset();
					grid.getSelectionModel().clearSelections();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('exam_name').focus(true,500);
					if(flgAdd == "true"){
									Ext.getCmp('btnSave').enable();
					 }
				}
			},
			{
				text:'Close', 
				name:'btnclose',
				tabIndex:5,
				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('exam_id').setValue('0');	
					ExamStore.load();	
					Ext.getCmp('exam_name').focus(true,500);								
				}
			});
	 	}  
  	}
	
	var varWidth =  (screen.width - ExamWindow.width)/2;
	ExamWindow.setPosition(varWidth,((screen.height/4)-85));
	ExamWindow.show();
	Ext.getCmp('exam_name').focus(true,500);
	
if(flgAdd == "true"){
				Ext.getCmp('btnSave').enable();
 }else
{
				Ext.getCmp('btnSave').disable();
}


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