@props(['employee' => null])

@php
    $docExpiry = fn (string $type) => optional(
        $employee?->documents?->firstWhere('type', \App\Modules\HR\Employees\Enums\EmployeeDocumentType::tryFrom($type))
    )->expires_at?->format('Y-m-d');
@endphp

<p class="text-muted small mb-3">{{ __('employees.documents.hint') }}</p>

@if ($employee !== null && $employee->documents->isNotEmpty())
    <p class="fw-semibold mb-2">{{ __('employees.fields.existing_documents') }}</p>
    <ul class="list-unstyled mb-4">
        @foreach ($employee->documents as $doc)
            <li class="mb-1">
                @can('employees.documents.view', $employee)
                    <a href="{{ route('hr.employees.documents.download', [$employee, $doc]) }}">
                        {{ __('employees.documents.'.$doc->type->value) }}
                        — {{ __('employees.documents.download') }}
                    </a>
                @else
                    {{ __('employees.documents.'.$doc->type->value) }}
                @endcan
                @if ($doc->expires_at)
                    <span class="text-muted small">({{ $doc->expires_at->format('Y-m-d') }})</span>
                @endif
            </li>
        @endforeach
    </ul>
@endif

@can('employees.documents.view')
    <div class="row g-3">
        <div class="col-md-6">
            <x-ui.form.field name="doc_photo" :label="__('employees.documents.photo')" id="doc_photo">
                <input id="doc_photo" type="file" name="doc_photo" accept=".jpg,.jpeg,.png,image/jpeg,image/png"
                    class="form-control @error('doc_photo') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_cv" :label="__('employees.documents.cv')" id="doc_cv">
                <input id="doc_cv" type="file" name="doc_cv" accept=".pdf,.doc,.docx,application/pdf"
                    class="form-control @error('doc_cv') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_national_id_front" :label="__('employees.documents.national_id_front')" id="doc_national_id_front">
                <input id="doc_national_id_front" type="file" name="doc_national_id_front"
                    accept=".pdf,.jpg,.jpeg,.png"
                    class="form-control @error('doc_national_id_front') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_national_id_back" :label="__('employees.documents.national_id_back')" id="doc_national_id_back">
                <input id="doc_national_id_back" type="file" name="doc_national_id_back"
                    accept=".pdf,.jpg,.jpeg,.png"
                    class="form-control @error('doc_national_id_back') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.input
                name="doc_national_id_expires_at"
                type="date"
                :label="__('employees.documents.national_id_expires_at')"
                :value="old('doc_national_id_expires_at', $docExpiry('national_id_front'))"
            />
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_passport" :label="__('employees.documents.passport')" id="doc_passport">
                <input id="doc_passport" type="file" name="doc_passport"
                    accept=".pdf,.jpg,.jpeg,.png"
                    class="form-control @error('doc_passport') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.input
                name="doc_passport_expires_at"
                type="date"
                :label="__('employees.documents.passport_expires_at')"
                :value="old('doc_passport_expires_at', $docExpiry('passport'))"
            />
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_contract" :label="__('employees.documents.contract')" id="doc_contract">
                <input id="doc_contract" type="file" name="doc_contract"
                    accept=".pdf,.jpg,.jpeg,.png"
                    class="form-control @error('doc_contract') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.input
                name="doc_contract_expires_at"
                type="date"
                :label="__('employees.documents.contract_expires_at')"
                :value="old('doc_contract_expires_at', $docExpiry('contract'))"
            />
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_residency" :label="__('employees.documents.residency')" id="doc_residency">
                <input id="doc_residency" type="file" name="doc_residency"
                    accept=".pdf,.jpg,.jpeg,.png"
                    class="form-control @error('doc_residency') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.input
                name="doc_residency_expires_at"
                type="date"
                :label="__('employees.documents.residency_expires_at')"
                :value="old('doc_residency_expires_at', $docExpiry('residency'))"
            />
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_visa" :label="__('employees.documents.visa')" id="doc_visa">
                <input id="doc_visa" type="file" name="doc_visa"
                    accept=".pdf,.jpg,.jpeg,.png"
                    class="form-control @error('doc_visa') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.input
                name="doc_visa_expires_at"
                type="date"
                :label="__('employees.documents.visa_expires_at')"
                :value="old('doc_visa_expires_at', $docExpiry('visa'))"
            />
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_work_permit" :label="__('employees.documents.work_permit')" id="doc_work_permit">
                <input id="doc_work_permit" type="file" name="doc_work_permit"
                    accept=".pdf,.jpg,.jpeg,.png"
                    class="form-control @error('doc_work_permit') is-invalid @enderror">
            </x-ui.form.field>
        </div>
        <div class="col-md-6">
            <x-ui.form.input
                name="doc_work_permit_expires_at"
                type="date"
                :label="__('employees.documents.work_permit_expires_at')"
                :value="old('doc_work_permit_expires_at', $docExpiry('work_permit'))"
            />
        </div>
        <div class="col-md-6">
            <x-ui.form.field name="doc_certificate" :label="__('employees.documents.certificate')" id="doc_certificate">
                <input id="doc_certificate" type="file" name="doc_certificate"
                    accept=".pdf,.doc,.docx"
                    class="form-control @error('doc_certificate') is-invalid @enderror">
            </x-ui.form.field>
        </div>
    </div>
@endcan
