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


$json = file_get_contents('php://input');
$result = json_decode($json, true);

$site_cd = $result['site_cd'] ?? '';
$date = $result['date'] ?? '';
$dept_mst_dept_cod = $result['dept_mst_dept_cod'] ?? '';

try {

    $sql = "select * from lin_mst (NOLOCK) where site_cd = ? order by lin_mst_lin_cd asc";

    $params = [$site_cd];

    $stmt = sqlsrv_query($conn, $sql, $params);
    if ($stmt === false) {
        throw new Exception("SQL error: " . print_r(sqlsrv_errors(), true));
    }

    $json = [];
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        $json[] = $row;
    }
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);

    echo json_encode([
        'status'  => 'SUCCESS',
        'message' => 'PAR linen list retrieved successfully',
        'data'    => $json
    ]);
    exit();

} catch (Exception $e) {
    if (isset($conn)) @sqlsrv_close($conn);
    echo json_encode([
        'status'  => 'ERROR',
        'message' => $e->getMessage(),
        'data'    => []
    ]);
    exit();
}
?>