<?php
// $formAction, $from, $to, $search, $showStatus (optional bool), $statuses (optional array)
$showStatus = $showStatus ?? false;
$statuses   = $statuses   ?? [];
$statusVal  = $statusVal  ?? '';
$statusLabels = [
    'pending'=>'Pending','confirmed'=>'Confirmed','cooking'=>'Cooking','ready'=>'Ready',
    'picked_up'=>'Picked Up','on_the_way'=>'On the Way','delivered'=>'Delivered','cancelled'=>'Cancelled',
];
?>
<div class="card mb-4">
    <div class="card-body py-3">
        <form method="GET" action="<?= $formAction ?>" class="row g-2 align-items-end">
            <div class="col-6 col-md-2">
                <label class="form-label" style="font-size:.78rem;">From</label>
                <input type="date" name="from" class="form-control form-control-sm" value="<?= e($from) ?>">
            </div>
            <div class="col-6 col-md-2">
                <label class="form-label" style="font-size:.78rem;">To</label>
                <input type="date" name="to" class="form-control form-control-sm" value="<?= e($to) ?>">
            </div>
            <?php if ($showStatus): ?>
            <div class="col-6 col-md-2">
                <label class="form-label" style="font-size:.78rem;">Status</label>
                <select name="status" class="form-select form-select-sm">
                    <option value="">All</option>
                    <?php foreach ($statuses as $s): ?>
                    <option value="<?= $s ?>" <?= $statusVal === $s ? 'selected':'' ?>><?= $statusLabels[$s] ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <?php endif; ?>
            <div class="col-12 col-md-3">
                <label class="form-label" style="font-size:.78rem;">Search</label>
                <input type="text" name="search" class="form-control form-control-sm"
                       placeholder="Search…" value="<?= e($search) ?>">
            </div>
            <div class="col-12 col-md-3 d-flex gap-2">
                <button type="submit" class="btn btn-accent btn-sm px-3">
                    <i class="bi bi-funnel me-1"></i> Apply
                </button>
                <a href="<?= $formAction ?>" class="btn btn-outline-secondary btn-sm">Reset</a>
                <a href="<?= $formAction . '?' . http_build_query(array_merge(
                    ['from'=>$from,'to'=>$to,'search'=>$search],
                    $showStatus ? ['status'=>$statusVal] : [],
                    ['export'=>'csv']
                )) ?>"
                   class="btn btn-outline-success btn-sm ms-auto">
                    <i class="bi bi-download me-1"></i> CSV
                </a>
            </div>
        </form>
    </div>
</div>
