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

$user_role = $_SESSION['role'] ?? 'guest';

// Fetch guests who have quotations
$guests = $conn->query("
    SELECT g.id, g.name, g.email, 
           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
");
?>

<div class="container mt-4">
    <h3>Quotations List</h3>
    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>#</th>
                <th>Guest Name</th>
                <th>Email</th>
                <th>Total Amount (MK)</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 1; while($g = $guests->fetch_assoc()): ?>
            <tr>
                <td><?= $i++ ?></td>
                <td><?= htmlspecialchars($g['name']) ?></td>
                <td><?= htmlspecialchars($g['email']) ?></td>
                <td><?= number_format($g['total_amount'], 2) ?></td>
                <td>
                    <a href="quotation_view.php?guest_id=<?= $g['id'] ?>" class="btn btn-sm btn-primary">
                        <i class="fa fa-eye"></i> View Quote
                    </a>
                </td>
            </tr>
            <?php endwhile; ?>

            <?php if($guests->num_rows == 0): ?>
            <tr>
                <td colspan="5" class="text-center">No quotations found.</td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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