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

if (!$pdo) {
    require __DIR__ . '/_header.php';
    echo '<div class="admin-card p-4"><h1 class="h4">MySQL setup required</h1><p>Orders 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[] = '(order_number LIKE ? OR customer_name LIKE ? OR customer_phone LIKE ? OR customer_email LIKE ? OR customer_city LIKE ?)';
    $like = '%' . $q . '%';
    $params = array_merge($params, array($like, $like, $like, $like, $like));
}
if ($status !== '') {
    $where[] = 'status = ?';
    $params[] = $status;
}

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

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">Orders / Inquiries</h1>
            <p class="admin-muted mb-0">Search customer requests and update inquiry status.</p>
        </div>
    </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 order, customer, phone, email, city"></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">
            <thead><tr><th>Order</th><th>Customer</th><th>Contact</th><th>City</th><th>Total</th><th>Payment</th><th>Status</th><th>Date</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($orders as $order): ?>
                <tr>
                    <td><strong><?= e($order['order_number']) ?></strong></td>
                    <td><?= e($order['customer_name']) ?></td>
                    <td><span class="small"><?= e($order['customer_phone']) ?><br><?= e($order['customer_email']) ?></span></td>
                    <td><?= e($order['customer_city']) ?></td>
                    <td><?= money($order['total']) ?></td>
                    <td><span class="badge badge-soft"><?= e(isset($order['payment_status']) ? $order['payment_status'] : 'Unpaid') ?></span><br><span class="small admin-muted"><?= e($order['payment_method']) ?></span></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>
                    <td class="text-end"><a href="order-view.php?id=<?= (int) $order['id'] ?>" class="btn btn-sm btn-outline-primary">View</a></td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$orders): ?><tr><td colspan="9" class="text-center admin-muted py-5">No orders found.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php require __DIR__ . '/_footer.php'; ?>
