<?php
require_once "web_admin_function.php";

header('Content-Type: application/json');

$action = $_POST['action'] ?? '';
$response = ['status' => 'error', 'message' => 'Invalid request'];

switch ($action) {

    case 'create':
        $agent_id = $_POST['agent_id'];
        $start_date = $_POST['start_date'];
        $end_date   = $_POST['end_date'];

        // Convert both to Y-m-d H:i:s
        $start_date = date('Y-m-d H:i:s', strtotime($start_date));
        $end_date   = date('Y-m-d H:i:s', strtotime($end_date));
        $reassign_to_agent = $_POST['reassign_to_agent'];
        $assign_type = $_POST['assign_type'];
        $created_by = $_SESSION['userid'] ?? 0;

        $query = "INSERT INTO agent_leave (agent_id, start_date, end_date, reassign_to_agent, created_by)
                  VALUES ('$agent_id', '$start_date', '$end_date', '$reassign_to_agent', '$created_by')";

        if (mysqli_query($link, $query)) {
            $response = ['status' => 'success', 'message' => 'Leave added successfully'];
        } else {
            $response['message'] = mysqli_error($link);
        }
        break;

    case 'read':
        $data = [];
        $res = mysqli_query($link,  "SELECT * 
                                                    FROM agent_leave 
                                                    WHERE (is_deleted IS NULL OR is_deleted = '0' OR is_deleted = 0) 
                                                    ORDER BY id DESC");
        while ($row = mysqli_fetch_assoc($res)) {
                $row['agent_name']   = getUserName($row['agent_id']);
                $row['reassign_to']  = getUserName($row['reassign_to_agent']);
                $row['created_by']   = getUserName($row['created_by']);
                $data[] = $row;
        }
        $response = ['status' => 'success', 'data' => $data];
        break;


    case 'update':
        $id = $_POST['id'];
        $agent_id = $_POST['agent_id'];
        $reassign_to_agent = $_POST['reassign_to_agent'];
        $start_date = $_POST['start_date'];
        $end_date   = $_POST['end_date'];
        $assign_type = $_POST['assign_type'];

        // Convert both to Y-m-d H:i:s
        $start_date = date('Y-m-d H:i:s', strtotime($start_date));
        $end_date   = date('Y-m-d H:i:s', strtotime($end_date));

        $query = "
            UPDATE agent_leave 
            SET 
                agent_id = '$agent_id',
                reassign_to_agent = '$reassign_to_agent',
                start_date = '$start_date',
                end_date = '$end_date'
            WHERE id = '$id'
        ";

        if (mysqli_query($link, $query)) {
            $response = ['status' => 'success', 'message' => 'Leave updated successfully'];
        } else {
            $response = ['status' => 'error', 'message' => mysqli_error($link)];
        }
        break;


    case 'delete':
        $id = $_POST['id'];
        $query = "UPDATE agent_leave SET is_deleted = 1 WHERE id = '$id'";
        if (mysqli_query($link, $query)) {
            $response = ['status' => 'success', 'message' => 'Leave deleted (soft delete) successfully'];
        } else {
            $response['message'] = mysqli_error($link);
        }
        break;

    case 'restore':
        $id = $_POST['id'];
        $query = "UPDATE agent_leave SET is_deleted = 0 WHERE id = '$id'";
        if (mysqli_query($link, $query)) {
            $response = ['status' => 'success', 'message' => 'Leave restored successfully'];
        } else {
            $response['message'] = mysqli_error($link);
        }
        break;

    case 'purge':
    $id = $_POST['id'];
    if (mysqli_query($link, "DELETE FROM agent_leave WHERE id='$id'")) {
        $response = ['status' => 'success', 'message' => 'Leave permanently deleted'];
    } else {
        $response['message'] = mysqli_error($link);
    }
    break;

}

echo json_encode($response);
