@php
    use App\Modules\Payroll\Support\PayrollAdjustmentType;
    use App\Modules\Payroll\Support\PayrollPeriodStatus;

    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('payroll.title'), 'url' => route('payroll.periods.index')],
        ['label' => $period->name],
    ];
    $typeOptions = collect(PayrollAdjustmentType::all())
        ->mapWithKeys(fn ($t) => [$t => __('payroll.adjustment_types.'.$t)])
        ->all();
    $empOptions = $employees->mapWithKeys(fn ($e) => [$e->id => $e->full_name])->all();
@endphp

<x-ui.layout.page
    :title="$period->name"
    :subtitle="__('payroll.show_title')"
    page-icon="fe fe-dollar-sign"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        <x-ui.buttons.secondary :href="route('payroll.periods.index')" icon="fe fe-arrow-left">
            {{ __('payroll.actions.back') }}
        </x-ui.buttons.secondary>
        @can('process', $period)
            <form method="POST" action="{{ route('payroll.periods.process', $period->id) }}" class="d-inline">
                @csrf
                <x-ui.buttons.primary type="submit" icon="fe fe-play">
                    {{ __('payroll.actions.process') }}
                </x-ui.buttons.primary>
            </form>
        @endcan
        @can('finalize', $period)
            <form method="POST" action="{{ route('payroll.periods.finalize', $period->id) }}" class="d-inline" onsubmit="return confirm(@json(__('payroll.confirm_finalize')));">
                @csrf
                <x-ui.buttons.primary type="submit" icon="fe fe-lock">
                    {{ __('payroll.actions.finalize') }}
                </x-ui.buttons.primary>
            </form>
        @endcan
    </x-slot:actions>

    <div class="row g-3 mb-4">
        <div class="col-md-3">
            <div class="small text-muted">{{ __('payroll.fields.status') }}</div>
            <div><span class="badge bg-secondary">{{ __('payroll.status.'.$period->status) }}</span></div>
        </div>
        <div class="col-md-3">
            <div class="small text-muted">{{ __('payroll.fields.start_date') }}</div>
            <div>{{ $period->start_date?->format('Y-m-d') }}</div>
        </div>
        <div class="col-md-3">
            <div class="small text-muted">{{ __('payroll.fields.end_date') }}</div>
            <div>{{ $period->end_date?->format('Y-m-d') }}</div>
        </div>
        <div class="col-md-3">
            <div class="small text-muted">{{ __('payroll.fields.finalized_at') }}</div>
            <div>{{ $period->finalized_at?->format('Y-m-d H:i') ?? '—' }}</div>
        </div>
    </div>

    @if ($period->status === PayrollPeriodStatus::FINALIZED)
        <div class="alert alert-warning border-0 shadow-sm">{{ __('payroll.sections.locked') }}</div>
    @else
        <div class="alert alert-secondary border-0 small">{{ __('payroll.help_process') }}</div>
    @endif

    <h2 class="h5 mb-3">{{ __('payroll.sections.snapshots') }}</h2>
    <x-ui.table.table :columns="[
        ['label' => __('payroll.fields.employee')],
        ['label' => __('payroll.columns.attendance_days')],
        ['label' => __('payroll.columns.absent_days')],
        ['label' => __('payroll.columns.late_days')],
        ['label' => __('payroll.columns.leave_days')],
        ['label' => __('payroll.columns.overtime_min')],
        ['label' => __('payroll.columns.adjustments')],
        ['label' => __('payroll.columns.gross')],
        ['label' => __('common.actions')],
    ]">
        @forelse ($snapshots as $snap)
            <x-ui.table.row>
                <td><strong>{{ $snap->employee?->full_name ?? '—' }}</strong></td>
                <td>{{ $snap->attendance_days }}</td>
                <td>{{ $snap->absent_days }}</td>
                <td>{{ $snap->late_days }}</td>
                <td>{{ $snap->leave_days }}</td>
                <td>{{ $snap->overtime_minutes }}</td>
                <td>{{ $snap->adjustments_total }}</td>
                <td>{{ $snap->gross_amount ?? '—' }}</td>
                <td>
                    @can('payroll.payslips.view')
                        @if ($period->isFinalized())
                            <x-ui.buttons.secondary :href="route('payroll.payslips.show', $snap->id)" icon="fe fe-file-text">
                                {{ __('payslips.title') }}
                            </x-ui.buttons.secondary>
                        @else
                            <span class="text-muted small">—</span>
                        @endif
                    @endcan
                </td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row>
                <td colspan="9" class="text-center text-muted py-3">—</td>
            </x-ui.table.row>
        @endforelse
    </x-ui.table.table>
    @if ($snapshots->hasPages())
        <div class="mt-2 mb-4">{{ $snapshots->links() }}</div>
    @endif

    <h2 class="h5 mb-3 mt-4">{{ __('payroll.sections.adjustments') }}</h2>
    @can('manageAdjustments', $period)
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-body">
                <form method="POST" action="{{ route('payroll.periods.adjustments.store', $period->id) }}" class="row g-3">
                    @csrf
                    <div class="col-md-4">
                        <x-ui.form.search-select
                            name="employee_id"
                            lookup="employees"
                            :label="__('payroll.fields.employee')"
                            :options="$empOptions"
                            :selected="old('employee_id')"
                            :placeholder="__('common.select')"
                            required
                        />
                    </div>
                    <div class="col-md-4">
                        <x-ui.form.select
                            name="type"
                            :label="__('payroll.fields.type')"
                            :options="$typeOptions"
                            :selected="old('type')"
                            :placeholder="__('common.select')"
                            required
                        />
                    </div>
                    <div class="col-md-4">
                        <x-ui.form.input name="amount" type="number" step="0.01" min="0.01" :label="__('payroll.fields.amount')" :value="old('amount')" required />
                    </div>
                    <div class="col-12">
                        <x-ui.form.field name="reason" :label="__('payroll.fields.reason')" required>
                            <textarea name="reason" class="form-control @error('reason') is-invalid @enderror" rows="2" required>{{ old('reason') }}</textarea>
                        </x-ui.form.field>
                    </div>
                    <div class="col-12">
                        <x-ui.buttons.primary type="submit" icon="fe fe-plus">
                            {{ __('payroll.actions.add_adjustment') }}
                        </x-ui.buttons.primary>
                    </div>
                </form>
            </div>
        </div>
    @endcan

    <x-ui.table.table :columns="[
        ['label' => __('payroll.fields.employee')],
        ['label' => __('payroll.fields.type')],
        ['label' => __('payroll.fields.amount')],
        ['label' => __('payroll.fields.reason')],
    ]">
        @forelse ($adjustmentPaginator as $adj)
            <x-ui.table.row>
                <td>{{ $adj->employee?->full_name ?? '—' }}</td>
                <td>{{ __('payroll.adjustment_types.'.$adj->type) }}</td>
                <td>{{ $adj->amount }}</td>
                <td class="small">{{ $adj->reason }}</td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row>
                <td colspan="4" class="text-center text-muted py-3">—</td>
            </x-ui.table.row>
        @endforelse
    </x-ui.table.table>
    @if ($adjustmentPaginator->hasPages())
        <div class="mt-2">{{ $adjustmentPaginator->links() }}</div>
    @endif
</x-ui.layout.page>
