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

    <div class="page-wide">
        <div class="page-header mb-6">
            <div>
                <h1 class="page-title">{{ __('inventory.movements.title') }}</h1>
                <p class="page-sub">{{ __('inventory.movements.sub') }}</p>
            </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.movements.list_title', ['count' => $movements->count()]) }}</div>
                <x-admin.dt-toolbar-actions />
            </div>
            <form method="GET" action="{{ route('admin.inventory.movements.index') }}" class="inv-filter">
                <label class="field inv-filter-select">
                    <span class="field-label">{{ __('inventory.movements.columns.store') }}</span>
                    <select name="store_id" class="pos-input" x-data="enhancedSelect()" onchange="this.form.submit()">
                        <option value="">{{ __('inventory.movements.filter.store_all') }}</option>
                        @foreach ($stores as $store)
                            <option value="{{ $store->id }}" @selected($filters['storeId'] === $store->id)>{{ $store->name }}</option>
                        @endforeach
                    </select>
                </label>
                <label class="field inv-filter-select">
                    <span class="field-label">{{ __('inventory.movements.columns.type') }}</span>
                    <select name="type" class="pos-input" x-data="enhancedSelect()" onchange="this.form.submit()">
                        <option value="">{{ __('inventory.movements.filter.type_all') }}</option>
                        @foreach ($types as $type)
                            <option value="{{ $type }}" @selected($filters['type'] === $type)>{{ __("inventory.movements.types.$type") }}</option>
                        @endforeach
                    </select>
                </label>
                <label class="field inv-filter-date">
                    <span class="field-label">{{ __('inventory.movements.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.movements.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.movements.filter.search') }}">
                    </span>
                </label>
                <x-admin.filter-reset route="admin.inventory.movements.index" />
            </form>

            @if ($movements->isEmpty())
                <div class="dt-empty">
                    <span class="dt-empty-icon"><x-icon name="list" class="w-5 h-5" /></span>
                    <div class="dt-empty-title">{{ __('inventory.movements.empty') }}</div>
                </div>
            @else
                <div class="dt-scroll">
                <table class="dt-table">
                    <thead>
                        <tr>
                            <th>{{ __('inventory.movements.columns.when') }}</th>
                            <th>{{ __('inventory.movements.columns.product') }}</th>
                            <th>{{ __('inventory.movements.columns.store') }}</th>
                            <th>{{ __('inventory.movements.columns.type') }}</th>
                            <th class="num">{{ __('inventory.movements.columns.delta') }}</th>
                            <th class="num">{{ __('inventory.movements.columns.after') }}</th>
                            <th class="num">{{ __('inventory.movements.columns.unit_cost') }}</th>
                            <th>{{ __('inventory.movements.columns.reference') }}</th>
                            <th>{{ __('inventory.movements.columns.by') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($movements as $m)
                            @php $delta = (float) $m->quantity_delta; @endphp
                            <tr data-dt-row
                                data-dt-name="{{ $m->product?->name ?? '' }}"
                                data-dt-id="{{ $m->id }}">
                                <td title="{{ format_datetime($m->created_at) }}">{{ $m->created_at?->diffForHumans() }}</td>
                                <td>
                                    <div class="store-row-name-line">
                                        <span class="store-row-name">{{ $m->product?->name }}</span>
                                        @if ($m->variant) <span class="fg-tertiary">· {{ $m->variant->label }}</span> @endif
                                    </div>
                                    <span class="mono fg-tertiary">{{ $m->variant?->sku ?? $m->product?->sku }}</span>
                                </td>
                                <td>{{ $m->store?->name }}</td>
                                <td>
                                    <span class="prod-badge prod-badge-muted">{{ __("inventory.movements.types.{$m->type}") }}</span>
                                </td>
                                <td class="num tnum {{ $delta >= 0 ? 'inv-delta-pos' : 'inv-delta-neg' }}">
                                    {{ $delta >= 0 ? '+' : '' }}{{ rtrim(rtrim(number_format($delta, 4, '.', ''), '0'), '.') ?: '0' }}
                                </td>
                                <td class="num tnum">{{ rtrim(rtrim(number_format((float) $m->quantity_after, 4, '.', ''), '0'), '.') ?: '0' }}</td>
                                <td class="num tnum fg-tertiary">{{ $m->unit_cost !== null ? format_money((float) $m->unit_cost) : '—' }}</td>
                                <td class="mono fg-tertiary">
                                    @if ($m->reference_type) {{ class_basename($m->reference_type) }}#{{ $m->reference_id }} @else — @endif
                                </td>
                                <td>{{ $m->creator?->name ?? '—' }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                </div>

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