<?php 
  $filepath = dirname(__FILE__);
  include_once ($filepath.'/../database/Session.php');
  Session::init();
  include_once ($filepath.'/../database/DB.php');
  $db = new DB();

  if (isset($_GET['action']) && $_GET['action'] == 'logout') {
        Session::destroy();
        echo "<script>location='login.php'</script>";
        exit();
    }
$empid= Session::get("emp_id");
 ?>
<?php

//fetch.php

include("database_connection.php");

$query = "SELECT * FROM tasks JOIN tbl_employees WHERE tasks.employee_id = tbl_employees.emp_id AND employee_id=$empid";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '
<table class="table table-striped table-bordered">
	<tr>
		<th>Submitted On</th>
		<th>Started At</th>
		<th>Stopped At</th>
		<th>Employee Name</th>
		<th>Work Description</th>
		<th>Priority</th>
		<th>Status</th>
		<th>Action</th>
	</tr>
';


if($total_row > 0)
{
	foreach($result as $row)
	{	
		if ($row["status"]==0) {
					$statusput= "<span style='color:red'>Pending</span>
					<br><button type='button' name='start' class='btn btn-success btn-xs start' id=".$row["id"].">Start Now</button>";
					}
					elseif ($row["status"]==1) {
						$statusput= "<span style='color:teal'>Running...</span>
						<br><button type='button' name='stop' class='btn btn-danger btn-xs stop' id=".$row["id"].">Stop Work</button>";
					}
					elseif ($row["status"]==2) {
						$statusput= "<span style='color:green'>Completed</span>";
					}
					elseif ($row["status"]==3) {
						$statusput= "End";
					};

		$output .= '
		<tr>
			<td width="12%">'.$row["created_at"].'</td>
			<td width="12%">'.$row["started_at"].'</td>
			<td width="12%">'.$row["stopped_at"].'</td>
			<td width="14%">'.$row["emp_name"].'</td>
			<td width="30%">'.$row["details"].'</td>
			<td width="5%">'.$row["priority"].'</td>
			<td width="5%">'.$statusput.'</td>			
			<td width="15%">
				<button style="margin:5px;" type="button" name="edit" class="btn btn-primary btn-xs edit" id="'.$row["id"].'"><i class="fa fa-edit"></i></button>
				<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["id"].'"><i class="fa fa-trash"></i></button>
			</td>			
		</tr>
		';
	}
}
else
{
	$output .= '
	<tr>
		<td colspan="4" align="center">Data not found</td>
	</tr>
	';
}
$output .= '</table>';
echo $output;
?>