<?php
session_start();
require_once('../includes/admin-security.php');
include_once('includes/config.php');

// Handle status updates
if (isset($_GET['action']) && isset($_GET['id'])) {
    $vendorId = intval($_GET['id']);
    $action = $_GET['action']; // active, suspended, rejected

    // Safety check: ensure action is valid
    if (in_array($action, ['active', 'suspended', 'rejected'])) {
        mysqli_query($con, "UPDATE vendors SET status='$action' WHERE id='$vendorId'");
        $_SESSION['msg'] = "Vendor status updated to " . ucfirst($action);
    }
    header('location: all-vendors.php');
    exit;
}

// Get all vendors except pending
$vendorsQuery = mysqli_query($con, "SELECT v.*, u.username, u.email, ud.name, 
                                   (SELECT COUNT(*) FROM products WHERE vendor_id = v.id) as product_count,
                                   (SELECT SUM(vendor_earning) FROM vendor_earnings WHERE vendor_id = v.id) as total_earnings
                                   FROM vendors v 
                                   JOIN users u ON v.user_id = u.id 
                                   LEFT JOIN user_details ud ON ud.user_id = u.id 
                                   WHERE v.status != 'pending' 
                                   ORDER BY v.created_at DESC");
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>All Vendors - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>

    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>

        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-store me-2"></i> All Vendors</h1>
                            <p>Manage active and suspended vendors</p>
                        </div>
                    </div>

                    <?php if (isset($_SESSION['msg'])): ?>
                        <div class="alert alert-success alert-dismissible fade show">
                            <i class="fas fa-check-circle me-2"></i>
                            <?php echo $_SESSION['msg'];
                            unset($_SESSION['msg']); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <div class="table-card">
                        <div class="card-header">
                            <h5 class="mb-0">Vendor List</h5>
                        </div>
                        <div class="card-body">
                            <table class="table table-hover" id="datatablesSimple">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Store Name</th>
                                        <th>Owner</th>
                                        <th>Status</th>
                                        <th>Products</th>
                                        <th>Total Earnings</th>
                                        <th>Joined</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $cnt = 1;
                                    while ($vendor = mysqli_fetch_array($vendorsQuery)) {
                                        $statusClass = 'secondary';
                                        if ($vendor['status'] == 'active')
                                            $statusClass = 'success';
                                        elseif ($vendor['status'] == 'suspended')
                                            $statusClass = 'warning';
                                        elseif ($vendor['status'] == 'rejected')
                                            $statusClass = 'danger';
                                        ?>
                                        <tr>
                                            <td>
                                                <?php echo $cnt; ?>
                                            </td>
                                            <td><strong>
                                                    <?php echo htmlentities($vendor['store_name']); ?>
                                                </strong></td>
                                            <td>
                                                <?php echo htmlentities($vendor['name'] ?: $vendor['username']); ?><br>
                                                <small class="text-muted">
                                                    <?php echo htmlentities($vendor['business_email']); ?>
                                                </small>
                                            </td>
                                            <td><span class="badge bg-<?php echo $statusClass; ?>">
                                                    <?php echo ucfirst($vendor['status']); ?>
                                                </span></td>
                                            <td>
                                                <?php echo $vendor['product_count']; ?>
                                            </td>
                                            <td>₵
                                                <?php echo number_format($vendor['total_earnings'] ?? 0, 2); ?>
                                            </td>
                                            <td>
                                                <?php echo date('M d, Y', strtotime($vendor['created_at'])); ?>
                                            </td>
                                            <td>
                                                <div class="dropdown">
                                                    <button class="btn btn-sm btn-outline-primary dropdown-toggle"
                                                        type="button" data-bs-toggle="dropdown">
                                                        Action
                                                    </button>
                                                    <ul class="dropdown-menu">
                                                        <li><a class="dropdown-item"
                                                                href="edit-vendor.php?id=<?php echo $vendor['id']; ?>">Edit
                                                                Details</a></li>
                                                        <li>
                                                            <hr class="dropdown-divider">
                                                        </li>
                                                        <?php if ($vendor['status'] != 'active'): ?>
                                                            <li><a class="dropdown-item text-success"
                                                                    href="?id=<?php echo $vendor['id']; ?>&action=active">Activate</a>
                                                            </li>
                                                        <?php endif; ?>
                                                        <?php if ($vendor['status'] == 'active'): ?>
                                                            <li><a class="dropdown-item text-warning"
                                                                    href="?id=<?php echo $vendor['id']; ?>&action=suspended">Suspend</a>
                                                            </li>
                                                        <?php endif; ?>
                                                    </ul>
                                                </div>
                                            </td>
                                        </tr>
                                        <?php
                                        $cnt++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="js/scripts.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest"></script>
    <script src="js/datatables-simple-demo.js"></script>
</body>

</html>