<?php
require_once '../config/config.php';
require_admin();

// Handle Filter
$status_filter = $_GET['status'] ?? '';
$sql = "SELECT r.*, u.business_name, u.phone_whatsapp as reseller_phone 
        FROM replacement_requests r 
        JOIN users u ON r.user_id = u.id";

if ($status_filter) {
    $sql .= " WHERE r.current_status = :status";
}
$sql .= " ORDER BY r.created_at DESC";

$stmt = $pdo->prepare($sql);
if ($status_filter) {
    $stmt->execute(['status' => $status_filter]);
} else {
    $stmt->execute();
}
$requests = $stmt->fetchAll();

require_once 'includes/header.php';
?>

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">All Requests</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <form method="GET" class="d-flex gap-2">
            <select name="status" class="form-select" onchange="this.form.submit()">
                <option value="">All Statuses</option>
                <option value="pending" <?php if($status_filter=='pending') echo 'selected'; ?>>Pending</option>
                <option value="in_progress" <?php if($status_filter=='in_progress') echo 'selected'; ?>>In Progress</option>
                <option value="completed" <?php if($status_filter=='completed') echo 'selected'; ?>>Completed</option>
            </select>
        </form>
    </div>
</div>

<div class="table-responsive">
    <table class="table table-bordered">
        <thead class="table-dark">
            <tr>
                <th>ID</th>
                <th>Reseller</th>
                <th>Customer Info</th>
                <th>Target Email</th>
                <th>Plan</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($requests as $r): ?>
            <tr>
                <td>#<?php echo $r['id']; ?></td>
                <td>
                    <strong><?php echo htmlspecialchars($r['business_name']); ?></strong><br>
                    <small><a href="https://wa.me/<?php echo $r['reseller_phone']; ?>" target="_blank" class="text-decoration-none text-success"><i class="fab fa-whatsapp"></i> <?php echo $r['reseller_phone']; ?></a></small>
                </td>
                <td>
                    <?php echo htmlspecialchars($r['customer_name']); ?><br>
                    <small class="text-muted"><?php echo htmlspecialchars($r['customer_business_name']); ?></small>
                </td>
                <td class="text-primary"><?php echo htmlspecialchars($r['customer_email']); ?></td>
                <td><?php echo htmlspecialchars($r['adobe_plan']); ?></td>
                <td>
                    <?php
                    $colors = [
                        'pending' => 'warning',
                        'in_progress' => 'info',
                        'completed' => 'success',
                        'rejected' => 'danger'
                    ];
                    $bg = $colors[$r['current_status']] ?? 'secondary';
                    ?>
                    <span class="badge bg-<?php echo $bg; ?>"><?php echo strtoupper(str_replace('_',' ',$r['current_status'])); ?></span>
                </td>
                <td>
                    <a href="request_view.php?id=<?php echo $r['id']; ?>" class="btn btn-sm btn-primary">Manage</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

</main>
</div>
</div>
</body>
</html>