<x-admin-layout
    active="stock-transfers"
    :title="__('inventory.transfers.title')"
    :crumbs="[
        ['label' => __('admin.shell.brand_sub')],
        ['label' => __('inventory.transfers.crumb_parent')],
        ['label' => __('inventory.transfers.title')],
    ]">

    <div class="page-wide">
        <div class="page-header mb-6">
            <div>
                <h1 class="page-title">{{ __('inventory.transfers.title') }}</h1>
                <p class="page-sub">{{ __('inventory.transfers.sub') }}</p>
            </div>
            <div class="flex items-center gap-2">
                <div class="dropdown" x-data="dropdown">
                    <button type="button"
                            class="pos-btn pos-btn-sm pos-btn-ghost"
                            @click="toggle()"
                            :aria-expanded="open">
                        <x-icon name="download" class="w-4 h-4" />
                        {{ __('inventory.transfers.actions.export') }}
                        <x-icon name="chevron" class="w-4 h-4" />
                    </button>
                    <div class="dropdown-panel"
                         x-show="open"
                         x-cloak
                         @click.outside="close()"
                         @keydown.escape.window="close()">
                        <a href="{{ route('admin.inventory.transfers.export', ['format' => 'csv']) }}"
                           class="dropdown-item"
                           @click="close()">
                            <span class="dropdown-item-label">{{ __('inventory.transfers.actions.export_csv') }}</span>
                        </a>
                        <a href="{{ route('admin.inventory.transfers.export', ['format' => 'xlsx']) }}"
                           class="dropdown-item"
                           @click="close()">
                            <span class="dropdown-item-label">{{ __('inventory.transfers.actions.export_xlsx') }}</span>
                        </a>
                    </div>
                </div>

                @can('create', App\Models\StockTransfer::class)
                    <a href="{{ route('admin.inventory.transfers.create') }}" class="pos-btn pos-btn-sm pos-btn-primary">
                        <x-icon name="plus" class="w-4 h-4" />
                        {{ __('inventory.transfers.new') }}
                    </a>
                @endcan
            </div>
        </div>

        <x-admin.summary-cards :cards="$summaryCards" />

        <div class="card card-pad-0"
             x-data="dataTable({ rowsSelector: 'tbody > tr[data-dt-row]' })">
            <div class="dt-toolbar">
                <div class="dt-toolbar-title">{{ __('inventory.transfers.list_title', ['count' => $transfers->count()]) }}</div>
                <x-admin.dt-toolbar-actions />
            </div>

            <form method="GET" action="{{ route('admin.inventory.transfers.index') }}" class="inv-filter">
                <label class="field inv-filter-select">
                    <span class="field-label">{{ __('inventory.transfers.columns.from_store') }}</span>
                    <select name="from_store_id" class="pos-input" x-data="enhancedSelect()" onchange="this.form.submit()">
                        <option value="">{{ __('inventory.transfers.filter.from_store_all') }}</option>
                        @foreach ($stores as $store)
                            <option value="{{ $store->id }}" @selected($filters['fromStoreId'] === $store->id)>{{ $store->name }}</option>
                        @endforeach
                    </select>
                </label>
                <label class="field inv-filter-select">
                    <span class="field-label">{{ __('inventory.transfers.columns.to_store') }}</span>
                    <select name="to_store_id" class="pos-input" x-data="enhancedSelect()" onchange="this.form.submit()">
                        <option value="">{{ __('inventory.transfers.filter.to_store_all') }}</option>
                        @foreach ($stores as $store)
                            <option value="{{ $store->id }}" @selected($filters['toStoreId'] === $store->id)>{{ $store->name }}</option>
                        @endforeach
                    </select>
                </label>
                <label class="field inv-filter-select">
                    <span class="field-label">{{ __('inventory.transfers.columns.status') }}</span>
                    <select name="status" class="pos-input" x-data="enhancedSelect()" onchange="this.form.submit()">
                        <option value="">{{ __('inventory.transfers.filter.status_all') }}</option>
                        <option value="draft"      @selected($filters['status'] === 'draft')>{{ __('inventory.transfers.status.draft') }}</option>
                        <option value="in_transit" @selected($filters['status'] === 'in_transit')>{{ __('inventory.transfers.status.in_transit') }}</option>
                        <option value="received"   @selected($filters['status'] === 'received')>{{ __('inventory.transfers.status.received') }}</option>
                        <option value="cancelled"  @selected($filters['status'] === 'cancelled')>{{ __('inventory.transfers.status.cancelled') }}</option>
                    </select>
                </label>
                <label class="field inv-filter-date">
                    <span class="field-label">{{ __('inventory.transfers.filter.date_from') }}</span>
                    <input type="text" name="from" value="{{ $filters['from'] }}"
                           class="pos-input js-datepicker"
                           placeholder="{{ __('table.filter_date_from_placeholder') }}"
                           data-fp-submit-on-change="1">
                </label>
                <label class="field inv-filter-date">
                    <span class="field-label">{{ __('inventory.transfers.filter.date_to') }}</span>
                    <input type="text" name="to" value="{{ $filters['to'] }}"
                           class="pos-input js-datepicker"
                           placeholder="{{ __('table.filter_date_to_placeholder') }}"
                           data-fp-submit-on-change="1">
                </label>
                <label class="field inv-filter-search">
                    <span class="field-label">&nbsp;</span>
                    <span class="inv-search">
                        <span class="inv-search-icon"><x-icon name="search" class="w-4 h-4" /></span>
                        <input type="search" name="q" value="{{ $filters['q'] }}" class="pos-input"
                               placeholder="{{ __('inventory.transfers.filter.search') }}">
                    </span>
                </label>
                <x-admin.filter-reset route="admin.inventory.transfers.index" />
            </form>

            @if ($transfers->isEmpty())
                <div class="dt-empty">
                    <span class="dt-empty-icon"><x-icon name="truck" class="w-5 h-5" /></span>
                    <div class="dt-empty-title">{{ __('inventory.transfers.empty_state.title') }}</div>
                    <div class="dt-empty-sub">{{ __('inventory.transfers.empty_state.sub') }}</div>
                </div>
            @else
                <div class="dt-scroll">
                <table class="dt-table">
                    <thead>
                        <tr>
                            <th>{{ __('inventory.transfers.columns.number') }}</th>
                            <th>{{ __('inventory.transfers.columns.date') }}</th>
                            <th>{{ __('inventory.transfers.columns.from_store') }}</th>
                            <th>{{ __('inventory.transfers.columns.to_store') }}</th>
                            <th class="num">{{ __('inventory.transfers.columns.items') }}</th>
                            <th>{{ __('inventory.transfers.columns.status') }}</th>
                            <th>{{ __('inventory.transfers.columns.by') }}</th>
                            <th class="dt-actions-col"><span class="sr-only">{{ __('inventory.transfers.columns.actions') }}</span></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($transfers as $t)
                            <tr class="prod-row" data-dt-row
                                data-dt-name="{{ $t->number ?? '' }}"
                                data-dt-id="{{ $t->id }}"
                                @click="window.location.href = {{ \Illuminate\Support\Js::from(route('admin.inventory.transfers.show', $t)) }}">
                                <td class="mono">{{ $t->number }}</td>
                                <td>{{ format_date($t->transfer_date) }}</td>
                                <td>{{ $t->fromStore?->name ?? '—' }}</td>
                                <td>{{ $t->toStore?->name ?? '—' }}</td>
                                <td class="num tnum">{{ $t->items_count }}</td>
                                <td>
                                    @if ($t->isReceived())
                                        <span class="prod-badge prod-badge-positive">{{ __('inventory.transfers.status.received') }}</span>
                                    @elseif ($t->isInTransit())
                                        <span class="prod-badge prod-badge-warning">{{ __('inventory.transfers.status.in_transit') }}</span>
                                    @elseif ($t->isCancelled())
                                        <span class="prod-badge prod-badge-negative">{{ __('inventory.transfers.status.cancelled') }}</span>
                                    @else
                                        <span class="prod-badge prod-badge-muted">{{ __('inventory.transfers.status.draft') }}</span>
                                    @endif
                                </td>
                                <td>{{ $t->creator?->name ?? '—' }}</td>
                                <td>
                                    <x-admin.row-actions>
                                        @if ($t->isDraft())
                                            <x-admin.row-action :href="route('admin.inventory.transfers.edit', $t)" icon="edit" :label="__('inventory.transfers.actions.edit')" />
                                            <div class="row-action-sep"></div>
                                            <x-admin.row-action icon="trash" variant="danger" :label="__('inventory.transfers.actions.delete')"
                                                @click="$store.confirm.show({
                                                    title: {{ \Illuminate\Support\Js::from(__('inventory.transfers.confirm_delete.title', ['number' => $t->number])) }},
                                                    message: {{ \Illuminate\Support\Js::from(__('inventory.transfers.confirm_delete.message')) }},
                                                    intent: 'danger',
                                                    confirmLabel: {{ \Illuminate\Support\Js::from(__('inventory.transfers.confirm_delete.confirm')) }},
                                                    onConfirm: () => $deleteForm({{ \Illuminate\Support\Js::from(route('admin.inventory.transfers.destroy', $t)) }}),
                                                })" />
                                        @else
                                            <x-admin.row-action :href="route('admin.inventory.transfers.show', $t)" icon="eye" :label="__('inventory.transfers.actions.view')" />
                                        @endif
                                    </x-admin.row-actions>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                </div>

                <x-admin.dt-pager />
            @endif
        </div>
    </div>
</x-admin-layout>
