<?php 
	require "_header.php"; 
	require "data/combo_semester.php";
?>
<script type="text/javascript" src="jquery/ux/CheckColumn.js"></script>
<script type="text/javascript" >

var labelArray;
labelArray = [  
	['Branch:<span class="comp">*</span>'],
	['Semester:<span class="comp">*</span>'],
	['Subject:<span class="comp">*</span>'],
	['Select Faculty:<span class="comp">*</span>']
	
];
var branchstore = new Ext.data.ArrayStore({		
	fields: ['branch_id','branch_name'],
	data:Ext.semester.branch
});

var semesterstore = new Ext.data.ArrayStore({		
	fields: ['semester_id','semester_name'],
	data:Ext.semester.semesterAll
});

var subjectstore = new Ext.data.JsonStore({
	url:'data/get-subject.php',
    root: 'data',
	totalProperty: 'total',
	fields: ['subject_id','subject_name'] 
});
var faclutystore = new Ext.data.ArrayStore({		
	fields: ['employee_id','employee_name'],
	data:Ext.semester.faculty
});

var branch = new Ext.form.ComboBox({
	name:'branch_name',
	id:'branch_name',
	hiddenName:'branch_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[0] ,
	displayField:'branch_name',
	valueField: 'branch_id',
	editable:true,
	width:150,
	forceSelection:true,
	triggerAction:'all',
	mode:'local',
	allowBlank:false,
	store:branchstore,
    listeners:{'select': function(cmb, rec, idx){
		//Ext.getCmp('branch_disp').setValue(this.getRawValue());
		//Ext.getCmp('division_id').reset();
		LoadData();
	
	}}
});

var semester = new Ext.form.ComboBox({
	name:'semester_name',
	id:'semester_name',
	hiddenName:'semester_hidden',
	labelSeparator:'',	
	fieldLabel:labelArray[1] ,
	displayField:'semester_name',
	valueField:'semester_id',
	editable:true,
	forceSelection:true,
	triggerAction:'all',
	mode:'local',
	allowBlank:false,
	width:150,
	store:semesterstore,
    listeners:{'select': function(cmb, rec, idx){
		//Ext.getCmp('semester_disp').setValue(this.getRawValue());
		LoadData();
		
	
	}}
});

var subject = new Ext.form.ComboBox({
	name: 'subject_name',
	id:'subject_name',
	hiddenName:'subject_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[2] ,
	displayField:'subject_name',
	valueField: 'subject_id',
	editable:true,
	forceSelection:true,	
	triggerAction:'all',
	mode:'local',
	width:300,
	allowBlank:true,
	store: subjectstore
    
});
var employee_name = new Ext.form.ComboBox({
	name:'employee_name',
	id:'employee_name',
	hiddenName:'employee_hidden',
	labelSeparator:'',	
	fieldLabel :labelArray[3] ,
	displayField:'employee_name',
	valueField: 'employee_id',
	editable:true,
	width:350,
	forceSelection:true,
	triggerAction:'all',
	mode:'local',
	allowBlank:false,
	store:faclutystore,
    listeners:{'select': function(cmb, rec, idx){
		empSubject_store.removeAll();
	
	}}
});


function LoadData(){
	var branch_name = Ext.getCmp('branch_name').getValue();
	var semester_name = Ext.getCmp('semester_name').getValue();
	
	if(branch_name != '' && semester_name != ''){	
		Ext.getCmp('subject_name').reset();
		subjectstore.load({params:{
			branch_name:branch_name,
			semester_name:semester_name
		}});
	}}
//=========
var EmpFields = [
	{name: 'employee_id'}, 	
	{name: 'employee_name'}	
]
var Store = new Ext.data.JsonStore({
	autoDestroy: true,
    url:'data/getfaculty-name.php',
	idProperty: 'employee_id',
    storeId: 'myStore',
    root:'data',   
    fields: EmpFields,
	autoLoad: true,
    writer:new Ext.data.JsonWriter()
});


Ext.onReady(function(){
	Ext.QuickTips.init();
var colModel = new Ext.grid.ColumnModel([
		{ 
			header: "employee_id",
			dataIndex: 'employee_id', 
			sortable: true, 
			hidden: true 
		},{ 
			id: 'employee_name',
			header: "Faculty Name",
			width: 190,
			dataIndex: 'employee_name',
			align: 'left',
			sortable: true
        }
		
    ]);	
	var grid = new Ext.grid.GridPanel({
		xtype: 'grid',
		ds: Store,
		cm: colModel,
		sm: new Ext.grid.RowSelectionModel({
			singleSelect: true,
			listeners: {
				 render: {
                fn: function(){
                    	Store.reload;
					}
                },
            //*******************************************************
				rowselect: function(sm, row, rec) {
					x=rec.get('employee_id');					
					Ext.getCmp('btnDelete').enable();					
					subjectstore.load({params:{task:'getall'},'callback':function(a,b,c){
								empSubject_store.load({params:{
											employee_id:x								
											  }});	
					}});
							
					Ext.getCmp('employee_name').setValue(x);
					Ext.getCmp('assign_id').setValue("1");		// temp for edit mode
					//Ext.getCmp("EmployeeForm").getForm().loadRecord(rec);					
				}
			}        	
		}),
		height: 380,
		border: true,
		frame: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 Records.";
				}
			}
		});
  	});	
	Store.on({
	'beforeload': {
    	fn: function(Store, options){
            options.params || (options.params = {}); 
          /*  Ext.apply(options.params, {
				//group_id: Ext.getCmp('group1').getValue()
            });*/
        },
        scope: this
    }
});
	
	
	var griddata_subdetails = new Ext.form.TextField({
		name: 'griddata_subdetails',
		id:'griddata_subdetails',
		hidden:true
	});	

var empAssignSubData = Ext.data.Record.create([
		{name: 'assign_id', type: 'int'},
		{name: 'branch_id', type: 'int'},
		{name: 'semester_id', type: 'int'},
		{name: 'subject_id', type: 'int'}
		
	   ]);
empSubject_store = new Ext.data.JsonStore({
	url:'data/getfaculty-assign-sub-details.php',
	root: 'data',
	storeId: 'assign_id',
	totalProperty: 'total',
	autoLoad:false,
	fields: [
	{
		name:'assign_id',
		type:'int'
	},
	{
		name:'branch_id',
		type:'int'
	},{
		name:'semester_id',
		type:'int'
	},
	{
		name:'employee_id',
		type:'int'
	},{
		name:'subject_id',
		type:'int'
	}
	] 
  });  
  
var facultyAssignSubgrid = new Ext.grid.EditorGridPanel({	
	store: empSubject_store,
	clicksToEdit:1,
	columns: [
		new Ext.grid.RowNumberer(),
		{
			dataIndex: 'assign_id',
			header: 'Id',
			id: 'assign_id',
			hidden: true
		},{
			dataIndex: 'branch_id',
			header: 'Branch',
			sortable:true,
			id: 'branch_id',
			width:150,
			 renderer : function(val,record){									    
					var index = branchstore.find('branch_id', val, false);
				     var r = branchstore.getAt(index);
					 //console.log("destidx="+index);
				   return r ? r.data.branch_name: '';
                },
			editor:branch
		},{
			dataIndex: 'semester_id',
			header: 'Semseter',
			sortable:true,
			id: 'semester_id',
			width:150,
			 renderer : function(val,record){						
					var index = semesterstore.find('semester_id', val, false);			
				     var r = semesterstore.getAt(index);
					 //console.log("destidx="+index);
				   return r ? r.data.semester_name: '';
                },
			editor:semester
		},{
			dataIndex: 'subject_id',
			header: 'Subject',
			sortable:true,
			id: 'subject_id',
			width:270,
			 renderer : function(val,record){						
					var index = subjectstore.find('subject_id', val, false);						
				     var r = subjectstore.getAt(index);
					 //console.log("destidx="+index);
				   return r ? r.data.subject_name: '';
                },
			editor:subject
		}
	],
	height:335,
	width :652,
	frame:true,
	layout: 'fit',
	sm: new Ext.grid.RowSelectionModel({
			singleSelect: true
		}),
	loadMask: true,
	columnLines:true,
	stripeRows:true,
	 tbar: [{
             iconCls: 'icon-add',
            text: 'Add item',
			id:'btnAddItem5',
			name:'btnAddItem5',
			//tabIndex:5,
            handler: function(){				
			var e = new empAssignSubData({
					assign_id:0,
					branch_id:'',
					semester_id:'',
					subject_id:''
					
                });				
                facultyAssignSubgrid.stopEditing();				
				var insert=(facultyAssignSubgrid.store.getCount());
                facultyAssignSubgrid.store.insert(insert, e);
               // destinationgrid.getView().refresh();
                facultyAssignSubgrid.getSelectionModel().selectRow(insert);            
            }
        },{
            ref: '../removeBtn5',
            iconCls:'remove',
			//tabIndex:6,
			id:'btnRemoveItem',
			name:'btnRemoveItem',
            text: 'Delete item',
            disabled: true,
            handler: function(){
                facultyAssignSubgrid.stopEditing();
                var s = facultyAssignSubgrid.getSelectionModel().getSelections();
                for(var i = 0, r; r = s[i]; i++){
					      	 facultyAssignSubgrid.store.remove(r);
				}
				var remove=(facultyAssignSubgrid.store.getCount()-1);
				if(remove!=-1)
				{
						facultyAssignSubgrid.getSelectionModel().selectRow(remove);
				        facultyAssignSubgrid.startEditing(remove,2);
				}
				if(remove==-1)
				{
				 facultyAssignSubgrid.store.removeAll();				
				}	   
            }
        }
		]
});
facultyAssignSubgrid.getSelectionModel().on('selectionchange', function(sm){
     facultyAssignSubgrid.removeBtn5.setDisabled(sm.getCount() < 1);
    });
facultyAssignSubgrid.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 = "Click on cell To enter info.";
			}
		}

	});
});


	
	var EmployeeForm = new Ext.FormPanel({
		id: 'EmployeeForm',
    	url:'save-assign-subject.php',
		method:'post',
        frame:true,
		height:350,
		layout:'column',
       // bodyStyle:'padding:5px 5px',
		items: [
			{
				columnWidth: 0.24,
				//layout: 'fit',
				items:[grid]
			},
			{
				columnWidth: 0.76,
				autoScroll: true,
				defaults: {border:false},
				defaultType: 'textfield',
				autoHeight: true,
				bodyStyle: Ext.isIE ? 'padding:0 0 5px 5px;' : 'padding:0px 5px;',
				border: false,
				style:"margin-left:7px;",
				layout:'form',
				items:[employee_name,facultyAssignSubgrid,griddata_subdetails,
				      { 	 xtype:'hidden',
							 id:'assign_id',
							 name:'assign_id',
							 value:0
					  }]
       		}
		]		
	});
    var EmpWindow = new Ext.Window({
		id:'EmpWindow',
        title: 'Assign Subject To Faculty',
		width:940,		
		height:470,
		modal:true,
		shadow:false,
		border: false,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: EmployeeForm,
		buttonAlign: 'center',
    	constrain: true,
		buttons:[
			{
      		text: 'Save',
			iconCls:'icon-save',
			//tabIndex:28,
			handler: function(){
			
			CopyStoreToForm_bankdetail(empSubject_store);				
				EmployeeForm.getForm().submit({
					waitMsg:'Saving...',
					success:function(){
						/*Ext.MessageBox.show({
							title: 'Message',
							msg: 'Package Saved Successfully!',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.INFO
				   		});*/
						Ext.getCmp('btnDelete').disable();
				EmployeeForm.getForm().reset();
				grid.getSelectionModel().clearSelections();				
				empSubject_store.removeAll();			
				Store.reload();
				
					},
					failure: function () {		
						Ext.MessageBox.show({
							title: 'Error',
							msg: 'Please fill complete form.',
							buttons: Ext.MessageBox.OK,
							icon:Ext.MessageBox.ERROR
						});
						
					}
				});
			}
      	},{
				text: 'Delete',
				iconCls:'remove',
				name:'btnDelete',
				id:'btnDelete',
				handler: function(){ 
				Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
					
				}
		},{
		  	text: 'Reset',
			iconCls:'icon-reset',
			//tabIndex:29,
			handler: function(){
				Ext.getCmp('btnDelete').disable();
				
				EmployeeForm.getForm().reset();
              	empSubject_store.removeAll();
				grid.getSelectionModel().clearSelections();
     			Ext.getCmp('assign_id').setValue('0');	
				Store.load();
				
			}	
        },
		{
        	text: 'Close',
			iconCls:'icon-close',
			//tabIndex:30,
			handler: function () {
				
				EmpWindow.close();	
			}
        }]
	});
		
		  function CopyStoreToForm_bankdetail(empSubject_store){
			 
					  var json = '[';
					  empSubject_store.each(function(empSubject_store){
						  json += Ext.util.JSON.encode(empSubject_store.data) + ',';
					  });
					  json = json.substring(0, json.length - 1);
					  json += ']';
					  Ext.getDom('griddata_subdetails').value = json
			  };	
			  	
			  	  
	function deleterecord(btn){
    	if(btn=='yes'){
			EmployeeForm.getForm().submit({
						waitMsg: 'Deleting...',
						params:{act:'del'},
						success: function () {					
				 empSubject_store.removeAll();
				grid.getSelectionModel().clearSelections();
				Ext.getCmp('btnDelete').disable();
				Store.load();
				EmployeeForm.getForm().reset();																
						}
					});
	  		
	  }}
	 
	 
 
					 																								
	Ext.getCmp('btnDelete').disable();  
	var varWidth =  (screen.width - EmpWindow.width)/2;
	EmpWindow.setPosition(varWidth,((screen.height/4)-85));
		
	EmpWindow.show();
	//get_supcode();
	//Ext.getCmp('type_name1').focus(true,500);

});
     

</script>
<!-- end custom CSS/JS -->`

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