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

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

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

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 Name",
		width: 280,
		dataIndex: 'lecture_name',
		sortable: true
  	}]);

	
	var grid = new Ext.grid.GridPanel({
   		store: LectureStore,
        cm: colModel,
				columnLines:true,
				stripeRows:true,
				loadMask:true,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                	Ext.getCmp("lectureform").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: 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 lectureform = new Ext.FormPanel({	
		id: 'lectureform',
		frame: true,
		url: 'save-lecture.php',
    	labelAlign: 'left',
    	items: [{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :labelArray[0] ,
						name: 'lecture_name',
						id: 'lecture_name',
						tabIndex:1,
						allowBlank:false
					},
					{
						xtype: 'textfield',
						name: 'lecture_id',
						id:'lecture_id',
						value:'0',
						hidden:true
					},{height:10},
					grid
				]
    });
	

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

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

});
</script>
</div>

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