<div class="employee-doc-section">

    {{-- ===================== HEADER ===================== --}}
    <div class="employee-doc-header mb-4 d-flex justify-content-between align-items-center">
         <div class="row align-items-center">
            <div class="col">
              <h5 class="employee-doc-title mb-1">Employee Documents</h5>
            <small class="text-muted">
                Upload and manage official employee documents
            </small>
            </div>
            <div class="col-auto">
              <button type="button"
                class="btn btn-sm btn-outline-primary"
                id="addDocumentRow">
            <i class="bi bi-plus"></i> Add Document
        </button>
            </div>
        </div>             
    </div>

    {{-- ===================== BODY ===================== --}}
    <div class="employee-doc-body">

        <div class="table-responsive">
            <table class="table table-bordered align-middle document-table"
                   id="documentTable">

                <thead class="table-light">
                    <tr>
                        <th style="min-width: 200px;">
                            Document Type <span class="text-danger">*</span>
                        </th>
                        <th style="min-width: 180px;">Document Number</th>
                        <th style="min-width: 150px;">Issue Date</th>
                        <th style="min-width: 150px;">Expiry Date</th>
                        <th style="min-width: 260px;">
                            File <span class="text-danger">*</span>
                        </th>
                        <th class="text-center" style="width: 50px;"></th>
                    </tr>
                </thead>

                <tbody>

                    {{-- ===================== EDIT MODE ===================== --}}
                    @if(isset($employee) && $employee->documents->count())
                        @foreach($employee->documents as $i => $doc)
                            <tr>

                                <td>
                                    <select name="documents[{{ $i }}][document_type_id]"
                                            class="form-control"
                                            required>
                                        <option value="">-- Select --</option>
                                        @foreach($documentTypes as $dt)
                                            <option value="{{ $dt->id }}"
                                                @selected($doc->document_type_id == $dt->id)>
                                                {{ $dt->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                </td>

                                <td>
                                    <input type="text"
                                           name="documents[{{ $i }}][document_number]"
                                           class="form-control"
                                           value="{{ $doc->document_number }}">
                                </td>

                                <td>
                                    <input type="date"
                                           name="documents[{{ $i }}][issue_date]"
                                           class="form-control"
                                           value="{{ $doc->issue_date }}">
                                </td>

                                <td>
                                    <input type="date"
                                           name="documents[{{ $i }}][expiry_date]"
                                           class="form-control"
                                           value="{{ $doc->expiry_date }}">
                                </td>

                                <td>
                                    <input type="file"
                                           name="documents[{{ $i }}][file]"
                                           class="form-control">

                                    @if($doc->file_path)
                                        <div class="form-text text-muted mt-1">
                                            Existing file will be retained
                                        </div>
                                    @endif
                                </td>

                                <td class="text-center">
                                    <button type="button"
                                            class="btn btn-sm btn-outline-danger document-remove-btn remove-row"
                                            title="Remove">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </td>

                            </tr>
                        @endforeach
                    @endif

                </tbody>

            </table>
        </div>

    </div>

</div>
