<?php 
	require "_header.php"; 
?>
<div id="main-content" >
<script type="text/javascript">
var Time_Fields = [
	{name:'time_id',type: 'int'},
	{name:'time_start_name'},
	{name:'time_end_name'},
	{name:'lec_id'},
]

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

var labelArray;
labelArray = [  
	['Start Time:<span class="comp">*</span>'],
	['End Time:<span class="comp">*</span>']
];

var timeStore = new Ext.data.JsonStore({
    url: 'data/time-grid-data.php',
    root: 'data',
    fields: Time_Fields,
    autoLoad: true
});
var Lecture_Fields = [
	{name:'lecture_id',type: 'int'},
	{name:'lecture_name',type: 'string'}
]
var LectureStore = new Ext.data.JsonStore({
    url: 'data/lecture-grid-data.php',
    root: 'data',
    fields: Lecture_Fields,
    autoLoad: true
});

Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	
	{ 
		header: "Lecture",
		width: 70,
		dataIndex: 'lec_id',
		sortable: true
  	},
	{ 
		header: "Start Time",
		width: 140,
		dataIndex: 'time_start_name',
		sortable: true
  	},
  { 
		header: "End Time",
		width: 140,
		dataIndex: 'time_end_name',
		sortable: true
  	},	
  	]);

	
	var grid = new Ext.grid.GridPanel({
   		store: timeStore,
        cm: colModel,
		columnLines:true,
		stripeRows:true,
		loadMask:true,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                	Ext.getCmp("Timeform").getForm().loadRecord(rec);
                	Ext.getCmp('lecture_name').setValue(rec.data.lec_id);
									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: 210,
		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 Timeform = new Ext.FormPanel({	
		id: 'Timeform',
		frame: true,
		url: 'save-time.php',
    	labelAlign: 'left',
    	items: [
    			{
						xtype: 'combo',
						labelSeparator:'',	
						fieldLabel :'Lecture' ,
						hiddenName:'hidden_lecture_id',
						name: 'lecture_name',
						id: 'lecture_name',
						tabIndex:1,
						displayField:'lecture_name',
						valueField:'lecture_id',
						forceSelection:true,
						width:130,
						triggerAction:'all',
						mode:'local',
						lastQuery:'',
						dataIndex:'lecture_name',	
						allowBlank:false,
						store:LectureStore
					},
    			{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :labelArray[0] ,
						name: 'time_start_name',
						id: 'time_start_name',
						tabIndex:1,
						allowBlank:false
					},
					{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :labelArray[1] ,
						name: 'time_end_name',
						id: 'time_end_name',
						tabIndex:1,
						allowBlank:false
					},
					{
						xtype: 'textfield',
						name: 'time_id',
						id:'time_id',
						value:'0',
						hidden:true
					},{height:10},
					grid
				]
    });
	

	var TimeWindow = new Ext.Window({
		title: 'Time Master',
		width:350,		
		height:340,	
		frame: true,
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: Timeform,
		buttons:[
			{
				text: 'Save', 
				name:'btnSave',
				tabIndex:2,
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
					
					
					var Time_start_name = Ext.getCmp('time_start_name').getValue();
					var Time_end_name = Ext.getCmp('time_end_name').getValue();
					var Time_id = Ext.getCmp('time_id').getValue();
					if(Time_start_name!= '')
					{
									Timeform.getForm().submit({
										waitMsg: 'Saving...',
										success: function () {	
											grid.getSelectionModel().clearSelections();
											Ext.getCmp('btnDelete').disable();	
											timeStore.load();
											Timeform.getForm().reset();	
											Ext.getCmp('time_start_name').focus(true,500);								
										},
										
									});							
				}
					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 Time_id=Ext.getCmp('time_id').getValue();
					Ext.Ajax.request({
						loadMask: true,
						sync : true,
						url: 'data/getRelatedRecord.php',
						params: {action:'Time',time_id:time_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 (){
					Timeform.getForm().reset();
					grid.getSelectionModel().clearSelections();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('Time_name').focus(true,500);
					if(flgAdd == "true"){
									Ext.getCmp('btnSave').enable();
					 }
				}
			},
			{
				text:'Close', 
				name:'btnclose',
				tabIndex:5,
				iconCls:'icon-close',
				id:'btnclose',
				handler: function (){
					TimeWindow.close();
				}
			}
		]
	});
	
	function deleterecord(btn){
    	if(btn=='yes'){
	   		Timeform.getForm().submit({
				waitMsg:'Saving...',
				params:{act:'del'},
				success:function(){	
					Timeform.getForm().reset();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('time_id').setValue('0');	
					timeStore.load();	
					Ext.getCmp('time_start_name').focus(true,500);								
				}
			});
	 	}  
  	}
	
	var varWidth =  (screen.width - TimeWindow.width)/2;
	TimeWindow.setPosition(varWidth,((screen.height/4)-85));
	TimeWindow.show();
	
if(flgAdd == "true"){
				Ext.getCmp('btnSave').enable();
 }else
{
				Ext.getCmp('btnSave').disable();
}

Ext.getCmp('time_start_name').focus(true,1000);

});
</script>
</div>

<?php require "_footer.php";?>