<?php
//user_action.php
include('db_con.php');
if (isset($_SESSION['user_id'])) {
		$user_type = $_SESSION['user_type'];
		$user_id = $_SESSION['user_id'];
		$user_name = $_SESSION['user_name'];
		$branch_id = $_SESSION['branch_id'];
		$branch_name = $_SESSION['branch_name'];
		$store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
	header('Location: login');
	die();
}
if(isset($_POST['btn_action']))
{
	if($_POST['btn_action'] == 'Add')
	{
			
		$query = "
		INSERT INTO todo (dated,description,user_id)
        VALUES (:dated, :description, :user_id)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
          ':dated'             =>  $_POST['dated'],
           ':description'             =>  $_POST['description'],
             ':user_id'             =>  $user_id
        
          )
		);
		
		//$result = $statement->fetchAll();
		if(isset($result))
		{
			echo 'New TODO Added';
		}
	}
	if($_POST['btn_action'] == 'fetch_single')
	{
		$query = "
		SELECT * FROM todo WHERE id = :id";
		
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':id'	=>	$_POST["id"]
			)
		);
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
			
			
			
           $output['dated']          =  $row['dated'];
           $output['description']          =  $row['description'];
          
          
          
		}
		echo json_encode($output);
	}
	if($_POST['btn_action'] == 'Edit')
	{			
		
			$query = "
			UPDATE todo SET
				
				dated = '".$_POST["dated"]."',		
				
			description = '".$_POST["description"]."'				
				
				WHERE id = '".$_POST["id"]."'
			";
			$statement = $connect->prepare($query);

		$statement->execute();

		$result = $statement->fetchAll();
		
		if(isset($result))
		{
			echo 'TODO Details Edited';
		}
	}
	if($_POST['btn_action'] == 'delete')
	{
		
		$query = "
		delete from todo

		WHERE id = :id
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				
				':id'		=>	$_POST["id"]
			)
		);	
		
		$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'Sub Vendor Status change to ' . $status;
		}
	}
}
?>