<?php
$pageTitle = "Manage Vendors";
require_once 'sidebar.php';

if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    $action = $_GET['action'];
    if ($action === 'approve') {
        mysqli_query($conn, "UPDATE users SET status='active' WHERE id=$id AND role='vendor'");
        redirect(BASE_URL.'/admin/vendors.php', 'Vendor approved successfully.');
    } elseif ($action === 'block') {
        mysqli_query($conn, "UPDATE users SET status='blocked' WHERE id=$id AND role='vendor'");
        redirect(BASE_URL.'/admin/vendors.php', 'Vendor blocked.');
    } elseif ($action === 'delete') {
        mysqli_query($conn, "DELETE FROM users WHERE id=$id AND role='vendor'");
        redirect(BASE_URL.'/admin/vendors.php', 'Vendor deleted.');
    }
}

$vendors = mysqli_query($conn, "SELECT u.*, (SELECT COUNT(*) FROM halls WHERE vendor_id=u.id) as hall_count 
                                FROM users u WHERE role='vendor' ORDER BY 
                                CASE status WHEN 'pending' THEN 1 WHEN 'active' THEN 2 ELSE 3 END, 
                                created_at DESC");
?>

<div class="page-header">
    <h1>Manage Vendors</h1>
    <p style="color:#888; font-size:14px;"><?php echo mysqli_num_rows($vendors); ?> total vendors</p>
</div>

<?php showFlash(); ?>

<div class="table-wrapper">
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Halls</th>
                <th>Status</th>
                <th>Joined</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (mysqli_num_rows($vendors) > 0): ?>
                <?php while ($v = mysqli_fetch_assoc($vendors)): ?>
                    <tr>
                        <td>#<?php echo $v['id']; ?></td>
                        <td><strong><?php echo htmlspecialchars($v['name']); ?></strong></td>
                        <td><?php echo htmlspecialchars($v['email']); ?></td>
                        <td><?php echo htmlspecialchars($v['phone']); ?></td>
                        <td style="text-align:center;"><strong style="color:var(--bronze);"><?php echo $v['hall_count']; ?></strong></td>
                        <td>
                            <?php 
                            $badgeClass = $v['status']=='active'?'confirmed':($v['status']=='pending'?'pending':'cancelled');
                            ?>
                            <span class="badge badge-<?php echo $badgeClass; ?>"><?php echo ucfirst($v['status']); ?></span>
                        </td>
                        <td><?php echo date('d M Y', strtotime($v['created_at'])); ?></td>
                        <td>
                            <?php if ($v['status'] === 'pending'): ?>
                                <a href="?action=approve&id=<?php echo $v['id']; ?>" class="btn btn-sm btn-success">Approve</a>
                            <?php elseif ($v['status'] === 'active'): ?>
                                <a href="?action=block&id=<?php echo $v['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Block this vendor?')">Block</a>
                            <?php else: ?>
                                <a href="?action=approve&id=<?php echo $v['id']; ?>" class="btn btn-sm btn-success">Unblock</a>
                            <?php endif; ?>
                            <a href="?action=delete&id=<?php echo $v['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete vendor and all their data?')">Delete</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="8" style="text-align:center; padding:40px; color:#888;">
                    <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">❦</div>
                    No vendors yet.
                </td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

</main></div></body></html>
