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

// Check permissions (Super Admin only for this specific view if restricted)
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$sql = "SELECT i.*, u.first_name, u.last_name, p.patient_number 
        FROM invoices i
        JOIN patients p ON i.patient_id = p.patient_id
        JOIN users u ON p.user_id = u.user_id
        WHERE i.status = 'DRAFT'
        ORDER BY i.created_at DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Draft Invoices')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" type="text/css" />
</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">Draft Invoices</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="draftsTable"
                                        class="table table-bordered dt-responsive nowrap table-striped align-middle"
                                        style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>Invoice #</th>
                                                <th>Patient</th>
                                                <th>Date Created</th>
                                                <th>Amount</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if ($result && $result->num_rows > 0): ?>
                                                <?php while ($row = $result->fetch_assoc()): ?>
                                                    <tr>
                                                        <td><?php echo htmlspecialchars($row['invoice_number']); ?></td>
                                                        <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                                        </td>
                                                        <td><?php echo date('d M Y', strtotime($row['created_at'])); ?></td>
                                                        <td>KES <?php echo number_format($row['total_amount'], 2); ?></td>
                                                        <td>
                                                            <a href="edit_invoice.php?id=<?php echo $row['invoice_id']; ?>"
                                                                class="btn btn-sm btn-primary">Edit</a>
                                                            <a href="finalize_invoice.php?id=<?php echo $row['invoice_id']; ?>"
                                                                class="btn btn-sm btn-success">Finalize</a>
                                                        </td>
                                                    </tr>
                                                <?php endwhile; ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#draftsTable').DataTable();
        });
    </script>
</body>

</html>