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

if (isset($_GET['Billno'])) {
    $billNo = $_GET['Billno'];

    // Fetch Patient ID based on BillNo
    $query = mysqli_query($db, "SELECT PatientId FROM bill WHERE BillNo = '$billNo' LIMIT 1");

    if (mysqli_num_rows($query) == 0) {
        echo "<script>alert('Invalid Bill Number!');</script>";
        echo "<meta http-equiv='refresh' content='0;url=FinaleBillId.php'>";
        exit;
    }

    $row = mysqli_fetch_assoc($query);
    $patid = $row['PatientId'];

    // Fetch patient details
    $patientQuery = mysqli_query($db, "SELECT * FROM patientdetails WHERE PatId = '$patid'");
    $patientData = mysqli_fetch_assoc($patientQuery);

    // Fetch advance payment details for the BillNo
    $advanceAmount = 0;
    $advanceDetails = [];
    $advanceQuery = mysqli_query($db, "SELECT * FROM advance WHERE BillNo = '$billNo'");

    while ($row = mysqli_fetch_assoc($advanceQuery)) {
        $advanceAmount += $row['Amount'];
        $advanceDetails[] = $row;
    }

    // Fetch all bills related to this patient
    $billNumbers = [];
    $res1 = mysqli_query($db, "SELECT BillNo FROM bill WHERE PatientId = '$patid'");
    while ($row = mysqli_fetch_assoc($res1)) {
        $billNumbers[] = $row['BillNo'];
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Advance Bill Details</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-4">
        <h2 class="text-center">Advance Bill Details</h2>

        <?php if (!empty($patientData)) { ?>
            <table class="table table-bordered">
                <tr><th>Patient ID</th><td><?= $patientData['PatId']; ?></td></tr>
                <tr><th>Name</th><td><?= $patientData['Name']; ?></td></tr>
                <tr><th>Contact</th><td><?= $patientData['Contact']; ?></td></tr>
                <tr><th>Advance Amount</th><td><?= number_format($advanceAmount, 2); ?></td></tr>
            </table>

            <h4>Advance Payment History</h4>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Bill No</th>
                        <th>Advance Date</th>
                        <th>Amount</th>
                        <th>Payment Mode</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($advanceDetails as $adv) { ?>
                        <tr>
                            <td><?= $adv['BillNo']; ?></td>
                            <td><?= $adv['AdvanceDate']; ?></td>
                            <td><?= number_format($adv['Amount'], 2); ?></td>
                            <td><?= $adv['PaymentMode']; ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>

            <h4>Related Bills</h4>
            <ul>
                <?php foreach ($billNumbers as $bill) { ?>
                    <li><a href="ViewBill.php?Billno=<?= $bill; ?>">View Bill No: <?= $bill; ?></a></li>
                <?php } ?>
            </ul>
        <?php } else { ?>
            <div class="alert alert-danger">No patient data found for this Bill No.</div>
        <?php } ?>
    </div>
</body>
</html>
