<?php 
require_once 'includes/user_header.php'; 

$user_id = $_SESSION['user_id'];

// 1. Get Stats for Cards
$stats = $pdo->prepare("
    SELECT 
        COUNT(*) as total,
        SUM(CASE WHEN current_status = 'pending' THEN 1 ELSE 0 END) as pending,
        SUM(CASE WHEN current_status = 'completed' THEN 1 ELSE 0 END) as completed
    FROM replacement_requests WHERE user_id = ?
");
$stats->execute([$user_id]);
$s = $stats->fetch();

// 2. Fetch User's Requests (Newest first)
$stmt = $pdo->prepare("SELECT * FROM replacement_requests WHERE user_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$requests = $stmt->fetchAll();
?>

<div class="row mb-4">
    <div class="col-md-4">
        <div class="card text-white bg-primary mb-3">
            <div class="card-body">
                <h5 class="card-title">Total Requests</h5>
                <h2 class="display-6"><?php echo $s['total']; ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card text-dark bg-warning mb-3">
            <div class="card-body">
                <h5 class="card-title">Pending</h5>
                <h2 class="display-6"><?php echo $s['pending'] ?? 0; ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card text-white bg-success mb-3">
            <div class="card-body">
                <h5 class="card-title">Completed</h5>
                <h2 class="display-6"><?php echo $s['completed'] ?? 0; ?></h2>
            </div>
        </div>
    </div>
</div>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h4>My Replacement List</h4>
    <a href="add_request.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add New Customer</a>
</div>

<div class="card shadow-sm">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>Customer Name</th>
                        <th>Replacing Email</th>
                        <th>Plan</th>
                        <th>Status</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($requests) > 0): ?>
                        <?php foreach($requests as $r): ?>
                        <tr>
                            <td>#<?php echo $r['id']; ?></td>
                            <td>
                                <strong><?php echo htmlspecialchars($r['customer_name']); ?></strong><br>
                                <small class="text-muted"><?php echo htmlspecialchars($r['customer_business_name']); ?></small>
                            </td>
                            <td><?php echo htmlspecialchars($r['customer_email']); ?></td>
                            <td><?php echo htmlspecialchars($r['adobe_plan']); ?></td>
                            <td>
                                <span class="badge status-badge bg-<?php echo $r['current_status']; ?>">
                                    <?php echo ucfirst(str_replace('_', ' ', $r['current_status'])); ?>
                                </span>
                            </td>
                            <td><?php echo date('d M Y', strtotime($r['created_at'])); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="6" class="text-center p-4">No requests found. Start by adding one!</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

</div> 
</body>
</html>