<?php 
	require "_header.php"; 
	require "data/combo_student.php";
	require "data/combo_semester.php";
	require "data/combo_branchyear.php";
?>
<div id="main-content">
<script type="text/javascript">

var labelArray;
		labelArray = [
			['Course <span class="comp">*</span>'],
			['Batch Year <span class="comp">*</span>'],
			['Course Year <span class="comp">*</span>'],
			['Semester <span class="comp">*</span>'],
			['Subject ']
		];


var Upload_Fields = [
	{name:'upload_id',type: 'int'},
	{name:'branch_id',type: 'int'},
	{name:'branch_name',type: 'string'},
	{name:'year_id',type: 'int'},
	{name:'year_name',type: 'string'},
	{name:'branchyear_id',type: 'int'},
	{name:'branchyear_name',type: 'string'},
	{name:'semester_id',type: 'int'},
	{name:'semester_name',type: 'string'},
	{name:'subject_id',type: 'int'},
	{name:'subject_name',type: 'string'},
	{name:'title'},
	{name:'description'},
	{name:'file_name'},
	{name:'assignment_date'},
	{name:'submission_date'},
	{name:'active'}
]

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



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

Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	{ 
		header: "Course",
		width: 60,
		dataIndex: 'branch_name',
		sortable: true
  	},{ 
		header: "Batch Year",
		width: 70,
		dataIndex: 'year_name',
		sortable: true
  	},
	  { 
		header: "Course Name",
		width: 70,
		dataIndex: 'branchyear_name',
		sortable: true
  	},{ 
		header: "Semester",
		width: 70,
		dataIndex: 'semester_name',
		sortable: true
  	},
	{ 
		header: "Subject",
		width:150,
		dataIndex: 'subject_name',
		sortable: true
  	},
	{ 
		header: "Chapter",
		width:250,
		dataIndex: 'title',
		sortable: true
  	},{ 
		header: "FileName",
		width:101,
		dataIndex: 'file_name',
		sortable: true
  	},
	{ 
		header: "Ass Date",
		width:90,
		dataIndex: 'assignment_date',
		sortable: true
  	},
	{ 
		header: "Sub Date",
		width:90,
		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,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
                Ext.getCmp("semesterform").getForm().loadRecord(rec);
				Ext.getCmp("old_upload_file").setValue(rec.data.file_name);
				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: 320,
		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 batchyearstore = new Ext.data.ArrayStore({
			fields: ['year_id', 'year_name'],
			data: Ext.semester.yearname
		});

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

		var yearstore = new Ext.data.ArrayStore({
			fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
			data: Ext.year.year
		});


		var subjectstore = new Ext.data.JsonStore({
				url:'data/get-subject.php',
   			 	root: 'data',
				totalProperty: 'total',
				fields: ['subject_id','subject_name'] 
				});
				subjectstore.load({params:{task:'getall'}});
		
	
	var semesterform = new Ext.FormPanel({	
		id: 'semesterform',
		frame: true,
		url: 'save-upload-material.php',
    	labelAlign: 'left',
    	layout:'column',      
		fileUpload: true,       
		items :[{
			columnWidth: 0.31,
			layout: 'form',
			labelWidth:85,
			items:[{
					xtype: 'combo',
					name: 'branch_id',
					id: 'branch_id',
					hiddenName: 'branch_hidden',
					fieldLabel: labelArray[0],
					displayField: 'branch_name',
					valueField: 'branch_id',
					width: 180,
					triggerAction: 'all',
					mode: 'local',
					allowBlank: true,
					store: branchstore,
					listeners: {
						'select': function (cmb, rec, idx) {
					//		Ext.getCmp('branch_disp').setValue(this.getRawValue());
							Ext.getCmp('semester_id').reset();
							Ext.getCmp('year_id').reset();
							Ext.getCmp('branchyear_id').reset();
							
							
							semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
							yearstore.filter('branch_id', cmb.getValue(), false, true, true);
							var semesterCombo = Ext.getCmp('semester_id');
							if (cmb.getValue() == 3 || cmb.getValue() == 5) {
								semesterCombo.enable();
							} else {
								semesterCombo.disable();
								semesterCombo.reset();
							}
						}
					}
				}, {
					xtype: 'combo',
					name: 'year_id',
					id: 'year_id',
					hiddenName: 'year_hidden',
					fieldLabel: labelArray[1],
					displayField: 'year_name',
					valueField: 'year_id',
					lastQuery: '',
					triggerAction: 'all',
					mode: 'local',
					width: 180,
					allowBlank: true,
					store: batchyearstore,
					
				}, {
					xtype: 'combo',
					name: 'branchyear_id',
					id: 'branchyear_id',
					hiddenName: 'branchyear_hidden',
					fieldLabel: labelArray[2],
					displayField: 'branchyear_name',
					valueField: 'branchyear_id',
					lastQuery: '',
					triggerAction: 'all',
					mode: 'local',
					width: 180,
					allowBlank: true,
					store: yearstore,
					listeners: {
						'select': function (cmb, rec, idx) {
							//Ext.getCmp('branchyear_disp').setValue(this.getRawValue());
							if (Ext.getCmp('branch_id').getValue() != "") {
								
							} else {
								alert('Select Branch First')
								this.reset();
							}
						}
					}
				}, {
					xtype: 'combo',
					name: 'semester_id',
					id: 'semester_id',
					hiddenName: 'semester_hidden',
					fieldLabel: labelArray[3],
					displayField: 'semester_name',
					valueField: 'semester_id',
					lastQuery: '',
					disabled: true,
					triggerAction: 'all',
					mode: 'local',
					width: 180,
					allowBlank: true,
					store: semesterstore,
					listeners: {
						'select': function (cmb, rec, idx) {
						//	Ext.getCmp('semester_disp').setValue(this.getRawValue());
							if (Ext.getCmp('branch_id').getValue() != "") {
								
							} else {
								alert('Select Branch First')
								this.reset();
							}
						}
					}
				},{
					xtype: 'combo',
					name: 'subject_id',
					id: 'subject_id',
					hiddenName: 'subject_hidden',
					fieldLabel: labelArray[4],
					displayField: 'subject_name',
					valueField: 'subject_id',
					triggerAction: 'all',
					mode: 'local',
					width: 180,
					allowBlank: true,
					store: subjectstore
					
				}
		   ,
					{
						xtype: 'textfield',
						labelSeparator:'',	
						fieldLabel :'Chapter :<span class="comp">*</span>' ,
						name: 'title',
						id: 'title',
						tabIndex:2,
						width:182,
						allowBlank:false
					},{
						xtype: 'fileuploadfield',
						labelSeparator:'',	
						fieldLabel :'File:<span class="comp"></span>' ,
						name: 'file_name',
						id: 'file_name',
						width:182,
						allowBlank:true,
						tabIndex:3
					},{
						xtype: 'checkbox',
						labelSeparator:'',	
						fieldLabel :'Active' ,
						name: 'active',
						checked:true,
						id: 'active',
						tabIndex:2
					}]},
		   {
			columnWidth: 0.69,
			layout: 'form',
			labelWidth:75,
			style:'padding:0px 5px',
			items:[{
						xtype: 'htmleditor',
						labelSeparator:'',	
						fieldLabel :'Chapter Description' ,
						name: 'description',
						id: 'description',
						width:500,
						height:125,
						tabIndex:3
					},
			{columnWidth: 1,
			layout: 'column',
			items:[{
					columnWidth: 0.5,
					layout: 'form',
					labelWidth:75,
					items:[{
								xtype: 'datefield',
								labelSeparator:'',	
								fieldLabel :'Ass Date' ,
								name: 'assignment_date',
								id: 'assignment_date',
								width:185,
								tabIndex:2
							}]
				},{
					columnWidth: 0.5,
					layout: 'form',
					labelWidth:75,
					items:[{
						xtype: 'datefield',
						labelSeparator:'',	
						fieldLabel :'Sub. Date' ,
						name: 'submission_date',
						id: 'submission_date',
						width:185,
						tabIndex:2
					}]
				}]
			},{
						xtype: 'textfield',
						name: 'upload_id',
						id:'upload_id',
						value:'0',
						hidden:true
					},{
						xtype: 'textfield',
						name: 'old_upload_file',
						id:'old_upload_file',
						value:'0',
						hidden:true
					}]},{
			columnWidth: 1,
			layout: 'form',
			labelWidth:75,
			style:'padding:0px 5px',
			items:[grid]}]
    });
	

	var SemesterWindow = new Ext.Window({
		title: 'Upload Material',
		width:910,		
		height:550,	
		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(){
					var flage =0;
					if(Ext.getCmp('file_name').getValue() !='')
					{
						var res = validate();
						if(res)
						{
							flage=1;
						}
						else {
							flage=0;
						}
					}else{
						flage= 1;
					}
					if(flage == 1){	
						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();
				
					UploadStore.load();	
					//Ext.getCmp('branch_id').focus(true,500);								
				}
			});
	 	}  
  	}
	
	var varWidth =  (screen.width - SemesterWindow.width)/2;
	SemesterWindow.setPosition(varWidth,((screen.height/4)-170));
	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","doc","DOC","docx","DOCX","ppt","pptx","ppsx","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>
</div>
<?php require "_footer.php";?>
