<?php

//action.php





include('database_connection.php');

if(isset($_POST["action"]))
{
	if($_POST["action"] == "insert")
	{	
		$dt = new DateTime();
        $tz = new DateTimeZone('Asia/Dhaka'); // or whatever zone you're after
        $dt->setTimezone($tz);
        $timenow= $dt->format('Y-m-d');
        
		$query = "
		INSERT INTO tasks (employee_id, details, priority, created_at) VALUES ('".$_POST["employee_id"]."', '".$_POST["details"]."', '".$_POST["priority"]."', '".$_POST["created_at"]."')
		";
		$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"]."' 
		ORDER BY created_at ASC";
		$statement = $connect->prepare($query);
		$statement->execute();
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
			$output['employee_id'] = $row['employee'];
			$output['details'] = $row['details'];
			$output['priority'] = $row['priority'];
			$output['created_at'] = $row['created_at'];
		}
		echo json_encode($output);
	}
	if($_POST["action"] == "update")
	{
		$query = "
		UPDATE tasks 
		SET employee_id = '".$_POST["employee_id"]."', 
		details = '".$_POST["details"]."', 
		priority = '".$_POST["priority"]."', 
		created_at = '".$_POST["created_at"]."' 
		WHERE id = '".$_POST["hidden_id"]."'
		";
		$statement = $connect->prepare($query);
		$statement->execute();
		echo '<p>Data Updated</p>';
	}

	if($_POST["action"] == "started")
	{	
		$dt = new DateTime();
        $tz = new DateTimeZone('Asia/Dhaka'); // or whatever zone you're after
        $dt->setTimezone($tz);
        $timenow= $dt->format('Y-m-d');
        
		$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")
	{	
		$dt = new DateTime();
        $tz = new DateTimeZone('Asia/Dhaka'); // or whatever zone you're after
        $dt->setTimezone($tz);
        $timenow= $dt->format('Y-m-d');
        
		$query = "UPDATE tasks SET status= '2' , started_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>';
	}
}

?>