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

// ===========================
// DELETE INVOICE (admin only)
// ===========================
if(isset($_POST['delete_invoice']) && isset($_SESSION['role']) && $_SESSION['role'] === 'admin'){
    $del_id = intval($_POST['delete_invoice_id']);

    // Optional: delete related entries from other tables if needed
    // $conn->query("DELETE FROM invoice_items WHERE invoice_id=$del_id");

    $del_stmt = $conn->prepare("DELETE FROM invoices WHERE id=?");
    $del_stmt->bind_param("i", $del_id);

    if($del_stmt->execute()){
        echo "<div class='alert alert-success'>Invoice deleted successfully.</div>";
        // Redirect to avoid resubmission
        echo "<meta http-equiv='refresh' content='1;url=conference_invoices.php'>";
        exit;
    } else {
        echo "<div class='alert alert-danger'>Failed to delete invoice: ".$conn->error."</div>";
    }
}
include 'includes/header.php';
?>

<div class="container mt-4">
      <button class="btn btn-secondary" onclick="history.back()">
        <i class="fa fa-arrow-left"></i> Back
    </button>
    <h3>Conference Booking Invoices</h3>

    <table class="table table-striped table-bordered mt-3">
        <thead class="table-dark">
            <tr>
                <th>#</th>
                <th>Invoice Number</th>
                <th>Guest Name</th>
                <th>Function Date</th>
                <th>Total Amount (MWK)</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $invoices = $conn->query("
               SELECT i.*, g.name AS guest_name, cb.booking_id, cb.client_name, cb.booking_date
    FROM invoices i
    LEFT JOIN conference_bookings cb ON i.stay_id = cb.id
    LEFT JOIN guests g ON i.guest_id = g.id
    WHERE i.invoice_number LIKE 'INVCNF%' 
    ORDER BY i.created_at DESC
            ");

            $count = 1;
            while($inv = $invoices->fetch_assoc()):
            ?>
            <tr>
                <td><?= $count++ ?></td>
                <td><?= htmlspecialchars($inv['invoice_number']) ?></td>
                <td><?= htmlspecialchars($inv['client_name'] ?? '-') ?></td>
                <td><?= isset($inv['booking_date']) ? date('d/m/Y', strtotime($inv['booking_date'])) : '-' ?></td>
                <td><?= number_format($inv['total_amount'],2) ?></td>
                <td>
                    <span class="badge bg-<?= $inv['status']=='Paid'?'success':($inv['status']=='Partially Paid'?'warning':'danger') ?>">
                        <?= $inv['status'] ?>
                    </span>
                </td>
 
<td>

     <!-- View Invoice -->
    <a href="view_invoice.php?invoice_id=<?= $inv['id'] ?>" class="btn btn-sm btn-primary">
        <i class="fa fa-eye"></i> View Invoice
    </a>
    <!-- Receive Payment -->
    <?php if(!empty($inv['guest_id']) && $inv['status'] != 'Paid'): ?>
        <a href="receive_pay.php?guest_id=<?= $inv['guest_id'] ?>&invoice_id=<?= $inv['id'] ?>" class="btn btn-sm btn-success">
            <i class="fa fa-money-bill-wave"></i> Receive Payment
        </a>
    <?php endif; ?>

    <!-- Print Receipt (only for Paid invoices) -->
    <?php if($inv['status']=='Paid'): ?>
        <a href="receipt_cthermal.php?invoice_id=<?= $inv['id'] ?>" target="_blank" class="btn btn-sm btn-warning">
            <i class="fa fa-receipt"></i> Print Receipt
        </a>
    <?php endif; ?>

    <!-- DELETE (only admin and NOT paid) -->
    <?php if(isset($_SESSION['role']) && $_SESSION['role'] === 'admin' && $inv['status'] != 'Paid'): ?>
        <form method="post" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this invoice?');">
            <input type="hidden" name="delete_invoice_id" value="<?= $inv['id'] ?>">
            <button type="submit" name="delete_invoice" class="btn btn-sm btn-danger">
                <i class="fa fa-trash"></i> Delete
            </button>
        </form>
    <?php endif; ?>
</td>



            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>

<?php include 'includes/footer.php'; ?>
