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

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

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

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

Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	
  	{ 
		header: "Marks Date",
		width: 150,
		dataIndex: 'marks_date',
		sortable: true
  	}
  	]);

	
	var grid = new Ext.grid.GridPanel({
   		store: RoomStore,
        cm: colModel,
		columnLines:true,
		stripeRows:true,
		loadMask:true,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                	Ext.getCmp("roomform").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 roomform = new Ext.FormPanel({	
		id: 'roomform',
		frame: true,
		url: 'save-room.php',
    	labelAlign: 'left',
    	items: [{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :labelArray[0] ,
						name: 'room_name',
						id: 'room_name',
						tabIndex:1,
						hidden:true,
						allowBlank:true
					},
					{
						xtype: 'datefield',
						name: 'marks_date',
						id:'marks_date',
						labelSeparator:'',	
						fieldLabel :'Date' ,
						tabIndex:13,
											allowBlank:true,
						width:158									
					},
					{
						xtype: 'textfield',
						name: 'room_id',
						id:'room_id',
						value:'0',
						hidden:true
					},{height:10},
					grid
				]
    });
    
	var RoomWindow = new Ext.Window({
		title: 'Marks Date Master',
		width:350,		
		height:340,	
		frame: true,
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: roomform,
		buttons:[
			{
				text: 'Save', 
				name:'btnSave',
				tabIndex:2,
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
					
					
					var room_name = Ext.getCmp('room_name').getValue();
					var room_id = Ext.getCmp('room_id').getValue();
					if(room_name!= '')
					{
									roomform.getForm().submit({
										waitMsg: 'Saving...',
										success: function () {	
											grid.getSelectionModel().clearSelections();
											Ext.getCmp('btnDelete').disable();	
											RoomStore.load();
											roomform.getForm().reset();	
											Ext.getCmp('room_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 room_id=Ext.getCmp('room_id').getValue();
					Ext.Ajax.request({
						loadMask: true,
						sync : true,
						url: 'data/getRelatedRecord.php',
						params: {action:'Lecture',room_id:room_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 (){
					roomform.getForm().reset();
					grid.getSelectionModel().clearSelections();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('room_name').focus(true,500);
					if(flgAdd == "true"){
									Ext.getCmp('btnSave').enable();
					 }
				}
			},
			{
				text:'Close', 
				name:'btnclose',
				tabIndex:5,
				iconCls:'icon-close',
				id:'btnclose',
				handler: function (){
					RoomWindow.close();
				}
			}
		]
	});
	
	function deleterecord(btn){
    	if(btn=='yes'){
	   		roomform.getForm().submit({
				waitMsg:'Saving...',
				params:{act:'del'},
				success:function(){	
					roomform.getForm().reset();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('room_id').setValue('0');	
					RoomStore.load();	
					Ext.getCmp('room_name').focus(true,500);								
				}
			});
	 	}  
  	}
	
	var varWidth =  (screen.width - RoomWindow.width)/2;
	RoomWindow.setPosition(varWidth,((screen.height/4)-85));
	RoomWindow.show();
	
if(flgAdd == "true"){
				Ext.getCmp('btnSave').enable();
 }else
{
				Ext.getCmp('btnSave').disable();
}

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

});
</script>
</div>

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