<?php 
	require "_header.php"; 
	require "data/combo_student.php";
?>
<div id="main-content" >
<script type="text/javascript">
var Semester_Fields = [
	{name:'semester_id',type: 'int'},
	{name:'branch_id',type: 'int'},
	{name:'branch_name'},
	{name:'semester_name'},
	{name:'rollno_print'}
]

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

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

var SemesterStore = new Ext.data.JsonStore({
    url: 'data/semester-grid-data.php',
    root: 'data',
    fields: Semester_Fields,
    autoLoad: true,
	
});
//  var rollnostore = new Ext.data.ArrayStore({
//            fields: ['rollno_print'],
// 	       data: [['Yes'],['No']]
//         }); 
var branchstore = new Ext.data.ArrayStore({
		fields: ['branch_id', 'branch_name'],
		data: Ext.student.branch
  	}); 
	
Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	{ 
		header: "Course Name",
		width: 200,
		dataIndex: 'branch_name',
		sortable: true
  	},
	{		
		header: "Semester Name",
		width: 100,
		dataIndex: 'semester_name',
		sortable: true
  	},
	// {		
	// 	header: "Roll No Print",
	// 	width: 100,
	// 	dataIndex: 'rollno_print',
	// 	sortable: true
  	// }
	]);

	
	var grid = new Ext.grid.GridPanel({
   		store: SemesterStore,
        cm: colModel,
		columnLines:true,
		stripeRows:true,
		loadMask:true,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                	Ext.getCmp("semesterform").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 semesterform = new Ext.FormPanel({	
		id: 'semesterform',
		frame: true,
		url: 'save-semester.php',
    	labelAlign: 'left',
    	items: [	
					{		
										xtype: 'combo',
										name: 'branch_id',
										id:'branch_id',
										hiddenName: 'branch_hidden',
										labelSeparator:'',	
										fieldLabel :'Course :<span class="comp">*</span>' ,
										forceSelection:true,				
										displayField:'branch_name',
										valueField: 'branch_id'
										,allowBlank: false
										,triggerAction:'all'
										,mode:'local'
										,store: branchstore
										,width:170
										,selectOnFocus: true,
										tabIndex:1,
										
					},
					{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :labelArray[0] ,
						name: 'semester_name',
						id: 'semester_name',
						tabIndex:1,
						allowBlank:false
					},
					// {
					// xtype: 'combo',
					// name: 'rollno_print',
					// id:'rollno_print',
					// labelSeparator:'',	
					// fieldLabel :'Roll No: <span class="comp">*</span>' ,
					// forceSelection:true,				
					// displayField:'rollno_print',
					// valueField: 'rollno_print',
					// allowBlank: false
					// ,triggerAction:'all'
					// ,mode:'local'
					// ,store: rollnostore
					// ,width:170
					// ,selectOnFocus: true,
					// tabIndex:99
					// },
					{
						xtype: 'textfield',
						name: 'semester_id',
						id:'semester_id',
						value:'0',
						hidden:true
					},{height:10},
					grid
				]
    });
	

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

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

});
</script>
</div>

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