<?php
include 'includes/config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cheque_id = isset($_POST['cheque_id']) ? $_POST['cheque_id'] : '';
    $status = isset($_POST['status']) ? $_POST['status'] : '';
    $status_date = isset($_POST['status_date']) ? $_POST['status_date'] : null;

    try {
        $sql = "UPDATE cheque_payments 
                SET 
                    status = :status, 
                    cleared_date = IF(:status = 'cleared', NOW(), cleared_date),
                     clearance_date = IF(:status = 'clearance', NOW(), clearance_date),
                    bounced_date = IF(:status = 'bounced', NOW(), bounced_date),
                    hold_until = IF(:status = 'hold', :status_date, hold_until)
                WHERE id = :cheque_id";
        
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':status', $status);
        $stmt->bindParam(':status_date', $status_date);
        $stmt->bindParam(':cheque_id', $cheque_id);
        $stmt->execute();

        header('Location: list_cheque.php');
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}
?>