<?php

//action.php

include('database_connection.php');

$dt = new DateTime();
$tz = new DateTimeZone('Asia/Dhaka'); // or whatever zone you're after
$dt->setTimezone($tz);
$timenow= $dt->format('d-M-Y h:i:s a');


if(isset($_POST["action"]))
{
	if($_POST["action"] == "insert")
	{	
// 		$timenow=date("d-M-Y h:i:s a");
		$query = "
		INSERT INTO tasks (employee_id, details,  created_at) VALUES ('".$_POST["employee_id"]."', '".$_POST["details"]."',  '".$timenow."')
		";
		$statement = $connect->prepare($query);
		$statement->execute();
		echo '<p>Data Inserted...</p>';
	}
	if($_POST["action"] == "fetch_single")
	{
		$query = "SELECT * FROM tasks LEFT JOIN tbl_admin ON tbl_admin.admin_id=tasks.employee_id WHERE id='".$_POST["id"]."'";
		$statement = $connect->prepare($query);
		$statement->execute();
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
 			$output['employee_id'] = $row['emp_name'];
			$output['details'] = $row['details'];
			$output['priority'] = $row['priority'];
		}
		echo json_encode($output);
	}
	if($_POST["action"] == "update")
	{
		$query = "
		UPDATE tasks 
		SET employee_id = '".$_POST["employee_id"]."', 
		details = '".$_POST["details"]."', 
		WHERE id = '".$_POST["hidden_id"]."'
		";
		$statement = $connect->prepare($query);
		$statement->execute();
		echo '<p>Data Updated</p>';
	}

	if($_POST["action"] == "started")
	{	
// 		$timenow=date("d-M-Y h:i:s a");
		$query = "UPDATE tasks SET status= '1', started_at='".$timenow."' WHERE id = '".$_POST["id"]."'";
		$statement = $connect->prepare($query);
		$statement->execute();
		echo '<p>Work Started</p>';
	}
	if($_POST["action"] == "stopped")
	{	
// 		$timenow=date("d-M-Y h:i:s a");
		$query = "UPDATE tasks SET status= '2', stopped_at='".$timenow."' WHERE id = '".$_POST["id"]."'";
		$statement = $connect->prepare($query);
		$statement->execute();
		echo '<p>Work Stopped</p>';
	}

	if($_POST["action"] == "delete")
	{
		$query = "DELETE FROM tasks WHERE id = '".$_POST["id"]."'";
		$statement = $connect->prepare($query);
		$statement->execute();
		echo '<p>Data Deleted</p>';
	}
}

?>