<?php 
$table="emp_leave_apply";
if($_REQUEST['Save']&& $_REQUEST['action']=='')
{
	$data['empid']=$_REQUEST['empid'];
	$data['leave_reason']=$_REQUEST['leave_reason'];
	$data['period_from']=FormatDateForSQL($_REQUEST['period_from']);
	$data['period_to']=FormatDateForSQL($_REQUEST['period_to']);
	$data['total_days']=$s->FinedDays($data['period_from'],$data['period_to']);
	$data['leave_type']=$_REQUEST['leave_type'];
	$data['Companyid']=$_SESSION['ACGCompanyId'];
	if($data['period_from']>$data['period_to'])
	{
		$msgErr[]="From date can't be greater then To date";
		$status=false;
	}
	if($status)
	{
	$insert=$s->RecordInsert($table, $data);
	if(!$insert){
	die("can't insert record");
	}
	$s->pageLocation($target."&daction=Inserted"); 
	}	
	
}

if($_REQUEST['Save'] && $_REQUEST['Edit']!='')
{
	$data['empid']=$_REQUEST['empid'];
	$data['leave_reason']=$_REQUEST['leave_reason'];
	$data['period_from']=FormatDateForSQL($_REQUEST['period_from']);
	$data['period_to']=FormatDateForSQL($_REQUEST['period_to']);
	$data['total_days']=$s->FinedDays($data['period_from'],$data['period_to']);
	$data['leave_type']=$_REQUEST['leave_type'];
	$data['Companyid']=$_SESSION['ACGCompanyId'];
	
	if(date('Y-m-d')>=$data['period_from'] && date('Y-m-d')>=$data['period_to'])
	{
	$msgErr[]="You Can not update Previous Record";
	$status=false;
	
	}
	if($status)
	{
	$cond="id='".$_REQUEST['ID']."'";
	$update=$s->RecordEdit($table, $data, $cond);
	if(!$update){
	die("can't Update record");
	
	}
	$s->pageLocation($target."&daction=Updated"); 
	}	 
}

if($_REQUEST['action']=='Edit')
{
	$condition="id='".$_REQUEST['ID']."'";
	$query=$s->GetRecord($table,  $condition);
	$rowdata=DB_query($query,$db);
	$RData=DB_fetch_array($rowdata);
	$holiday_title=$RData['holiday_title'];		
	$start_date=ConvertSQLDate($RData['start_date']);
	$end_date=ConvertSQLDate($RData['end_date']);
	$published=$RData['published'];
}
 ?>
  
  <center> <h2>Leave Apply</h2></center>
		<fieldset><legend><h4> Leave Request</h4></legend>
	<form action=" " method="post">		
			<table border="0" width="50%" cellpadding="2" cellspacing="2">
				<tr><td><strong>Leave Period From</strong></td>
					<td><input type="text" name="period_from" class="datepicker" value="<?php  echo $period_from;?>"></td>
					
				</tr>
				<tr><td><strong>Leave Period To</strong></td>
					<td><input type="text" name="period_to" class="datepicker" value="<?php  echo $period_to;?>"></td>
				</tr>
				 
				<tr><td><strong>Reason of Leave</strong></td>
				<td>	<textarea name="leave_reason"> <?php echo $leave_reason; ?></textarea>
					
				  </td>
				</tr> 
				<tr><td><strong>Leave Type</strong></td>
				<td>   
					
   <select name="leave_type" id="leave_type">
   <option value="000">---Select Leave Type---</option>
   <?php 
   $leave_sql="Select * from leavemaster";
   $leave_res=DB_query($leave_sql,$db);
   while($leave_row=DB_fetch_array($leave_res))
   { 
   ?>
   <option value="<?php echo $leave_row['leavecode'];?>"<?php echo($leave_type[$i]==$leave_row['leavecode']?'selected':'')?>><?php echo $leave_row['leavename'];?></option>
   <?php } ?>
   </select>
   
					
				  </td>
					
				</tr> 
				<tr><td colspan="2"><input type="submit" name="Save" value="Save"></td></tr>
			</table>
			
			
	    </form>	
			
		</fieldset>
