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

    <div class="page-wide"
         x-data="dataTable({ rowsSelector: 'tbody > tr[data-dt-row]' })">
        <div class="page-header mb-6">
            <div>
                <h1 class="page-title">{{ __('customer_payments.title') }}</h1>
                <p class="page-sub">{{ __('customer_payments.sub') }}</p>
            </div>
            @can('create', App\Models\SalePayment::class)
                <a href="{{ route('admin.customer-payments.create') }}" class="pos-btn pos-btn-sm pos-btn-primary">
                    <x-icon name="plus" class="w-4 h-4" />
                    {{ __('customer_payments.new') }}
                </a>
            @endcan
        </div>

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

        <div class="card card-pad-0">
            <div class="dt-toolbar">
                <div class="dt-toolbar-title">{{ __('customer_payments.list.title') }} (<span x-text="rowCount">{{ $payments->count() }}</span>)</div>
                <x-admin.dt-toolbar-actions />
            </div>
            <form method="GET" action="{{ route('admin.customer-payments.index') }}" class="inv-filter">
                <label class="field inv-filter-select">
                    <span class="field-label">{{ __('customer_payments.columns.customer') }}</span>
                    <select name="customer_id" class="pos-input" x-data="enhancedSelect()" onchange="this.form.submit()">
                        <option value="">{{ __('customer_payments.filter.customer_all') }}</option>
                        @foreach ($customers as $customer)
                            <option value="{{ $customer->id }}" @selected($filters['customerId'] === $customer->id)>{{ $customer->name }}</option>
                        @endforeach
                    </select>
                </label>
                <label class="field inv-filter-date">
                    <span class="field-label">{{ __('customer_payments.filter.from') }}</span>
                    <input type="text" name="from" value="{{ $filters['from'] }}"
                           class="pos-input js-datepicker" data-fp-submit-on-change="1"
                           placeholder="{{ __('table.filter_date_from_placeholder') }}">
                </label>
                <label class="field inv-filter-date">
                    <span class="field-label">{{ __('customer_payments.filter.to') }}</span>
                    <input type="text" name="to" value="{{ $filters['to'] }}"
                           class="pos-input js-datepicker" data-fp-submit-on-change="1"
                           placeholder="{{ __('table.filter_date_to_placeholder') }}">
                </label>
                <x-admin.filter-reset route="admin.customer-payments.index" />
            </form>

            @if ($payments->isEmpty())
                <div class="dt-empty">
                    <span class="dt-empty-icon"><x-icon name="cash" class="w-5 h-5" /></span>
                    <div class="dt-empty-title">{{ __('customer_payments.empty_state.title') }}</div>
                    <div class="dt-empty-sub">{{ __('customer_payments.empty_state.sub') }}</div>
                </div>
            @else
                <div class="dt-scroll">
                <table class="dt-table">
                    <thead>
                        <tr>
                            <th>{{ __('customer_payments.columns.date') }}</th>
                            <th>{{ __('customer_payments.columns.customer') }}</th>
                            <th>{{ __('customer_payments.columns.sale') }}</th>
                            <th>{{ __('customer_payments.columns.method') }}</th>
                            <th>{{ __('customer_payments.columns.reference') }}</th>
                            <th class="num">{{ __('customer_payments.columns.amount') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($payments as $p)
                            <tr class="prod-row" data-dt-row
                                data-dt-name="{{ $p->customer->name ?? $p->reference ?? '' }}"
                                data-dt-id="{{ $p->id }}"
                                @click="window.location.href = {{ \Illuminate\Support\Js::from(route('admin.customer-payments.show', $p)) }}">
                                <td>{{ format_date($p->paid_at) }}</td>
                                <td>{{ $p->customer?->name ?: '—' }}</td>
                                <td class="mono">{{ $p->sale?->number ?: __('customer_payments.unallocated_credit') }}</td>
                                <td>{{ $p->paymentMethod?->name ?: '—' }}</td>
                                <td class="mono">{{ $p->reference ?: '—' }}</td>
                                <td class="num tnum">{{ format_money($p->amount) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                </div>
                <x-admin.dt-pager />
            @endif
        </div>
    </div>
</x-admin-layout>
