<?php
include("../config.php");

if (!empty($_POST['c_no'])) {
    
    if (strpos($_POST['c_no'], 'BRD/') === 0) {
        $cNo = $_POST['c_no'];
    } else {
        $numericPart = str_pad($_POST['c_no'], 5, '0', STR_PAD_LEFT);
        $cNo = "BRD/" . $numericPart;
    }

    $query = "SELECT code, from_place, to_place FROM contract WHERE code = '$cNo'";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        echo json_encode([
            'success' => true,
            'code' => $row['code'],
            'from_place' => $row['from_place'],
            'to_place' => $row['to_place'],
        ]);
    } else {
        echo json_encode(['success' => false, 'message' => 'No records found']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid Contract Number']);
}
?>
