<?php
include 'includes/auth.php';
include 'db.php';
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 Quotations</h3>

    <table class="table table-striped table-bordered mt-3">
        <thead class="table-dark">
            <tr>
                <th>#</th>
                <th>Guest Name</th>
                <th>Email</th>
                <th>Number of Items</th>
                <th>Total Amount (MWK)</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php
            // Fetch guests who have quotations
            $quotes = $conn->query("
                SELECT g.id AS guest_id, g.name, g.email, COUNT(q.id) AS items_count, SUM(q.amount) AS total_amount
                FROM guests g
                JOIN quotations q ON g.id = q.guest_id
                GROUP BY g.id, g.name, g.email
                ORDER BY g.name ASC
            ");

            $count = 1;
            while($q = $quotes->fetch_assoc()):
            ?>
            <tr>
                <td><?= $count++ ?></td>
                <td><?= htmlspecialchars($q['name']) ?></td>
                <td><?= htmlspecialchars($q['email'] ?? '-') ?></td>
                <td><?= $q['items_count'] ?></td>
                <td><?= number_format($q['total_amount'],2) ?></td>
                <td>
                    <!-- View Quotation -->
                    <a href="view_quotation.php?guest_id=<?= $q['guest_id'] ?>" class="btn btn-sm btn-primary">
                        <i class="fa fa-eye"></i> View
                    </a>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>

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