<?php
include("dbconfig.php");

// Check if billno is passed in the URL
if (isset($_GET['billno'])) {
    $billno = $_GET['billno'];

    // Query to fetch Bill details for the given BillNo and PatientType = 'OPD'
    $sql_bill = "SELECT `BillId`, `BillNo`, `PatientId`, `PatientType`, `TotAmount`, `PaymentMode`, `BDate`, `BTime`, `PayableAmount`, `InsuranceName`, `InsuranceNo`, `bank`, `cardno`, `upi`, `cash`, `PreparedBy`, `upiid`, `cashamount`, `cardamount`
                 FROM `bill` WHERE `BillNo` = '$billno' AND `PatientType` = 'OPD'";

    $result_bill = $db->query($sql_bill);

    // Fetch the Bill Data
    if ($result_bill && $result_bill->num_rows > 0) {
        $bill_data = $result_bill->fetch_assoc();
        $patientId = $bill_data['PatientId']; // Store PatientId for fetching patient details
    } else {
        echo "No Bill details found for this BillNo.";
        exit;
    }

    // Query to fetch Patient details from opdpatientregister based on PatientId
    $sql_patient = "SELECT `SlNo`, `PatientId`, `title`, `Name`, `FHN`, `Contact`, `Email`, `Age`, `Gender`, `Address`, `Consultant`, `admitDate`, `AdmitTime`, `CreatedAt`
                    FROM `opdpatientregister` WHERE `PatientId` = '$patientId'";

    $result_patient = $db->query($sql_patient);

    // Fetch the Patient Data
    if ($result_patient && $result_patient->num_rows > 0) {
        $patient_data = $result_patient->fetch_assoc();
    } else {
        echo "No Patient details found for this PatientId.";
        exit;
    }
}
?>

<!-- HTML Code to display the fetched data -->
<div class="container">
    <h2>Bill and Patient Details</h2>
    <h4>Bill Information</h4>
    <table class="table">
        <tr><th>Bill No.</th><td><?php echo $bill_data['BillNo']; ?></td></tr>
        <tr><th>Total Amount</th><td><?php echo $bill_data['TotAmount']; ?></td></tr>
        <tr><th>Payable Amount</th><td><?php echo $bill_data['PayableAmount']; ?></td></tr>
        <tr><th>Payment Mode</th><td><?php echo $bill_data['PaymentMode']; ?></td></tr>
        <tr><th>Insurance Name</th><td><?php echo $bill_data['InsuranceName']; ?></td></tr>
        <tr><th>Insurance No.</th><td><?php echo $bill_data['InsuranceNo']; ?></td></tr>
        <tr><th>Bank</th><td><?php echo $bill_data['bank']; ?></td></tr>
        <tr><th>Card No.</th><td><?php echo $bill_data['cardno']; ?></td></tr>
        <tr><th>UPI</th><td><?php echo $bill_data['upi']; ?></td></tr>
        <tr><th>Cash</th><td><?php echo $bill_data['cash']; ?></td></tr>
        <tr><th>Created At</th><td><?php echo $bill_data['BDate'] . " " . $bill_data['BTime']; ?></td></tr>
    </table>

    <h4>Patient Information</h4>
    <table class="table">
        <tr><th>Patient ID</th><td><?php echo $patient_data['PatientId']; ?></td></tr>
        <tr><th>Name</th><td><?php echo $patient_data['Name']; ?></td></tr>
        <tr><th>Title</th><td><?php echo $patient_data['title']; ?></td></tr>
        <tr><th>Father/Husband Name</th><td><?php echo $patient_data['FHN']; ?></td></tr>
        <tr><th>Contact</th><td><?php echo $patient_data['Contact']; ?></td></tr>
        <tr><th>Email</th><td><?php echo $patient_data['Email']; ?></td></tr>
        <tr><th>Age</th><td><?php echo $patient_data['Age']; ?></td></tr>
        <tr><th>Gender</th><td><?php echo $patient_data['Gender']; ?></td></tr>
        <tr><th>Address</th><td><?php echo $patient_data['Address']; ?></td></tr>
        <tr><th>Consultant</th><td><?php echo $patient_data['Consultant']; ?></td></tr>
        <tr><th>Admit Date</th><td><?php echo $patient_data['admitDate']; ?></td></tr>
        <tr><th>Admit Time</th><td><?php echo $patient_data['AdmitTime']; ?></td></tr>
        <tr><th>Created At</th><td><?php echo $patient_data['CreatedAt']; ?></td></tr>
    </table>

    <!-- Add buttons for actions (Edit/Delete) -->
    <a href="editBill.php?billno=<?php echo $bill_data['BillNo']; ?>"><button type="button" class="btn btn-primary">Edit Bill</button></a>
    <a href="deleteBill.php?billno=<?php echo $bill_data['BillNo']; ?>" onclick="return confirm('Are you sure you want to delete this bill?');"><button type="button" class="btn btn-danger">Delete Bill</button></a>
</div>
