{{-- Partial: Lista agrupada de planillas por quincena (acordeón) --}}
@if ($groups->isEmpty())
    <div class="text-center py-5">
        <i class="fa-solid fa-file-invoice-dollar fa-3x text-muted mb-3 d-block"></i>
        <p class="text-muted">{{ _trans('payroll.No payroll records found') }}</p>
    </div>
@else
    <div class="accordion" id="planillaAccordion">
        @foreach ($groups as $group)
            @php
                $safeKey = str_replace(['-', '.', ' '], '_', $group['key']);
            @endphp
            <div class="accordion-item mb-2 border rounded">
                {{-- Header de la quincena --}}
                <div class="accordion-header d-flex align-items-center justify-content-between pe-3"
                    id="heading-{{ $safeKey }}">
                    <button class="accordion-button collapsed flex-grow-1 py-2 px-3" type="button"
                        data-bs-toggle="collapse" data-bs-target="#collapse-{{ $safeKey }}"
                        aria-expanded="false" aria-controls="collapse-{{ $safeKey }}"
                        style="box-shadow: none; background: transparent;">
                        <div class="d-flex flex-wrap align-items-center gap-2">
                            <strong class="me-2">{{ $group['label'] }}</strong>
                            <span class="badge bg-primary rounded-pill">
                                {{ $group['total_employees'] }} {{ _trans('payroll.employees') }}
                            </span>
                            @if ($group['calculated'] > 0)
                                <span class="badge bg-info rounded-pill">
                                    {{ $group['calculated'] }} {{ _trans('payroll.calculated') }}
                                </span>
                            @endif
                            @if ($group['paid'] > 0)
                                <span class="badge bg-success rounded-pill">
                                    {{ $group['paid'] }} {{ _trans('payroll.paid') }}
                                </span>
                            @endif
                            @if ($group['total_net'] > 0)
                                <span class="badge bg-dark rounded-pill">
                                    {{ _trans('payroll.Net') }}: ${{ number_format($group['total_net'], 2) }}
                                </span>
                            @endif
                        </div>
                    </button>

                    {{-- Botones de acción del grupo (fuera del accordion-button) --}}
                    <div class="d-flex gap-1 flex-shrink-0">
                        {{-- Exportar Excel --}}
                        @if (hasPermission('salary_read'))
                            <a href="{{ route('hrm.payroll_salary.export_planilla', ['date' => $group['date']]) }}"
                                class="btn btn-sm btn-outline-success" data-bs-toggle="tooltip"
                                data-bs-placement="top"
                                data-bs-title="{{ _trans('payroll.Export Payroll') }}">
                                <i class="fa-solid fa-file-excel"></i>
                            </a>
                        @endif

                        {{-- Eliminar planilla completa --}}
                        @if (hasPermission('salary_delete') && $group['can_delete_all'])
                            <button type="button" class="btn btn-sm btn-outline-danger"
                                onclick="confirmDeletePlanilla('{{ $group['date'] }}')"
                                data-bs-toggle="tooltip" data-bs-placement="top"
                                data-bs-title="{{ _trans('payroll.Delete entire payroll') }}">
                                <i class="fa-solid fa-trash"></i>
                            </button>
                        @endif
                    </div>
                </div>

                {{-- Contenido plegable: tabla de empleados --}}
                <div id="collapse-{{ $safeKey }}" class="accordion-collapse collapse"
                    aria-labelledby="heading-{{ $safeKey }}" data-bs-parent="#planillaAccordion">
                    <div class="accordion-body p-0">
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover table-striped mb-0 align-middle">
                                <thead class="table-light">
                                    <tr>
                                        <th class="text-center" style="width: 60px;">ID</th>
                                        <th>{{ _trans('common.Name') }}</th>
                                        <th class="text-center">{{ _trans('payroll.Salary') }}</th>
                                        <th class="text-center">{{ _trans('payroll.Type') }}</th>
                                        <th class="text-center">{{ _trans('payroll.Calculation') }}</th>
                                        <th class="text-center">{{ _trans('common.Status') }}</th>
                                        <th class="text-center" style="width: 80px;">{{ _trans('common.Action') }}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse ($group['items'] as $item)
                                        <tr>
                                            {{-- ID / Código empleado --}}
                                            <td class="text-center">
                                                @if (hasPermission('salary_read'))
                                                    <a href="{{ route('hrm.payroll_salary.show', $item['id']) }}"
                                                        class="text-success text-decoration-none">
                                                        #{{ $item['employee_id'] ?: '0000' }}
                                                    </a>
                                                @else
                                                    <span class="text-muted">#{{ $item['employee_id'] ?: '0000' }}</span>
                                                @endif
                                            </td>

                                            {{-- Nombre --}}
                                            <td>{{ $item['employee_name'] }}</td>

                                            {{-- Salario Bruto --}}
                                            <td class="text-center">
                                                <span class="text-info fw-semibold">
                                                    ${{ number_format($item['salary'], 2) }}
                                                </span>
                                            </td>

                                            {{-- Tipo de recibo --}}
                                            <td class="text-center">
                                                <small>{{ $item['type'] }}</small>
                                            </td>

                                            {{-- Cálculo (detalles si está calculado) --}}
                                            <td class="text-center">
                                                @if (!$item['is_calculated'])
                                                    <span class="badge bg-danger">{{ _trans('common.No') }}</span>
                                                @else
                                                    <div class="small lh-lg">
                                                        <span class="text-success">
                                                            {{ _trans('payroll.Addition') }}: ${{ number_format($item['allowance_amount'], 2) }}
                                                        </span><br>
                                                        <span class="text-danger">
                                                            {{ _trans('payroll.Deduction') }}: ${{ number_format($item['deduction_amount'] + $item['absent_amount'] + $item['advance_amount'], 2) }}
                                                        </span><br>
                                                        <span class="text-success">
                                                            {{ _trans('payroll.Adjust Salary') }}: ${{ number_format($item['adjust'], 2) }}
                                                        </span><br>
                                                        <span class="text-info fw-bold">
                                                            {{ _trans('payroll.Net Salary') }}: ${{ number_format($item['net_salary'], 2) }}
                                                        </span>
                                                    </div>
                                                @endif
                                            </td>

                                            {{-- Estado --}}
                                            <td class="text-center">
                                                <span class="badge bg-{{ $item['status_class'] }}">
                                                    {{ $item['status_name'] }}
                                                </span>
                                            </td>

                                            {{-- Acciones --}}
                                            <td class="text-center">
                                                <div class="dropdown">
                                                    <button
                                                        class="btn btn-white btn-sm dropdown-toggle action-dot-btn"
                                                        data-bs-toggle="dropdown" aria-expanded="false">
                                                        <i class="fas fa-ellipsis-v"></i>
                                                    </button>
                                                    <ul class="dropdown-menu dropdown-menu-end">
                                                        @if (hasPermission('salary_read'))
                                                            <li>
                                                                <a class="dropdown-item"
                                                                    href="{{ route('hrm.payroll_salary.show', $item['id']) }}">
                                                                    <i class="fa-solid fa-eye me-1"></i>
                                                                    {{ _trans('common.View') }}
                                                                </a>
                                                            </li>
                                                        @endif

                                                        @if (hasPermission('salary_calculate') && !$item['is_calculated'])
                                                            <li>
                                                                <a class="dropdown-item" href="javascript:;"
                                                                    onclick="mainModalOpen('{{ route('hrm.payroll_salary.calculate_modal', $item['id']) }}')">
                                                                    <i class="fa-solid fa-calculator me-1"></i>
                                                                    {{ _trans('common.Calculate') }}
                                                                </a>
                                                            </li>
                                                        @endif

                                                        @if (hasPermission('salary_pay') && $item['status_id'] != 8 && $item['is_calculated'] == 1)
                                                            <li>
                                                                <a class="dropdown-item" href="javascript:;"
                                                                    onclick="mainModalOpen('{{ route('hrm.payroll_salary.pay', $item['id']) }}')">
                                                                    <i class="fa-solid fa-money-bill-wave me-1"></i>
                                                                    {{ _trans('common.Pay') }}
                                                                </a>
                                                            </li>
                                                        @endif

                                                        @if (hasPermission('salary_payslip'))
                                                            <li>
                                                                <a class="dropdown-item"
                                                                    href="{{ route('hrm.payroll_salary.invoice', $item['id']) }}">
                                                                    <i class="fa-solid fa-file-invoice me-1"></i>
                                                                    {{ _trans('common.Payslip') }}
                                                                </a>
                                                            </li>
                                                        @endif

                                                        @if (hasPermission('salary_delete') && $item['status_id'] == 9)
                                                            <li>
                                                                <hr class="dropdown-divider">
                                                            </li>
                                                            <li>
                                                                <a class="dropdown-item text-danger"
                                                                    href="javascript:;"
                                                                    onclick="__globalDelete({{ $item['id'] }}, 'hrm/payroll/salary/delete/')">
                                                                    <i class="fa-solid fa-trash me-1"></i>
                                                                    {{ _trans('common.Delete') }}
                                                                </a>
                                                            </li>
                                                        @endif
                                                    </ul>
                                                </div>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="7" class="text-center text-muted py-3">
                                                {{ _trans('common.No data found') }}
                                            </td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>
@endif
