<?php
session_start();

require_once '../database.php';  

if(!isset($_SESSION['id']) || $_SESSION['id']=='')
{
	http_response_code(401);
    echo json_encode(['error_text' => 'Authentication Problem']);
    exit;
}
if(!isset($_SESSION['user_type']) || ($_SESSION['user_type'] == 'chef'))
{
	http_response_code(401);
    echo json_encode(['error_text' => 'Authentication Problem']);
    exit;
}


class getTicketCount extends database
{
    public function getUserTicketCount($date)
    {
        return $this->fetchRow('user_ticket', ['date' => $date]);
    }
}


if(!isset($_POST['date']) || $_POST['date'] == null || trim($_POST['date']) == '')
{
    http_response_code(400);
    echo json_encode(['error_text' => 'Date is required']);
    exit;
}


$obj = new getTicketCount();
$date = $_POST['date'];
$date = date('Y-m-d', strtotime($date));

$res = $obj->getUserTicketCount($date);
if(!$res)
{
    http_response_code(200);
    echo json_encode(['count' => 0]);
    exit;
}

http_response_code(200);
    echo json_encode(['count' => $res['user_count']]);
    exit;