<?php
require __DIR__ . '/_auth.php';
require_admin();
$pdo = db();
$title = 'Custom Sofa Quotes';

if (!$pdo) {
    require __DIR__ . '/_header.php';
    echo '<div class="admin-card p-4"><h1 class="h4">MySQL setup required</h1><p>Custom Sofa admin needs MySQL. Import <code>database/schema.sql</code>, then edit <code>data/database.php</code>.</p></div>';
    require __DIR__ . '/_footer.php';
    exit;
}

$q = trim((string) (isset($_GET['q']) ? $_GET['q'] : ''));
$status = trim((string) (isset($_GET['status']) ? $_GET['status'] : ''));
$params = array();
$where = array();

if ($q !== '') {
    $where[] = '(request_number LIKE ? OR customer_name LIKE ? OR customer_phone LIKE ? OR customer_email LIKE ? OR customer_city LIKE ? OR sofa_type LIKE ? OR design_style LIKE ?)';
    $like = '%' . $q . '%';
    $params = array_merge($params, array($like, $like, $like, $like, $like, $like, $like));
}
if ($status !== '') {
    $where[] = 'status = ?';
    $params[] = $status;
}

$sql = 'SELECT * FROM custom_sofa_quotes';
if ($where) {
    $sql .= ' WHERE ' . implode(' AND ', $where);
}
$sql .= ' ORDER BY created_at DESC LIMIT 200';
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$requests = $stmt->fetchAll();
$statuses = $pdo->query('SELECT DISTINCT status FROM custom_sofa_quotes ORDER BY status ASC')->fetchAll();
$typeOptions = custom_sofa_type_options();
$styleOptions = custom_style_options();
$materialOptions = custom_material_options();

require __DIR__ . '/_header.php';
?>
<div class="admin-card p-4">
    <div class="d-flex flex-wrap gap-2 justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h4 mb-1">Custom Sofa Quotes</h1>
            <p class="admin-muted mb-0">Review made-to-order sofa requests, reference photos, dimensions, and estimate ranges.</p>
        </div>
        <a href="../custom-sofa.php" target="_blank" class="btn btn-outline-primary"><i class="bi bi-box-arrow-up-right"></i> Open Custom Sofa Form</a>
    </div>

    <form class="row g-2 mb-4" method="get">
        <div class="col-md-7"><input class="form-control" name="q" value="<?= e($q) ?>" placeholder="Search request, customer, phone, city, style"></div>
        <div class="col-md-3">
            <select class="form-select" name="status">
                <option value="">All status</option>
                <?php foreach ($statuses as $row): ?>
                    <option value="<?= e($row['status']) ?>" <?= $status === $row['status'] ? 'selected' : '' ?>><?= e($row['status']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2"><button class="btn btn-primary w-100" type="submit">Search</button></div>
    </form>

    <div class="table-responsive">
        <table class="table align-middle admin-table">
            <thead><tr><th>Photo</th><th>Request</th><th>Customer</th><th>Sofa</th><th>Size</th><th>Estimate</th><th>Status</th><th>Date</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($requests as $request): ?>
                <tr>
                    <td><?php if ($request['inspiration_photo_path']): ?><img src="../<?= e($request['inspiration_photo_path']) ?>" alt="Inspiration photo"><?php endif; ?></td>
                    <td><strong><?= e($request['request_number']) ?></strong></td>
                    <td><?= e($request['customer_name']) ?><br><span class="small admin-muted"><?= e($request['customer_phone']) ?><br><?= e($request['customer_city']) ?></span></td>
                    <td><?= e(isset($typeOptions[$request['sofa_type']]) ? $typeOptions[$request['sofa_type']] : $request['sofa_type']) ?><br><span class="small admin-muted"><?= e(isset($styleOptions[$request['design_style']]) ? $styleOptions[$request['design_style']] : $request['design_style']) ?></span></td>
                    <td><?= e(number_format((float) $request['width_cm'], 0)) ?> x <?= e(number_format((float) $request['depth_cm'], 0)) ?> x <?= e(number_format((float) $request['height_cm'], 0)) ?> cm</td>
                    <td><?= money($request['estimate_low']) ?><br><span class="small admin-muted">to <?= money($request['estimate_high']) ?></span></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>
                    <td class="text-end"><a href="custom-quote-view.php?id=<?= (int) $request['id'] ?>" class="btn btn-sm btn-outline-primary">View</a></td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$requests): ?><tr><td colspan="9" class="text-center admin-muted py-5">No custom sofa requests found.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php require __DIR__ . '/_footer.php'; ?>
