<?php
/**
 * Admin Dashboard Overview (Light UI Refactor - Premium & Cohesive)
 */
require_once 'header.php';

// Original Data Logic
try {
    $total_prods = $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn() ?: 0;
    $total_orders = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'approved'")->fetchColumn() ?: 0;
    $total_users = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn() ?: 0;
    $total_revenue = $pdo->query("SELECT SUM(amount) FROM orders WHERE status = 'approved'")->fetchColumn() ?: 0;
    
    // Recent 5 Orders
    $recent_orders = $pdo->query("SELECT o.*, u.name as user_name FROM orders o LEFT JOIN users u ON o.user_id = u.id ORDER BY o.id DESC LIMIT 5")->fetchAll();
} catch (PDOException $e) {
    $total_prods = $total_orders = $total_users = $total_revenue = 0;
    $recent_orders = [];
}

$currency = defined('CURRENCY_SYMBOL') ? CURRENCY_SYMBOL : 'BDT ';
?>

<style>
    :root {
        --primary: #84cc16; /* Light Green */
        --primary-hover: #65a30d;
        --bg-light: #f8fafc;
        --card-bg: #ffffff;
        --border: #e2e8f0;
        --text-main: #1e293b;
        --text-muted: #64748b;
    }

    body {
        background-color: var(--bg-light) !important;
        color: var(--text-main) !important;
        font-family: 'Inter', sans-serif;
        margin: 0;
    }

    .dashboard-wrapper {
        max-width: 1100px;
        margin: 0 auto;
        padding: 20px;
    }

    .page-title {
        font-size: 1.5rem;
        font-weight: 800;
        margin-bottom: 25px;
        color: var(--text-main);
    }

    /* ================= Stats Cards UI ================= */
    .top-stats { 
        display: grid; 
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); 
        gap: 20px; 
        margin-bottom: 30px; 
    }
    .st-card { 
        background: var(--card-bg); 
        border: 1px solid var(--border); 
        border-radius: 12px; 
        padding: 24px 24px 60px 24px; 
        position: relative; 
        overflow: hidden;
        box-shadow: 0 1px 3px rgba(0,0,0,0.02);
    }
    .st-content {
        position: relative;
        z-index: 2;
    }
    .st-title { 
        font-size: 0.85rem; 
        color: var(--text-muted); 
        font-weight: 700; 
        margin-bottom: 8px; 
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    .st-value { 
        font-size: 2rem; 
        font-weight: 800; 
        color: var(--text-main); 
        margin-bottom: 12px; 
    }
    .st-sub { 
        font-size: 0.85rem; 
        display: flex; 
        align-items: center; 
        gap: 6px; 
        font-weight: 600; 
    }
    .text-up { color: #10b981; }
    .text-down { color: #ef4444; }
    
    /* Smooth Waves mimicking the UI */
    .st-wave { 
        position: absolute; 
        bottom: -1px; 
        left: 0; 
        width: 100%; 
        height: 50px;
        pointer-events: none;
        z-index: 1;
        display: block;
    }

    /* ================= Latest Orders UI ================= */
    .table-section {
        background: var(--card-bg);
        border: 1px solid var(--border);
        border-radius: 12px;
        padding: 24px;
        margin-bottom: 40px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.02);
    }
    .table-header-title {
        font-size: 1.2rem;
        font-weight: 700;
        color: var(--text-main);
        margin-bottom: 20px;
    }
    
    /* Search Bar in Table */
    .search-box-wrapper {
        position: relative;
        margin-bottom: 20px;
    }
    .search-box-wrapper input {
        width: 100%;
        background: #fafafa;
        border: 1px solid var(--border);
        border-radius: 8px;
        padding: 12px 12px 12px 40px;
        color: var(--text-main);
        font-size: 0.9rem;
        transition: 0.2s;
        box-sizing: border-box;
    }
    .search-box-wrapper input:focus {
        border-color: var(--primary);
        outline: none;
        background: #ffffff;
    }
    .search-box-wrapper svg {
        position: absolute;
        left: 14px;
        top: 50%;
        transform: translateY(-50%);
        color: var(--text-muted);
    }

    .dash-table-wrapper { 
        width: 100%; 
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .dash-table { width: 100%; border-collapse: collapse; min-width: 700px; }
    .dash-table th { 
        padding: 14px 15px; 
        font-size: 0.8rem; 
        color: var(--text-muted); 
        font-weight: 700; 
        border-bottom: 1px solid var(--border); 
        text-align: left;
        background: #fafafa;
        text-transform: uppercase;
    }
    .dash-table th svg { width: 12px; margin-left: 4px; color: #cbd5e1; vertical-align: middle; }
    
    .dash-table td { 
        padding: 16px 15px; 
        font-size: 0.9rem; 
        color: var(--text-main); 
        border-bottom: 1px solid var(--border); 
        font-weight: 500;
    }
    .dash-table tr:last-child td { border-bottom: none; }
    .dash-table tr:hover { background: #fdfdfd; }
    
    /* Status Badges */
    .badge-status { 
        padding: 6px 12px; 
        border-radius: 6px; 
        font-size: 0.75rem; 
        font-weight: 700; 
        display: inline-block;
        border: 1px solid transparent;
        text-transform: uppercase;
    }
    .badge-completed { 
        color: #10b981; 
        background: #ecfdf5; 
        border-color: #a7f3d0; 
    }
    .badge-processing { 
        color: #3b82f6; 
        background: #eff6ff; 
        border-color: #bfdbfe; 
    }

    .btn-open {
        color: var(--primary);
        background: #f4fce3;
        font-weight: 600;
        text-decoration: none;
        font-size: 0.8rem;
        padding: 6px 12px;
        border-radius: 6px;
        transition: 0.2s;
    }
    .btn-open:hover {
        background: #d9f99d;
        color: var(--primary-hover);
    }

    /* Mobile Pagination Style */
    .table-footer {
        display: flex;
        justify-content: center;
        margin-top: 20px;
    }
    .page-selector {
        background: #ffffff;
        border: 1px solid var(--border);
        color: var(--text-main);
        padding: 8px 12px;
        border-radius: 8px;
        font-size: 0.9rem;
        font-weight: 500;
        outline: none;
    }

    @media (max-width: 768px) {
        .top-stats { grid-template-columns: 1fr; gap: 15px; }
        .st-card { padding: 20px 20px 50px 20px; }
        .dashboard-wrapper { padding: 15px 10px; }
    }
</style>

<div class="dashboard-wrapper">
    <h1 class="page-title">Dashboard</h1>

    <div class="top-stats">
        <div class="st-card">
            <div class="st-content">
                <div class="st-title">Revenue</div>
                <div class="st-value"><?php echo $currency . number_format($total_revenue, 2); ?></div>
                <div class="st-sub text-down">
                    Decrease by -100% in this month 
                    <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2"><path d="M13 11L9 7L6 10L1 5m12 6h-4m4 0V7"/></svg>
                </div>
            </div>
            <svg class="st-wave" preserveAspectRatio="none" viewBox="0 0 1440 100"><path d="M0 80 Q 360 40 720 80 T 1440 80 V 100 H 0 Z" fill="rgba(239, 68, 68, 0.05)" stroke="#ef4444" stroke-width="2"/></svg>
        </div>

        <div class="st-card">
            <div class="st-content">
                <div class="st-title">New customers</div>
                <div class="st-value"><?php echo $total_users; ?></div>
                <div class="st-sub text-up">
                    Increase by 0% in this month
                    <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2"><path d="M1 11l5-5 3 3 7-7m0 0h-4m4 0v4"/></svg>
                </div>
            </div>
            <svg class="st-wave" preserveAspectRatio="none" viewBox="0 0 1440 100"><path d="M0 80 Q 360 100 720 60 T 1440 80 V 100 H 0 Z" fill="rgba(132, 204, 22, 0.1)" stroke="var(--primary)" stroke-width="2"/></svg>
        </div>

        <div class="st-card">
            <div class="st-content">
                <div class="st-title">New orders</div>
                <div class="st-value"><?php echo $total_orders; ?></div>
                <div class="st-sub text-down">
                    Decrease by -100% in this month
                    <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2"><path d="M13 11L9 7L6 10L1 5m12 6h-4m4 0V7"/></svg>
                </div>
            </div>
            <svg class="st-wave" preserveAspectRatio="none" viewBox="0 0 1440 100"><path d="M0 80 Q 360 40 720 80 T 1440 80 V 100 H 0 Z" fill="rgba(239, 68, 68, 0.05)" stroke="#ef4444" stroke-width="2"/></svg>
        </div>
    </div>

    <div class="table-section">
        <div class="table-header-title">Latest Orders</div>
        
        <div class="search-box-wrapper">
            <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><path d="M21 21l-4.35-4.35"/></svg>
            <input type="text" placeholder="Search orders...">
        </div>

        <div class="dash-table-wrapper">
            <table class="dash-table">
                <thead>
                    <tr>
                        <th>Order Date <svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg></th>
                        <th>Order ID <svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg></th>
                        <th>User <svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg></th>
                        <th>Status <svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg></th>
                        <th>Amount <svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 10l5 5 5-5z"/></svg></th>
                        <th style="text-align: right;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($recent_orders as $order): ?>
                    <tr>
                        <td><?php echo date('M d, Y', strtotime($order['created_at'])); ?></td>
                        <td style="font-family: monospace; font-weight: 600; color: var(--primary);"><?php echo $order['invoice_no']; ?></td>
                        <td><?php echo htmlspecialchars($order['user_name'] ?? 'Guest'); ?></td>
                        <td>
                            <?php if($order['status'] == 'approved'): ?>
                                <span class="badge-status badge-completed">COMPLETED</span>
                            <?php else: ?>
                                <span class="badge-status badge-processing">PROCESSING</span>
                            <?php endif; ?>
                        </td>
                        <td style="font-weight: 700;"><?php echo number_format($order['amount'], 2); ?></td>
                        <td style="text-align: right;">
                            <a href="manage-orders.php?id=<?php echo $order['id']; ?>" class="btn-open">Open</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    
                    <?php if(empty($recent_orders)): ?>
                        <tr><td colspan="6" style="text-align: center; padding: 40px; color: var(--text-muted); font-weight: 500;">No recent records found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div class="table-footer">
            <select class="page-selector">
                <option>5</option>
                <option>10</option>
                <option>20</option>
            </select>
        </div>
    </div>
</div>

<?php echo '</div></div></body></html>'; ?>
