<?php
/**
 * Shared report filter toolbar.
 *
 * @var string $actionPath  e.g. sale/reports/sales-performance
 * @var array  $filters
 * @var bool   $showCompany
 * @var bool   $showRep
 * @var bool   $showGroup
 * @var bool   $showSource
 */
$showCompany = $showCompany ?? true;
$showRep     = $showRep ?? false;
$showGroup   = $showGroup ?? false;
$showSource  = $showSource ?? false;
$companies   = $companies ?? [];
$reps        = $reps ?? [];
$groups      = $groups ?? [];
$sources     = $sources ?? [];
?>
<div class="enterprise-data-panel__toolbar">
    <form action="<?= base_url($actionPath) ?>" method="get">
        <div class="enterprise-filter-grid">
            <div>
                <label class="enterprise-field-label">From</label>
                <input type="date" name="date_from" class="form-control form-control-sm"
                       value="<?= esc($filters['date_from']) ?>">
            </div>
            <div>
                <label class="enterprise-field-label">To</label>
                <input type="date" name="date_to" class="form-control form-control-sm"
                       value="<?= esc($filters['date_to']) ?>">
            </div>
            <?php if ($showCompany): ?>
                <div>
                    <label class="enterprise-field-label">Company</label>
                    <select name="company_id" class="form-select form-select-sm">
                        <option value="">All companies</option>
                        <?php foreach ($companies as $c): ?>
                            <option value="<?= $c['id'] ?>" <?= (string) ($filters['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                                <?= esc($c['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>
            <?php if ($showRep): ?>
                <div>
                    <label class="enterprise-field-label">Sales Rep</label>
                    <select name="rep_id" class="form-select form-select-sm">
                        <option value="">All reps</option>
                        <?php foreach ($reps as $r): ?>
                            <option value="<?= $r['id'] ?>" <?= (string) ($filters['rep_id'] ?? '') === (string) $r['id'] ? 'selected' : '' ?>>
                                <?= esc($r['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>
            <?php if ($showGroup): ?>
                <div>
                    <label class="enterprise-field-label">Customer Group</label>
                    <select name="group_id" class="form-select form-select-sm">
                        <option value="">All groups</option>
                        <?php foreach ($groups as $g): ?>
                            <option value="<?= $g['id'] ?>" <?= (string) ($filters['group_id'] ?? '') === (string) $g['id'] ? 'selected' : '' ?>>
                                <?= esc($g['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>
            <?php if ($showSource): ?>
                <div>
                    <label class="enterprise-field-label">Lead Source</label>
                    <select name="source_id" class="form-select form-select-sm">
                        <option value="">All sources</option>
                        <?php foreach ($sources as $s): ?>
                            <option value="<?= $s['id'] ?>" <?= (string) ($filters['source_id'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                                <?= esc($s['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>
            <div class="enterprise-filter-actions">
                <button type="submit" class="btn btn-sm btn-primary">Apply</button>
                <a href="<?= base_url($actionPath) ?>" class="btn btn-sm btn-outline-secondary">Reset</a>
                <?= view('Modules\Reports\Views\partials\export_buttons', [
                    'actionPath' => $actionPath,
                    'query'      => $filters,
                ]) ?>
            </div>
        </div>
    </form>
</div>
