<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

// Fetch all bulk invoices
$invoices = $conn->query("
    SELECT i.*, g.name AS guest_name
    FROM invoices i
    LEFT JOIN guests g ON i.guest_id = g.id
    WHERE i.invoice_number LIKE 'INV-BLK-%'
    ORDER BY i.id DESC
");
?>

<div class="container mt-4">
    <h3>Combined / Bulk Invoices</h3>
    <table class="table table-bordered table-striped mt-3">
        <thead class="table-dark">
            <tr>
                <th>#</th>
                <th>Invoice Number</th>
                <th>Guest</th>
                <th>Total Amount (MWK)</th>
                <th>Status</th>
                <th>Description</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php if($invoices->num_rows > 0): ?>
            <?php $count=1; while($row = $invoices->fetch_assoc()): ?>
            <tr>
                <td><?= $count++ ?></td>
                <td><?= htmlspecialchars($row['invoice_number']) ?></td>
                <td><?= htmlspecialchars($row['guest_name'] ?? '-') ?></td>
                <td><?= number_format($row['total_amount'],2) ?></td>
                <td><?= $row['status'] ?></td>
                <td><?= htmlspecialchars($row['description']) ?></td>
     <td>

    <!-- View Invoice -->
    <a href="bulk_invoice_new.php?invoice_id=<?= $row['id'] ?>" 
       target="_blank" 
       class="btn btn-sm btn-primary mb-1">
        <i class="fa fa-eye"></i> View
    </a>


    <!-- Edit Invoice -->
    <a href="edit_bulk_invoice.php?invoice_id=<?= $row['id'] ?>" 
       class="btn btn-sm btn-warning mb-1">
        <i class="fa fa-edit"></i> Edit
    </a>


    <!-- Receive Payment -->
    <a href="receive_bulk_pay.php?invoice_id=<?= $row['id'] ?>" 
       class="btn btn-sm btn-success mb-1">
        <i class="fa fa-money-bill"></i> Receive Payment
    </a>

</td>
            </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr><td colspan="7" class="text-center">No bulk invoices found.</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</div>