<?php
require __DIR__ . '/includes/functions.php';
customer_require_login();
$currentPage = 'account';
$pageTitle = 'My Furnify Dashboard';
$customer = customer_current();
$counts = customer_dashboard_counts($customer);
$pdo = db();
$recentOrders = array();
$recentReup = array();
$recentCustom = array();
if ($pdo && $customer) {
    $id = (int) $customer['id'];
    $email = (string) $customer['email'];
    try {
        $stmt = $pdo->prepare('SELECT id, order_number, total, status, payment_status, balance_due, created_at FROM orders WHERE customer_id = ? OR customer_email = ? ORDER BY created_at DESC LIMIT 5');
        $stmt->execute(array($id, $email));
        $recentOrders = $stmt->fetchAll();
        $stmt = $pdo->prepare('SELECT id, request_number, estimate_low, estimate_high, final_quote, status, created_at FROM reup_requests WHERE customer_id = ? OR customer_email = ? ORDER BY created_at DESC LIMIT 5');
        $stmt->execute(array($id, $email));
        $recentReup = $stmt->fetchAll();
        $stmt = $pdo->prepare('SELECT id, request_number, sofa_type, estimate_low, estimate_high, final_quote, status, created_at FROM custom_sofa_quotes WHERE customer_id = ? OR customer_email = ? ORDER BY created_at DESC LIMIT 5');
        $stmt->execute(array($id, $email));
        $recentCustom = $stmt->fetchAll();
    } catch (Exception $e) {}
}
require __DIR__ . '/includes/header.php';
?>
<section class="page-section">
    <div class="container">
        <div class="section-heading split-heading">
            <div>
                <p class="eyebrow">Customer dashboard</p>
                <h1>Hello, <?= e(array_get($customer, 'full_name', 'Customer')) ?></h1>
                <p class="lead">Track your sofa orders, Re-Up makeovers, custom quotes, payments, and account verification.</p>
            </div>
            <div class="d-flex flex-wrap gap-2">
                <a href="customer-profile.php" class="btn btn-outline-primary">Profile</a>
                <a href="logout.php" class="btn btn-primary">Logout</a>
            </div>
        </div>
        <?php if (isset($_GET['registered'])): ?>
            <div class="alert alert-success">Account created. Please verify your email address.</div>
        <?php endif; ?>
        <?php if (!customer_is_email_verified($customer)): ?>
            <div class="alert alert-warning d-flex flex-wrap justify-content-between align-items-center gap-2">
                <div><strong>Email not verified.</strong> Verify your email so your account is trusted for quotes, payments, and order tracking.</div>
                <a class="btn btn-sm btn-primary" href="resend-verification.php?csrf_token=<?= e(csrf_token()) ?>">Resend verification</a>
            </div>
            <?php start_secure_session(); if (customer_show_debug_verification_link() && !empty($_SESSION['last_verification_link'])): ?>
                <div class="alert alert-info"><strong>Testing link:</strong> <a href="<?= e($_SESSION['last_verification_link']) ?>"><?= e($_SESSION['last_verification_link']) ?></a><br><span class="small">For production, set <code>show_debug_verification_link</code> to <code>false</code> in <code>data/settings.php</code> after email sending is configured.</span></div>
            <?php endif; ?>
        <?php else: ?>
            <div class="alert alert-success"><i class="bi bi-patch-check" aria-hidden="true"></i> Your email is verified.</div>
        <?php endif; ?>
        <div class="row g-3 mb-4">
            <div class="col-md-3"><div class="premium-panel p-4"><span class="text-muted">Orders</span><h2><?= (int) $counts['orders'] ?></h2></div></div>
            <div class="col-md-3"><div class="premium-panel p-4"><span class="text-muted">Unpaid / balance</span><h2><?= (int) $counts['unpaid'] ?></h2></div></div>
            <div class="col-md-3"><div class="premium-panel p-4"><span class="text-muted">Custom quotes</span><h2><?= (int) $counts['custom'] ?></h2></div></div>
            <div class="col-md-3"><div class="premium-panel p-4"><span class="text-muted">Re-Up requests</span><h2><?= (int) $counts['reup'] ?></h2></div></div>
        </div>
        <div class="row g-4">
            <div class="col-lg-4"><div class="premium-panel p-4 h-100"><h2 class="h5">Quick actions</h2><div class="d-grid gap-2 mt-3"><a href="index.php#catalog" class="btn btn-outline-primary">Shop sofas</a><a href="custom-sofa.php" class="btn btn-outline-primary">Request custom sofa quote</a><a href="reup.php" class="btn btn-outline-primary">Submit Re-Up request</a><a href="checkout.php" class="btn btn-primary">Go to checkout</a></div></div></div>
            <div class="col-lg-8"><div class="premium-panel p-4 h-100"><h2 class="h5">Recent orders</h2><div class="table-responsive"><table class="table align-middle"><thead><tr><th>Order</th><th>Total</th><th>Payment</th><th>Status</th><th>Date</th></tr></thead><tbody><?php foreach ($recentOrders as $row): ?><tr><td><?= e($row['order_number']) ?></td><td><?= money($row['total']) ?></td><td><?= e($row['payment_status']) ?></td><td><span class="badge text-bg-light"><?= e($row['status']) ?></span></td><td><?= e(date('M d, Y', strtotime($row['created_at']))) ?></td></tr><?php endforeach; ?><?php if (!$recentOrders): ?><tr><td colspan="5" class="text-center text-muted py-4">No orders yet.</td></tr><?php endif; ?></tbody></table></div><a href="customer-orders.php" class="btn btn-sm btn-outline-primary">View all orders</a></div></div>
            <div class="col-lg-6"><div class="premium-panel p-4 h-100"><h2 class="h5">Recent custom sofa quotes</h2><div class="table-responsive"><table class="table align-middle"><thead><tr><th>Request</th><th>Estimate</th><th>Status</th></tr></thead><tbody><?php foreach ($recentCustom as $row): ?><tr><td><?= e($row['request_number']) ?><br><span class="small text-muted"><?= e($row['sofa_type']) ?></span></td><td><?= $row['final_quote'] !== null ? money($row['final_quote']) : money($row['estimate_low']) . ' - ' . money($row['estimate_high']) ?></td><td><span class="badge text-bg-light"><?= e($row['status']) ?></span></td></tr><?php endforeach; ?><?php if (!$recentCustom): ?><tr><td colspan="3" class="text-center text-muted py-4">No custom requests yet.</td></tr><?php endif; ?></tbody></table></div><a href="customer-custom.php" class="btn btn-sm btn-outline-primary">View custom quotes</a></div></div>
            <div class="col-lg-6"><div class="premium-panel p-4 h-100"><h2 class="h5">Recent Re-Up requests</h2><div class="table-responsive"><table class="table align-middle"><thead><tr><th>Request</th><th>Estimate</th><th>Status</th></tr></thead><tbody><?php foreach ($recentReup as $row): ?><tr><td><?= e($row['request_number']) ?></td><td><?= $row['final_quote'] !== null ? money($row['final_quote']) : money($row['estimate_low']) . ' - ' . money($row['estimate_high']) ?></td><td><span class="badge text-bg-light"><?= e($row['status']) ?></span></td></tr><?php endforeach; ?><?php if (!$recentReup): ?><tr><td colspan="3" class="text-center text-muted py-4">No Re-Up requests yet.</td></tr><?php endif; ?></tbody></table></div><a href="customer-reup.php" class="btn btn-sm btn-outline-primary">View Re-Up requests</a></div></div>
        </div>
    </div>
</section>
<?php require __DIR__ . '/includes/footer.php'; ?>
