<?php require "_header.php";
	
	$employee_id = Escape($_REQUEST["ID"]);
	
	if ($employee_id !="" && isset($employee_id)){
		$mode="edit";
		if($EDIT == "false")
		{
			 redirectToPage();
		}
		$limit=1;
		$start=0;
		$condition="";
		$order_field="";
		$order_type="";

		if ($employee_id!="") {
           $condition .= " and employee_id='$employee_id' ";
    	}
				
		$objEmployee = new EmployeeMgmt();
		$out = $objEmployee->getAllRecords($limit,$start,$condition,$order_field,$order_type);
		if (dbRows($out)){
			$row=dbArray($out);
			extract($row);			
		}
		
		$out = $objEmployee->getAllRecords($limit,$start,$condition,$order_field,$order_type);
		if (dbRows($out)){
			$row=dbArray($out);
			extract($row);			
		}
		    	
	}	
	else{
		$mode="add";
		if ($ADD == "false")
		{
			 redirectToPage();
		}
		$employee_id=0;
	}
?>
<!-- start custom CSS/JS -->
<script language="javascript" type="text/javascript">
var formMode = '<?php echo $mode;?>';
var employee_id = '<?php  echo $employee_id; ?>';
var add = '<?php echo $_REQUEST["add"]; ?>';
var labelArray;
labelArray = [
	['Employee Name:<span class="comp">*</span>'],
	['Contact Address'],
	['Contact No(R)'],
	['Contact No(M)'],
	['Email'],
	['Designation:<span class="comp">*</span>'],
	['Birth Date:<span class="comp">*</span>'],
	['Basic Salary:<span class="comp">*</span>'],
	['Joining Date:<span class="comp">*</span>']
];

if(employee_id==""){
	employee_id = 0;
}
   
Ext.onReady(function(){
	
   	Ext.QuickTips.init();
	
	var dt = new Date();
	date = dt.getDate() - 1;
	dt.setDate(date);
 	
	var designationstore = new Ext.data.Store
	({
		reader: new Ext.data.JsonReader
		({
			fields:['designation_id','designation_name'],
			root: 'rows'
		}),
		proxy: new Ext.data.HttpProxy
		({
			url: 'data/getdesignation.php'
		}),autoLoad:true
		
	});

	
	var picFiles = { 
		columnWidth: .8,
		layout: 'form',
		labelAlign:'top',
		items: [
				{
					xtype: 'textfield',
					fieldLabel: 'Current',
					labelSeparator: '',
					name: 'currPic',
					id:'currPic',
					readOnly: true,
					disabled:true,
					anchor:'100%'
				},
				{
					xtype: 'textfield',
					fieldLabel: 'New (JPG or JPEG only)',
					labelSeparator: '',
					name: 'newPic',
					id:'newPic',
					style:'width: 300px',
					inputType: 'file',
					allowBlank: false
				}]
	}

	var pictUploadForm = new Ext.FormPanel({
		   frame: true,
		  	bodyStyle: 'padding:5px',
		   width: 415,
		   layout: 'column',
		   url:'empphotoupload.php',
		   method: 'POST',
		   fileUpload: true,
		   items: [picFiles],
		   buttons: [{
			  text: 'Upload Picture',
			  handler: function() {
				 var theForm = pictUploadForm.getForm();
				 if (!theForm.isValid()) {
				    	Ext.MessageBox.show({
											   title: 'Change Picture',
											   msg: 'Please select a picture',
											   buttons: Ext.MessageBox.OK,
											   icon:Ext.MessageBox.ERROR
									  });
					
					return;
				 }
				 if (!validateFileExtension(Ext.getDom('newPic').value)) {
				       Ext.MessageBox.show({
											   title: 'Change Picture',
											   msg: 'Only JPG or JPEG please.',
											   buttons: Ext.MessageBox.OK,
											   icon:Ext.MessageBox.ERROR
									  });
					
					return;
				 }
				 theForm.submit({		 
					params: { act: 'setPicture', id:employee_id},
					waitMsg: 'Uploading picture'
				 });
			   }
			},
			{
			  text: 'Reset',
			  handler: function () {
						// when this button clicked, reset this form
						pictUploadForm.getForm().reset();
					}
			},{
			  text: 'Close',
			  handler: function () {
						
						window.location = 'addedit-employee.php'; 
						
						
					}
			}]
	});
  
	pictUploadForm.on({
    actioncomplete: function(form, action) {
					if (action.type == 'load') {
						var pic = action.result.data;			
						Ext.getDom('pic').src = pic.file;
						Ext.getCmp('currPic').setValue(pic.file);
					}
					if (action.type == 'submit') {
						var pic = action.result.data;
						Ext.getDom('pic').src = pic.file;
						employeeentry.getForm().findField('employee_photo').setValue(pic.photoname);
						Ext.getCmp('currPic').setValue(pic.file);
						Ext.getDom('newPic').value = '';
						win.hide();
					}
			}
	});
	
	pictUploadForm.getForm().load({ 
						  	 params:{ act:'getPicture',id:employee_id,photoname:'<?php print $employee_photo; ?>'}, 
						  	 waitMsg: 'Loading'
						  });
						  
  	var win = new Ext.Window({
        title: 'Change Picture',
        height: 180,
        width: 415,
		modal:true,
        layout: 'fit',
		closeAction:'close',
		border: false,
     	autoDestroy :true,
		items: pictUploadForm
    });
		 		
	var employeeentry = new Ext.FormPanel({
    	labelWidth: 115,
		url:'save-employee.php',
		method:'post',
        frame:true,
		autoHeight: true,
        bodyStyle:'padding: 5px 5px 0',
	    items:[
			{
				layout:'column',
				items:[
					{
						columnWidth: .6,
						layout: 'form',					
						items:[
							{
								xtype:'textfield',
								name: 'employee_name',
								id:'employee_name',
								tabIndex:1,
								allowBlank:false,
								maxLength:50,
								width:158,
								labelSeparator:'',	
								fieldLabel :labelArray[0],
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{ 
								xtype: 'textarea',
								growAppend: "",
								name: 'employee_address',
								id:'employee_address',
								fieldLabel: labelArray[1],
								maxLength: 200,
								tabIndex:2,
								maxLengthText: 'max 200 characters',
								width: 158
							},
							{
								xtype: 'textfield',
								name: 'employee_phone',
								id: 'employee_phone',
								tabIndex:3,
								minLength:10,
								maxLength:13,
								fieldLabel: labelArray[2],
								width:158,
								autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
								,enableKeyEvents: true
								,listeners:{
									keyup: validphoneno
								}
							},
							{
								xtype: 'numberfield',
								name: 'employee_mobile',
								id: 'employee_mobile',
								minLength:10,
								maxLength:13,
								tabIndex:4,
								fieldLabel: labelArray[3],
								width:158,
								autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
							},
							{
								xtype:'textfield',
								name: 'employee_email',
								fieldLabel: labelArray[4],
								width:158,
								tabIndex:5,
								vtype:'email',
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{
								xtype:'combo',
								name:'employee_designation',
								id:'employee_designation',
								labelSeparator:'',	
								hiddenName: 'employeedesignation',
								fieldLabel :labelArray[5] ,			
								displayField:'designation_name',
								valueField: 'designation_id',
								width:158,										
								tabIndex:6,
								allowBlank:false,
								triggerAction:'all',
								mode:'local',
								store: designationstore,
								editable:true,
								forceSelection:true
							},				
							{
								xtype: 'datefield',
								name: 'employee_birthdate',
								id:'employee_birthdate',
								labelSeparator:'',	
								fieldLabel :labelArray[6] ,
								tabIndex:7,
								allowBlank:false,
								width:158,
								maxValue:dt								
							}
						]				
					},						   
					{
						columnWidth: .4,
						layout: 'form',
						height:157,
						style: 'padding:0px 0px 0px 85px',							
						items:[
							{
								xtype: 'box',										       
								autoEl: 
									{ 
										tag: 'div',
										html: '<img id="pic" src="photo.gif" alt="photograph" class="img-contact" width="<?php echo $SiteImageWidth ?>" height="<?php echo $SiteImageHeight ?>"/>'
									}
							},
							{
								style: 'padding:0px 0px 0px 0px',
								xtype: 'button',
								tabIndex:10,											
								text: 'Upload',
								width:100,
								handler:function(){
									win.show();
									Ext.getDom('newPic').size = 30;																						
								}
							}
						]				
					},
					{
						columnWidth: .4,
						layout: 'form',					
						labelWidth:80,
						items:[
							{
								xtype: 'numberfield',
								name: 'employee_salary',
								id:'employee_salary',
								tabIndex:8,
								labelSeparator:'',	
								fieldLabel :labelArray[7] ,
								width:100,
								allowBlank:false,
								autoCreate: {tag: 'input', type: 'text',  maxlength: '10'}
							},
							{
								xtype: 'datefield',
								name: 'employee_joindate',
								id:'employee_joindate',
								tabIndex:9,
								labelSeparator:'',	
								fieldLabel :labelArray[8] ,
								allowBlank:false
								,width:100								    					
							},
							{
					  			xtype: 'textfield',
								name: 'employee_id',
								id:'employee_id',
								hidden:true,
								value:employee_id
							},
							{
					  			xtype: 'textfield',
								name: 'employee_photo',
								id:'employee_photo',
								hidden:true
							}
						]        				
					}
									 
				]
			}
		]
	});
    
	var EmployeeWindow = new Ext.Window({
		title: 'Employee Information',
		width:545,		
		height:390,
		modal:true,
		shadow:false,
		autoHeight:true,
		border: false,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: employeeentry,
		buttonAlign: 'center',
	    buttons:[
			{
            	text: 'Save',
				tabIndex:11,
				iconCls: 'icon-save',		
				handler: function () {
					employeeentry.getForm().submit({
						waitMsg: 'Saving...',		
						success: function () {
										
								  Ext.MessageBox.show({
										   title: 'Message',
										   msg: 'Data has been saved',
										   buttons: Ext.MessageBox.OK,
										   icon:Ext.MessageBox.INFO
										  });	
										
								if(formMode=='edit'){
									location.href="view-employee.php";
								} else{
									employeeentry.getForm().reset();
									Ext.getDom('pic').src = '<?php print $SiteImagePath."default.gif"; ?>';
								}		
								
												 							    							 
						},
						failure: function () {		
							Ext.MessageBox.show({
										   title: 'Error',
										   msg: 'Please fill complete form.',
										   buttons: Ext.MessageBox.OK,
										   icon:Ext.MessageBox.ERROR
										  });
						}
					});
				}
          	},
		  	{
		    	text: 'Reset',
				tabIndex:12,
				iconCls: 'icon-reset',
				handler: function () {
					if(formMode!='edit'){
						employeeentry.getForm().reset();
					}
				}	
        	},
			{
            	text: 'Close',
				iconCls: 'icon-close',
				tabIndex:13,
				handler: function(){
					if(add!=1){
						location.href="view-employee.php";
					}
					else{
						EmployeeWindow.close();
					}
				}
        	}
		]
	});
	
	var varWidth =  (screen.width - EmployeeWindow.width)/2;
	EmployeeWindow.setPosition(varWidth,((screen.height/4)-110));
	EmployeeWindow.show();
	
	
	if(formMode=='edit'){
		employeeentry.getForm().findField('employee_name').setValue('<?php print $employee_name; ?>');
		employeeentry.getForm().findField('employee_address').setValue('<?php print replaceSpecial($employee_address); ?>');
		employeeentry.getForm().findField('employee_phone').setValue('<?php print $employee_phone; ?>');
		employeeentry.getForm().findField('employee_mobile').setValue('<?php print $employee_mobile; ?>');
		employeeentry.getForm().findField('employee_email').setValue('<?php print $employee_email; ?>');
		designationstore.on({'load':{
			fn: function(store, options){
				employeeentry.getForm().findField('employee_designation').setRawValue('<?php print $employee_designation; ?>');
				employeeentry.getForm().findField('employee_designation').setValue('<?php print $designation_id; ?>');	
			}
		}});		
		employeeentry.getForm().findField('employee_birthdate').setValue('<?php print $employee_birthdate; ?>');
     	employeeentry.getForm().findField('employee_salary').setValue('<?php print $employee_salary; ?>');
		employeeentry.getForm().findField('employee_joindate').setValue('<?php print $employee_joindate; ?>');
	}
});
</script>
<!-- end custom CSS/JS -->`
<div align="center"><div id="main-content" style="width:680px" align="left"></div></div> 
<?php require "_footer.php";?>