<?php
$message = (isset($_SESSION['message'])) ? $_SESSION['message'] :"";
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "insert";

$where 	= " WHERE 1=1 ";


$query_string = "";

$button_value 	= "Add";
$table_name		= "timesheet";

$next_page_condition 	= "";

if($_REQUEST['hide_top']){
	$next_page_condition.="&hide_top=1";
}
if($_REQUEST['hide_left']){
	$next_page_condition.="&hide_left=1";
}
if($_REQUEST['job_id']){
	$job_id = $_REQUEST['job_id'];
	$where 	.= " AND job_id = $job_id ";
	$next_page_condition.="&job_id=".$job_id;
}
if($_REQUEST['project_id']){
	$project_id = $_REQUEST['project_id'];
	$where 	.= " AND project_id = $project_id ";
	$next_page_condition.="&project_id=".$project_id;
}

if(isset($_REQUEST['source'])){
	$source = $_REQUEST['source'];
	$next_page_condition.="&source=$source";
}
if(isset($_REQUEST['from'])){
	$from = $_REQUEST['from'];
}

$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
	//$next_page_condition.="&start=$start";
}
$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	$action 	= "update";
	$button_value 	= "Update";
	$page_name		= "Edit Timesheet";	
		
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $id= $row_edit['id'];
    $job_id= $row_edit['job_id'];
    $project_id= $row_edit['project_id'];
    $admin_id= explode(",",$row_edit['admin_id']);
	$timesheet_text= $row_edit['timesheet_text'];
	$timesheet_option= $row_edit['timesheet_option'];
	$timesheet_date= $row_edit['timesheet_date'];
	$timesheet_time= $row_edit['timesheet_time'];
	$from_time= $row_edit['from_time'];
	$to_time= $row_edit['to_time'];
}
################################################# END EDIT ################################################# 


################################################## START update ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string","admin_id"));
	$id= $_REQUEST['id'];
    $job_id= $_REQUEST['job_id'];
    $project_id= $_REQUEST['project_id'];
    $admin_id= implode(",",$_REQUEST['admin_id']);
    //$timesheet_text = Keycode::getkeycodeById($_REQUEST['timesheet_option']);    
    $timesheet_text= $_REQUEST['timesheet_text'];;    
    $timesheet_option= $_REQUEST['timesheet_option'];    
	
    $sql= "UPDATE timesheet SET job_id = '$job_id',project_id = '$project_id',admin_id = '$admin_id',timesheet_text = '$timesheet_text',timesheet_option = '$timesheet_option' WHERE id = $id";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace(array("page=add","action=edit"), array("page=list",""), $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("timesheet option updated sucessfully");
	$redirect = "index?page=view_timesheet_option$query_string";
	header("Location: $redirect");
	exit();
	
}
################################################# END update ################################################# 
?>

<section class="content-header hide">
  <h1>
	<? echo str_replace(array("_"),array(" "),$page_header); ?>
  </h1>		
</section>
<!----------------------- Message ------------------------------->
	<? if(isset($_SESSION['message'])){ ?>
	<section class="content-header">
	  <div class="alert alert-success alert-dismissable" style="margin-bottom:0px;">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		<? echo $_SESSION['message'];
			unset($_SESSION['message']);
		?>
	  </div>
	</section>
	<? } ?>
<!----------------------- End Message ------------------------------->
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
				<div class="box-body">
					<div class="row" style="padding:0px 10px;">
						<?php include_once('add_timesheet_option.inc.php');?>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>
		