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

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

if(operator_id==""){
	operator_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:operator_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('operator_photo').setValue(pic.photoname);
						Ext.getCmp('currPic').setValue(pic.file);
						Ext.getDom('newPic').value = '';
						win.hide();
					}
			}
	});
	
	pictUploadForm.getForm().load({ 
						  	 params:{ act:'getPicture',id:operator_id,photoname:'<?php print $operator_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-operator.php',
		method:'post',
        frame:true,
		autoHeight: true,
        bodyStyle:'padding: 5px 5px 0',
	    items:[
			{
				layout:'column',
				items:[
					{
						columnWidth: .6,
						layout: 'form',					
						items:[
							{
								xtype:'textfield',
								name: 'operator_code',
								id:'operator_code',
								tabIndex:1,
								allowBlank:false,
								maxLength:50,
								width:158,
								labelSeparator:'',	
								fieldLabel :'Operator Code',
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{
								xtype:'textfield',
								name: 'operator_name',
								id:'operator_name',
								tabIndex:1,
								allowBlank:false,
								maxLength:50,
								width:158,
								labelSeparator:'',	
								fieldLabel :labelArray[0],
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{ 
								xtype: 'textarea',
								growAppend: "",
								name: 'operator_address',
								id:'operator_address',
								fieldLabel: labelArray[1],
								maxLength: 200,
								tabIndex:2,
								maxLengthText: 'max 200 characters',
								width: 158
							},
							{
								xtype: 'textfield',
								name: 'operator_phone',
								id: 'operator_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: 'operator_mobile',
								id: 'operator_mobile',
								minLength:10,
								maxLength:13,
								tabIndex:4,
								fieldLabel: labelArray[3],
								width:158,
								autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
							},
							{
								xtype:'textfield',
								name: 'operator_email',
								id: 'operator_email',
								fieldLabel: labelArray[4],
								width:158,
								tabIndex:5,
								vtype:'email',
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{
								xtype:'textfield',
								name: 'bank_name',
								id: 'bank_name',
								fieldLabel: 'Bank Name',
								width:158,
								tabIndex:5,
								
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{
								xtype:'textfield',
								name: 'branch_name',
								id: 'branch_name',
								fieldLabel: 'Barnch Name',
								width:158,
								tabIndex:5,
								
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{
								xtype:'textfield',
								name: 'operator_panno',
								id: 'operator_panno',
								fieldLabel: labelArray[5],
								width:158,
								tabIndex:5,
							//	vtype:'email',
								autoCreate: {tag: 'input', type: 'text',  maxlength: '20'}
							},			
							{
								xtype:'textfield',
								name: 'operator_aadharno',
								id: 'operator_aadharno',
								fieldLabel: labelArray[6],
								width:158,
								tabIndex:5,
							//	vtype:'email',
								autoCreate: {tag: 'input', type: 'text',  maxlength: '12'}
							},
						]				
					},						   
					{
						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: 'operator_otherid',
								id:'operator_otherid',
								tabIndex:8,
								labelSeparator:'',	
								fieldLabel :labelArray[7] ,
								width:100,
								allowBlank:true,
								autoCreate: {tag: 'input', type: 'text',  maxlength: '10'}
							},
							{
								xtype:'textfield',
								name: 'account_no',
								id: 'account_no',
								fieldLabel: 'Account No',
								width:158,
								tabIndex:5,
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{
								xtype:'textfield',
								name: 'ifsc_code',
								id: 'ifsc_code',
								fieldLabel: 'IFSC No',
								width:158,
								tabIndex:5,
								autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							},
							{
								xtype: 'datefield',
								name: 'operator_joindate',
								id:'operator_joindate',
								tabIndex:9,
								labelSeparator:'',	
								fieldLabel :labelArray[8] ,
								allowBlank:true
								,width:100								    					
							},
							{
					  			xtype: 'textfield',
								name: 'operator_id',
								id:'operator_id',
								hidden:true,
								value:operator_id
							},
							{
					  			xtype: 'textfield',
								name: 'operator_photo',
								id:'operator_photo',
								hidden:true
							}
						]        				
					}
									 
				]
			}
		]
	});
    
	var EmployeeWindow = new Ext.Window({
		title: 'Operator Information',
		width:645,		
		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-operator.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-operator.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('operator_name').setValue('<?php print $operator_name; ?>');
		employeeentry.getForm().findField('operator_address').setValue('<?php print replaceSpecial($operator_address); ?>');
		employeeentry.getForm().findField('operator_phone').setValue('<?php print $operator_phone; ?>');
		employeeentry.getForm().findField('operator_mobile').setValue('<?php print $operator_mobile; ?>');
		employeeentry.getForm().findField('operator_email').setValue('<?php print $operator_email; ?>');
		employeeentry.getForm().findField('operator_panno').setValue('<?php print $operator_panno; ?>');	
		employeeentry.getForm().findField('operator_aadharno').setValue('<?php print $operator_aadharno; ?>');
     	employeeentry.getForm().findField('operator_otherid').setValue('<?php print $operator_otherid; ?>');
		employeeentry.getForm().findField('operator_joindate').setValue('<?php print $operator_joindate; ?>');
		
		employeeentry.getForm().findField('operator_code').setValue('<?php print $operator_code; ?>');
		employeeentry.getForm().findField('account_no').setValue('<?php print $account_no; ?>');
		employeeentry.getForm().findField('bank_name').setValue('<?php print $bank_name; ?>');	
		employeeentry.getForm().findField('branch_name').setValue('<?php print $branch_name; ?>');
     	employeeentry.getForm().findField('ifsc_code').setValue('<?php print $ifsc_code; ?>');
	}
});
</script>
<!-- end custom CSS/JS -->`
<div align="center"><div id="main-content" style="width:680px" align="left"></div></div> 
<?php require "_footer.php";?>