@php
    $queryBase = request()->only([
        'invoice', 'q', 'client_name', 'invoice_number', 'status', 'type', 'city', 'item_name',
        'issued_from', 'issued_to', 'due_from', 'due_to',
    ]);
@endphp
<div class="billing-clients-sidebar__toolbar">
    <div class="dropdown flex-grow-1">
        <button type="button" class="btn btn-filter-clients dropdown-toggle w-100" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
            <span class="text-truncate">Filter Invoices</span>
            @if($hasFilters)<span class="billing-filter-dot"></span>@endif
        </button>
        <div class="dropdown-menu billing-clients-filter-menu shadow p-3 w-100" style="min-width:300px">
            <form method="GET" action="{{ route('admin.invoices.index') }}">
                @foreach(collect($queryBase)->except([
                    'client_name', 'invoice_number', 'status', 'type', 'city', 'item_name',
                    'issued_from', 'issued_to', 'due_from', 'due_to', 'q',
                ]) as $key => $val)
                    @if($val && $val !== 'all')<input type="hidden" name="{{ $key }}" value="{{ $val }}">@endif
                @endforeach
                <div class="mb-2">
                    <label class="form-label small mb-1">Client name</label>
                    <input type="text" name="client_name" class="form-control form-control-sm" value="{{ $filters['client_name'] ?? '' }}">
                </div>
                <div class="mb-2">
                    <label class="form-label small mb-1">Invoice no</label>
                    <input type="text" name="invoice_number" class="form-control form-control-sm" value="{{ $filters['invoice_number'] ?? '' }}">
                </div>
                <div class="mb-2">
                    <label class="form-label small mb-1">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="all">All</option>
                        @foreach($statuses as $key => $meta)
                            <option value="{{ $key }}" @selected(($filters['status'] ?? 'all') === $key)>{{ $meta['label'] }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label small mb-1">Type</label>
                    <select name="type" class="form-select form-select-sm">
                        <option value="all">All</option>
                        @foreach($invoiceTypes as $key => $label)
                            <option value="{{ $key }}" @selected(($filters['type'] ?? 'all') === $key)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-success btn-sm flex-grow-1">Apply</button>
                    <a href="{{ route('admin.invoices.index', collect($queryBase)->only(['invoice'])->filter()->all()) }}" class="btn btn-light btn-sm">Clear</a>
                </div>
            </form>
        </div>
    </div>
    <div class="dropdown">
        <button type="button" class="btn btn-new-client dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" title="New invoice">
            <i class="ri-add-line"></i><span class="d-none d-xl-inline ms-1">New</span>
        </button>
        <ul class="dropdown-menu dropdown-menu-end billing-invoice-new-menu shadow">
            @foreach($createTypes as $typeKey => $meta)
                <li>
                    <a class="dropdown-item py-2" href="{{ route('admin.invoices.create', ['type' => $typeKey]) }}">
                        <i class="{{ $meta['icon'] }} me-2 text-success"></i>
                        <span class="d-block fw-medium">{{ $meta['label'] }}</span>
                    </a>
                </li>
            @endforeach
        </ul>
    </div>
</div>
