<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role_code'] !== 'PATIENT') {
    header('Location: login.php');
    exit();
}

$user_id = $_SESSION['user_id'];
$pat_res = $conn->query("SELECT patient_id FROM patients WHERE user_id = $user_id")->fetch_assoc();
$patient_id = $pat_res['patient_id'];

// Get Breakdown of Outstanding
$sql = "SELECT * FROM invoices WHERE patient_id = ? AND balance > 0 AND status != 'CANCELLED' ORDER BY invoice_date ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $patient_id);
$stmt->execute();
$result = $stmt->get_result();

$total = 0;
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Outstanding Balance')); ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Outstanding Balance</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card border-warning">
                                <div class="card-header bg-warning-subtle text-warning">
                                    <h5 class="card-title mb-0"><i class="ri-alert-line me-2"></i>Payment Required</h5>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered dt-responsive nowrap w-100">
                                        <thead>
                                            <tr>
                                                <th>Invoice #</th>
                                                <th>Date</th>
                                                <th>Due Amount</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()):
                                                $total += $row['balance'];
                                                ?>
                                                <tr>
                                                    <td><?php echo $row['invoice_number']; ?></td>
                                                    <td><?php echo date('M d, Y', strtotime($row['invoice_date'])); ?></td>
                                                    <td class="fw-bold">KES <?php echo number_format($row['balance'], 2); ?>
                                                    </td>
                                                    <td>
                                                        <a href="patient_make_payment.php?invoice_id=<?php echo $row['invoice_id']; ?>"
                                                            class="btn btn-sm btn-success">
                                                            <i class="ri-bank-card-line"></i> Pay Now
                                                        </a>
                                                    </td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                        <tfoot>
                                            <tr class="table-light">
                                                <td colspan="2" class="text-end fw-bold">TOTAL OUTSTANDING:</td>
                                                <td class="fw-bold text-danger">KES
                                                    <?php echo number_format($total, 2); ?></td>
                                                <td>
                                                    <?php if ($total > 0): ?>
                                                        <a href="patient_make_payment.php?amount=<?php echo $total; ?>"
                                                            class="btn btn-primary">Pay All</a>
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>