<?php
require __DIR__ . '/_auth.php';
require_admin();

$pdo = db();
$title = 'Dashboard';
$stats = array('products' => 0, 'orders' => 0, 'pending' => 0, 'sales' => 0, 'reup' => 0, 'reup_pending' => 0, 'custom' => 0, 'custom_pending' => 0, 'customers' => 0);
$recentOrders = array();
$recentReup = array();
$recentCustom = array();

if ($pdo) {
    try {
        $stats['products'] = (int) $pdo->query('SELECT COUNT(*) FROM products WHERE is_active = 1')->fetchColumn();
        $stats['customers'] = (int) $pdo->query('SELECT COUNT(*) FROM customers')->fetchColumn();
        $stats['orders'] = (int) $pdo->query('SELECT COUNT(*) FROM orders')->fetchColumn();
        $stats['pending'] = (int) $pdo->query("SELECT COUNT(*) FROM orders WHERE status IN ('Pending quotation','New','Pending')")->fetchColumn();
        $stats['sales'] = (float) $pdo->query('SELECT COALESCE(SUM(total),0) FROM orders')->fetchColumn();
        $stats['reup'] = (int) $pdo->query('SELECT COUNT(*) FROM reup_requests')->fetchColumn();
        $stats['reup_pending'] = (int) $pdo->query("SELECT COUNT(*) FROM reup_requests WHERE status IN ('For review','Need more photos')")->fetchColumn();
        $stats['custom'] = (int) $pdo->query('SELECT COUNT(*) FROM custom_sofa_quotes')->fetchColumn();
        $stats['custom_pending'] = (int) $pdo->query("SELECT COUNT(*) FROM custom_sofa_quotes WHERE status IN ('For design review','Need more details','Need room measurement')")->fetchColumn();
        $stmt = $pdo->query('SELECT id, order_number, customer_name, customer_city, total, status, created_at FROM orders ORDER BY created_at DESC LIMIT 8');
        $recentOrders = $stmt->fetchAll();
        $reupStmt = $pdo->query('SELECT id, request_number, customer_name, customer_city, estimate_low, estimate_high, status, created_at FROM reup_requests ORDER BY created_at DESC LIMIT 8');
        $recentReup = $reupStmt->fetchAll();
        $customStmt = $pdo->query('SELECT id, request_number, customer_name, customer_city, sofa_type, design_style, estimate_low, estimate_high, status, created_at FROM custom_sofa_quotes ORDER BY created_at DESC LIMIT 8');
        $recentCustom = $customStmt->fetchAll();
    } catch (Exception $e) {
        $pdo = null;
    }
}

require __DIR__ . '/_header.php';
?>
<?php if (!$pdo): ?>
    <div class="admin-card p-4">
        <h1 class="h4">MySQL setup required</h1>
        <p>Import <code>database/schema.sql</code> in phpMyAdmin, then update <code>data/database.php</code> and set <code>enabled</code> to <code>true</code>.</p>
        <pre class="code-box">'enabled' =&gt; true,
'host' =&gt; 'localhost',
'database' =&gt; 'your_db',
'username' =&gt; 'your_user',
'password' =&gt; 'your_password',</pre>
    </div>
<?php else: ?>
    <div class="row g-3 mb-4">
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Active Products</span><h2><?= (int) $stats['products'] ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Customers</span><h2><?= (int) $stats['customers'] ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Total Orders</span><h2><?= (int) $stats['orders'] ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Pending</span><h2><?= (int) $stats['pending'] ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Inquiry Value</span><h2><?= money($stats['sales']) ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Custom Sofa</span><h2><?= (int) $stats['custom'] ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Custom Pending</span><h2><?= (int) $stats['custom_pending'] ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Re-Up Requests</span><h2><?= (int) $stats['reup'] ?></h2></div></div>
        <div class="col-md-3"><div class="admin-card p-4"><span class="admin-muted">Re-Up Pending</span><h2><?= (int) $stats['reup_pending'] ?></h2></div></div>
    </div>

    <div class="admin-card p-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div>
                <h1 class="h4 mb-1">Recent orders</h1>
                <p class="admin-muted mb-0">Latest customer inquiries from the online store.</p>
            </div>
            <a href="orders.php" class="btn btn-outline-primary">View all</a>
        </div>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead><tr><th>Order</th><th>Customer</th><th>City</th><th>Total</th><th>Status</th><th>Date</th></tr></thead>
                <tbody>
                <?php foreach ($recentOrders as $order): ?>
                    <tr>
                        <td><a href="order-view.php?id=<?= (int) $order['id'] ?>"><?= e($order['order_number']) ?></a></td>
                        <td><?= e($order['customer_name']) ?></td>
                        <td><?= e($order['customer_city']) ?></td>
                        <td><?= money($order['total']) ?></td>
                        <td><span class="badge badge-soft"><?= e($order['status']) ?></span></td>
                        <td><?= e(date('M d, Y h:i A', strtotime($order['created_at']))) ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$recentOrders): ?>
                    <tr><td colspan="6" class="text-center admin-muted py-5">No orders yet.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>


    <div class="admin-card p-4 mt-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div>
                <h1 class="h4 mb-1">Recent custom sofa requests</h1>
                <p class="admin-muted mb-0">Latest personalized sofa quotation requests.</p>
            </div>
            <a href="custom-quotes.php" class="btn btn-outline-primary">View all</a>
        </div>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead><tr><th>Request</th><th>Customer</th><th>City</th><th>Sofa</th><th>Estimate</th><th>Status</th><th>Date</th></tr></thead>
                <tbody>
                <?php foreach ($recentCustom as $request): ?>
                    <tr>
                        <td><a href="custom-quote-view.php?id=<?= (int) $request['id'] ?>"><?= e($request['request_number']) ?></a></td>
                        <td><?= e($request['customer_name']) ?></td>
                        <td><?= e($request['customer_city']) ?></td>
                        <td><?= e($request['sofa_type']) ?><br><span class="small admin-muted"><?= e($request['design_style']) ?></span></td>
                        <td><?= money($request['estimate_low']) ?> - <?= money($request['estimate_high']) ?></td>
                        <td><span class="badge badge-soft"><?= e($request['status']) ?></span></td>
                        <td><?= e(date('M d, Y h:i A', strtotime($request['created_at']))) ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$recentCustom): ?>
                    <tr><td colspan="7" class="text-center admin-muted py-5">No custom sofa requests yet.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>


    <div class="admin-card p-4 mt-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div>
                <h1 class="h4 mb-1">Recent Re-Up requests</h1>
                <p class="admin-muted mb-0">Latest old-sofa repair and makeover inquiries.</p>
            </div>
            <a href="reup.php" class="btn btn-outline-primary">View all</a>
        </div>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead><tr><th>Request</th><th>Customer</th><th>City</th><th>Estimate</th><th>Status</th><th>Date</th></tr></thead>
                <tbody>
                <?php foreach ($recentReup as $request): ?>
                    <tr>
                        <td><a href="reup-view.php?id=<?= (int) $request['id'] ?>"><?= e($request['request_number']) ?></a></td>
                        <td><?= e($request['customer_name']) ?></td>
                        <td><?= e($request['customer_city']) ?></td>
                        <td><?= money($request['estimate_low']) ?> - <?= money($request['estimate_high']) ?></td>
                        <td><span class="badge badge-soft"><?= e($request['status']) ?></span></td>
                        <td><?= e(date('M d, Y h:i A', strtotime($request['created_at']))) ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$recentReup): ?>
                    <tr><td colspan="6" class="text-center admin-muted py-5">No Re-Up requests yet.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

<?php endif; ?>
<?php require __DIR__ . '/_footer.php'; ?>
