<?php
session_start(['read_and_close' => true]);
require_once "db.php";

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

$coid  = $_SESSION['co_id'];
$user_id   = $_SESSION['user_id'];

session_write_close(); // Release session lock immediately

try {

    if (!isset($_POST['doc_no'])) {
        echo json_encode([
            'status' => 'error',
            'message' => 'Missing document number'
        ]);
        exit;
    }

    $doc_no = trim($_POST['doc_no']);
    $t_type = trim($_POST['ttype']);

    $stmt = $pdo->prepare("
        UPDATE transactions
        SET t_status = 'Confirmed', t_conf_by = ?, t_conf_date=Now()
        WHERE t_doc_no = ? AND co_id = ?  AND t_status = 'Pending'
    ");

    $stmt->execute([$user_id, $doc_no, $coid]);

    echo json_encode([
        'status' => 'success'
    ]);

} catch (PDOException $e) {

    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage()
    ]);
}
?>