<?php 
	require "_header.php"; 
	require "data/combo_semester.php";
?>
<script type="text/javascript">
var Upload_Fields = [
	{name:'upload_id',type: 'int'},
	{name:'branch_id',type: 'int'},
	{name:'branch_name',type: 'string'},
	{name:'branch_sem_name',type: 'string'},
	{name:'semester_id',type: 'int'},
	{name:'semester_name',type: 'string'},
	{name:'subject_id',type: 'int'},
	{name:'subject_name',type: 'string'},
	{name:'upload_title'},
	{name:'title'},
	{name:'description'},
	{name:'active'},
	{name:'file_title'},
	{name:'file_name'},
	{name:'upload_file_id'},
	{name:'assignment_date'},
	{name:'submission_date'}
]

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 reader = new Ext.data.JsonReader({
		root: 'data',
		idProperty: 'upload_id',
		fields: [{name:'upload_id',type: 'int'},
				{name:'branch_id',type: 'int'},
				{name:'branch_name',type: 'string'},
				{name:'branch_sem_name',type: 'string'},
				{name:'semester_id',type: 'int'},
				{name:'semester_name',type: 'string'},
				{name:'subject_id',type: 'int'},
				{name:'subject_name',type: 'string'},
				{name:'upload_title'},
				{name:'title'},
				{name:'description'},
				{name:'active'},
				{name:'file_title'},
				{name:'file_name'},
				{name:'upload_file_id'},
				{name:'assignment_date'},
				{name:'submission_date'}			 
				 ]

    });
    
	
var UploadStore =new Ext.data.GroupingStore({
    url: 'data/upload-files-grid-data.php',
   	reader: reader,
	autoLoad:true,
	successProperty: 'success',
	sortInfo: { field: 'branch_id', direction: 'ASC' },
	groupField: 'branch_sem_name'
});

Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	{ 
		header: "Semester",
		width: 70,
		dataIndex: 'branch_sem_name',
		sortable: true,
		hidden:true
  	},
	{ 
		header: "Subject",
		width:185,
		dataIndex: 'subject_name',
		sortable: true
  	},
	{ 
		header: "Title",
		width:110,
		dataIndex:'upload_title',
		sortable: true
  	},
	{ 
		header: "FileTitle",
		width:110,
		dataIndex: 'title',
		sortable: true
  	},
	{ 
		header: "FileName",
		width:101,
		dataIndex: 'file_name',
		sortable: true
  	},
	{ 
		header: "Ass Date",
		width:101,
		dataIndex: 'assignment_date',
		sortable: true
  	},
	{ 
		header: "Sub Date",
		width:101,
		dataIndex: 'submission_date',
		sortable: true
  	},
	{ 
		header: "Active",
		width:45,
		dataIndex: 'active',
		sortable: true,
		renderer: function(v, params, record){
				 if(v == 1){
						return 'Yes';
					}else{
						return 'No';
					}
				
				}
  	}
	]);

	
	var grid = new Ext.grid.GridPanel({
   		store: UploadStore,
        cm: colModel,
		columnLines:true,
		stripeRows:true,
		loadMask:true,
		view: new Ext.grid.GroupingView({
					forceFit: true,
					showGroupName: false,
					enableNoGroups: false,
					enableGroupingMenu: false,
					//hideGroupedColumn: true
				}),
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                Ext.getCmp("semesterform").getForm().loadRecord(rec);
				Ext.getCmp('upload_file_id').setValue(rec.data.upload_file_id);
				var branch_name = rec.data.branch_id
				var semester_name = rec.data.semester_id
				if(branch_name != '' && semester_name != ''){	
				subjectstore.load({params:{branch_name:branch_name,semester_name:semester_name}});
				}
				var subject_id = rec.data.subject_id
				if(branch_name != '' && semester_name != '' && subject_id!=''){	
					titlestore.load({params:{branch_name:branch_name,semester_name:semester_name,subject_id:subject_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();
					 }
					 
					Ext.getCmp('subject_id').setValue(rec.data.subject_id);
					Ext.getCmp('subject_id').setRawValue(rec.data.subject_name);

					Ext.getCmp('upload_id').setValue(rec.data.upload_id);
					Ext.getCmp('upload_id').setRawValue(rec.data.upload_title);
				
				}
         	}
   		}),
		height: 380,
		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 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.semester
	});
	
	var subjectstore = new Ext.data.JsonStore({
	url:'data/get-subject.php',
    root: 'data',
	totalProperty: 'total',
	fields: ['subject_id','subject_name'] 
	});
	
	var titlestore = new Ext.data.JsonStore({
	url:'data/get-upload.php',
    root: 'data',
	totalProperty: 'total',
	fields: ['upload_id','title'] 
	});
	
	var branch = new Ext.form.ComboBox({
		name: 'branch_id',
		id:'branch_id',
		hiddenName:'branch_hidden',
		labelSeparator:'',	
		fieldLabel :'Branch :<span class="comp">*</span>' ,
		displayField:'branch_name',
		valueField: 'branch_id',
		editable:true,
		triggerAction:'all',
		tabIndex:1,
		mode:'local',
		allowBlank:false,
		store: branchstore,
		listeners: {'select': function(cmb, rec, idx){
						
		}}
	});

	var semester = new Ext.form.ComboBox({
		name: 'semester_id',
		id:'semester_id',
		hiddenName:'semester_hidden',
		labelSeparator:'',	
		fieldLabel :'Semester: <span class="comp">*</span>' ,
		displayField:'semester_name',
		valueField: 'semester_id',
		editable:true,
		tabIndex:2,
		triggerAction:'all',
		mode:'local',
		allowBlank:false,
		store: semesterstore,
		listeners: {'select': function(cmb, rec, idx){
		
		var branch_name = Ext.getCmp('branch_id').getValue();
		var semester_name = Ext.getCmp('semester_id').getValue();
		if(branch_name != '' && semester_name != ''){	
		Ext.getCmp('subject_id').reset();
		subjectstore.load({params:{branch_name:branch_name,semester_name:semester_name}});
		}
			
		}}
	});
	
	var subject = new Ext.form.ComboBox({
		name: 'subject_id',
		id:'subject_id',
		hiddenName:'subject_hidden',
		labelSeparator:'',	
		fieldLabel :'Subject: <span class="comp">*</span>' ,
		displayField:'subject_name',
		valueField: 'subject_id',
		editable:true,
		triggerAction:'all',
		mode:'local',
		tabIndex:2,
		allowBlank:false,
		store: subjectstore,
		listeners: {'select': function(cmb, rec, idx){
		
		var branch_name = Ext.getCmp('branch_id').getValue();
		var semester_name = Ext.getCmp('semester_id').getValue();
		var subject_id = Ext.getCmp('subject_id').getValue();
			if(branch_name != '' && semester_name != '' && subject_id!=''){	
				Ext.getCmp('upload_id').reset();
				titlestore.load({params:{branch_name:branch_name,semester_name:semester_name,subject_id:subject_id}});
			}
			
			}
		}
		
	});
	
	var upload = new Ext.form.ComboBox({
		name: 'upload_id',
		id:'upload_id',
		hiddenName:'upload_hidden',
		labelSeparator:'',	
		fieldLabel :'Upload Title:<span class="comp">*</span>' ,
		displayField:'title',
		valueField: 'upload_id',
		editable:true,
		triggerAction:'all',
		mode:'local',
		tabIndex:2,
		allowBlank:false,
		store: titlestore
	});
	
	var semesterform = new Ext.FormPanel({	
		id: 'semesterform',
		frame: true,
		url: 'save-upload-files.php',
    	labelAlign: 'left',
    	layout:'column',   
		fileUpload: true,     
		items :[{
			columnWidth: 0.70,
			layout: 'form',
			labelWidth:85,
			items:[grid]},
		   {
			columnWidth: 0.30,
			layout: 'form',
			labelWidth:80,
			style:'padding:0px 5px',
			items:[branch,semester,subject,upload,
					{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :'Doc Title' ,
						name: 'title',
						id: 'title',
						tabIndex:2,
						width:185
					},{
						xtype: 'fileuploadfield',
						labelSeparator:'',	
						fieldLabel :'File:<span class="comp">*</span>' ,
						name: 'file_name',
						id: 'file_name',
						allowBlank:false,
						tabIndex:3
					},{
						xtype: 'checkbox',
						labelSeparator:'',	
						fieldLabel :'Active' ,
						name: 'active',
						checked:true,
						id: 'active',
						tabIndex:2
					},
					{
						xtype: 'datefield',
						labelSeparator:'',	
						fieldLabel :'Ass Date' ,
						name: 'assignment_date',
						id: 'assignment_date',
						width:185,
						tabIndex:2
					}
					,{
						xtype: 'datefield',
						labelSeparator:'',	
						fieldLabel :'Sub. Date' ,
						name: 'submission_date',
						id: 'submission_date',
						width:185,
						tabIndex:2
					},
					{
						xtype: 'textfield',
						name: 'upload_file_id',
						id:'upload_file_id',
						//value:'0',
						hidden:true
					}]}]
    });
	

	var SemesterWindow = new Ext.Window({
		title: 'Upload Files',
		width:1040,		
		height:450,	
		frame: true,
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: semesterform,
		buttons:[
			{
				text: 'Save', 
				name:'btnSave',
				tabIndex:4,
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
					validate();
				semesterform.getForm().submit({
										waitMsg: 'Saving...',
										success: function () {	
											grid.getSelectionModel().clearSelections();
											Ext.getCmp('btnDelete').disable();	
											UploadStore.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
											});									
										}
									});		
				}	
			},
			{
				text: 'Delete',
				iconCls:'remove',
				disabled:true,
				id: 'btnDelete',
				tabIndex:5,
				name: 'btnDelete',
				handler: function(){
					Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
				}
			},
			{
				text: 'Reset', 
				name:'btnreset',
				tabIndex:6,
				iconCls:'icon-reset',
				id:'btnreset',
				handler: function (){
					semesterform.getForm().reset();
					grid.getSelectionModel().clearSelections();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('branch_id').focus(true,500);
					if(flgAdd == "true"){
									Ext.getCmp('btnSave').enable();
					 }
				}
			},
			{
				text:'Close', 
				name:'btnclose',
				tabIndex:7,
				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');	
					UploadStore.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);

});
function validate()
{
	var fruits = ["txt", "TXT","xls","XLS","pdf","PDF","jpg", "JPG", "gif", "gif","png", "PNG", "jpeg", "JPEG","doc","DOC","docx","DOCX","ppt","pptx","flv"];
	var f_ext = document.getElementById("file_name").value.match(/(.{3}$)/)[1];	
	
	var a = fruits.indexOf(f_ext); 
	if(a == -1)
	{
		alert(" Invalid file format, please try again!");
		document.getElementById("file_name").value='';
		document.getElementById("file_name").focus();
		return false;
	}
	else
	{
		return true;
	}
}
</script>
<div id="abcd"></div>
<?php require "_footer.php";?>
