<?php
require_once('config.php');

try {

    $body = json_decode(file_get_contents("php://input"), true);

    $site_cd = $body['site_cd'] ?? '';
    $ccls_no = $body['ccls_no'] ?? '';

    if ($site_cd === '' || $ccls_no === '') {
        throw new Exception("Missing required parameters: site_cd or ccls_no");
    }

    /* ================= HEADER QUERY ================= */

    $sql_header = "
        SELECT TOP 1
            m.site_cd,
            m.ccls_mst_ccls_no,
            m.ccls_mst_ccls_period,
            m.ccls_mst_ccls_date,
            m.ccls_mst_status,
            m.ccls_mst_desc,
            m.ccls_mst_remark,
            m.ccls_mst_create_by,
            m.ccls_mst_create_date,
            m.audit_user,
            m.audit_date,
            m.RowID
        FROM ccls_mst m
        WHERE m.site_cd = ?
        AND m.ccls_mst_ccls_no = ?
        ORDER BY m.ccls_mst_ccls_date DESC, m.RowID DESC
    ";

    $params_header = [$site_cd, $ccls_no];
    $stmt_header = sqlsrv_query($conn, $sql_header, $params_header);

    if ($stmt_header === false) {
        throw new Exception(print_r(sqlsrv_errors(), true));
    }

    $header = sqlsrv_fetch_array($stmt_header, SQLSRV_FETCH_ASSOC);

    if (!$header) {
        throw new Exception("Header record not found");
    }

    $mst_RowID = $header['RowID'];

    if ($header['ccls_mst_ccls_date'] instanceof DateTime) {
        $header['ccls_mst_ccls_date'] = $header['ccls_mst_ccls_date']->format('Y-m-d H:i:s');
    }

    if ($header['ccls_mst_create_date'] instanceof DateTime) {
        $header['ccls_mst_create_date'] = $header['ccls_mst_create_date']->format('Y-m-d H:i:s');
    }

    if ($header['audit_date'] instanceof DateTime) {
        $header['audit_date'] = $header['audit_date']->format('Y-m-d H:i:s');
    }

    sqlsrv_free_stmt($stmt_header);

    /* ================= DETAILS QUERY ================= */
    /* ccls_ls1 data for review */

    $sql_details = "
        SELECT
            l.site_cd,
            l.mst_RowID,
            l.ccls_ls1_linen,
            l.ccls_ls1_qty,
            l.ccls_ls1_remark,
            l.RowID
        FROM ccls_ls1 l
        WHERE l.site_cd = ?
        AND l.mst_RowID = ?
        ORDER BY l.ccls_ls1_linen ASC, l.RowID ASC
    ";

    $params_details = [$site_cd, $mst_RowID];
    $stmt_details = sqlsrv_query($conn, $sql_details, $params_details);

    if ($stmt_details === false) {
        throw new Exception(print_r(sqlsrv_errors(), true));
    }

    $details = [];

    while ($row = sqlsrv_fetch_array($stmt_details, SQLSRV_FETCH_ASSOC)) {


        $details[] = $row;
    }

    sqlsrv_free_stmt($stmt_details);

    /* ================= RESPONSE ================= */

    echo json_encode([
        "status"  => "SUCCESS",
        "header"  => $header,
        "details" => $details
    ]);

} catch (Exception $e) {

    echo json_encode([
        "status"  => "ERROR",
        "message" => $e->getMessage(),
        "header"  => null,
        "details" => []
    ]);
} finally {
    if (isset($conn)) {
        sqlsrv_close($conn);
    }
}
?>