{{-- 사업소득자 임금대장 스프레드시트 (HTMX로 갱신) --}}
@php
    use App\Models\HR\BusinessIncomePayment;
@endphp

{{-- earners 데이터를 JS에 전달 --}}
<script>window.__bipEarners = @json($earnersForJs);</script>

{{-- 공유 datalist --}}
<datalist id="bipEarnerDatalist">
    @foreach($earnersForJs as $earner)
        <option value="{{ $earner['business_name'] }}">{{ $earner['user_name'] }}</option>
    @endforeach
</datalist>

<x-table-swipe>
    <table class="min-w-full" id="bipTable">
        <thead class="bg-gray-50 border-b border-gray-200">
            <tr>
                <th class="px-3 py-3 text-center text-xs font-semibold text-gray-600" style="width: 50px;">구분</th>
                <th class="px-3 py-3 text-left text-xs font-semibold text-gray-600" style="min-width: 150px;">상호/성명</th>
                <th class="px-3 py-3 text-left text-xs font-semibold text-gray-600" style="min-width: 130px;">사업자등록번호</th>
                <th class="px-3 py-3 text-left text-xs font-semibold text-gray-600" style="min-width: 150px;">용역내용</th>
                <th class="px-3 py-3 text-right text-xs font-semibold text-gray-600" style="min-width: 130px;">지급총액</th>
                <th class="px-3 py-3 text-right text-xs font-semibold text-gray-600 bg-red-50" style="min-width: 110px;">소득세(3%)</th>
                <th class="px-3 py-3 text-right text-xs font-semibold text-gray-600 bg-red-50" style="min-width: 110px;">지방소득세(0.3%)</th>
                <th class="px-3 py-3 text-right text-xs font-semibold text-gray-600 bg-red-50" style="min-width: 110px;">공제합계</th>
                <th class="px-3 py-3 text-right text-xs font-semibold text-gray-600 bg-emerald-50" style="min-width: 130px;">실지급액</th>
                <th class="px-3 py-3 text-center text-xs font-semibold text-gray-600" style="min-width: 130px;">지급일자</th>
                <th class="px-3 py-3 text-left text-xs font-semibold text-gray-600" style="min-width: 120px;">비고</th>
                <th class="px-3 py-3 text-center text-xs font-semibold text-gray-600" style="width: 50px;">삭제</th>
            </tr>
        </thead>
        <tbody id="bipTbody" class="bg-white divide-y divide-gray-100">
            @foreach($payments as $idx => $payment)
            @php
                $isLocked = !$payment->isEditable();
                $statusColor = BusinessIncomePayment::STATUS_COLORS[$payment->status] ?? 'gray';
                $statusLabel = BusinessIncomePayment::STATUS_MAP[$payment->status] ?? '';
            @endphp
            <tr class="hover:bg-gray-50 transition-colors bip-row {{ $isLocked ? 'bg-gray-50' : '' }}"
                data-payment-id="{{ $payment->id }}"
                data-user-id="{{ $payment->user_id ?? '' }}">
                {{-- 구분 --}}
                <td class="px-3 py-2 text-center text-sm text-gray-500 bip-row-num">
                    {{ $idx + 1 }}
                    @if($isLocked)
                        <span class="block text-xs mt-0.5 px-1 rounded bg-{{ $statusColor }}-100 text-{{ $statusColor }}-700">{{ $statusLabel }}</span>
                    @endif
                </td>

                {{-- 상호/성명 --}}
                <td class="px-3 py-2">
                    @if($isLocked)
                        <span class="text-sm font-medium text-gray-900">{{ $payment->display_name ?: ($payment->user?->name ?? '-') }}</span>
                    @else
                        <input type="text"
                               name="display_name"
                               list="bipEarnerDatalist"
                               value="{{ $payment->display_name ?: ($payment->user?->name ?? '') }}"
                               placeholder="상호/성명"
                               oninput="onEarnerInput(this)"
                               onchange="onEarnerSelect(this)"
                               class="w-full px-2 py-1 border border-gray-200 rounded text-sm focus:ring-1 focus:ring-blue-500 focus:border-blue-500"
                               style="min-width: 120px;">
                    @endif
                </td>

                {{-- 사업자등록번호 --}}
                <td class="px-3 py-2">
                    @if($isLocked)
                        <span class="text-sm text-gray-600">{{ $payment->business_reg_number ?? '-' }}</span>
                    @else
                        <input type="text"
                               name="business_reg_number"
                               value="{{ $payment->business_reg_number ?? '' }}"
                               placeholder="사업자등록번호"
                               class="w-full px-2 py-1 border border-gray-200 rounded text-sm focus:ring-1 focus:ring-blue-500 focus:border-blue-500"
                               style="min-width: 110px;">
                    @endif
                </td>

                {{-- 용역내용 --}}
                <td class="px-3 py-2">
                    <input type="text"
                           name="service_content"
                           value="{{ $payment->service_content ?? '' }}"
                           placeholder="용역내용"
                           {{ $isLocked ? 'disabled' : '' }}
                           class="w-full px-2 py-1 border border-gray-200 rounded text-sm focus:ring-1 focus:ring-blue-500 focus:border-blue-500 {{ $isLocked ? 'bg-gray-100 text-gray-500' : '' }}"
                           style="min-width: 120px;">
                </td>

                {{-- 지급총액 --}}
                <td class="px-3 py-2">
                    <input type="text"
                           name="gross_amount"
                           inputmode="numeric"
                           value="{{ number_format($payment->gross_amount) }}"
                           {{ $isLocked ? 'disabled' : '' }}
                           oninput="formatMoneyInput(this); calcRow(this)"
                           onfocus="moneyFocus(this)"
                           onblur="moneyBlur(this)"
                           class="w-full px-2 py-1 border border-gray-200 rounded text-sm text-right money-input focus:ring-1 focus:ring-blue-500 focus:border-blue-500 {{ $isLocked ? 'bg-gray-100 text-gray-500' : '' }}"
                           style="min-width: 100px;">
                </td>

                {{-- 소득세 (자동계산) --}}
                <td class="px-3 py-2 text-right text-sm text-red-600 bg-red-50/30 bip-income-tax">
                    {{ number_format($payment->income_tax) }}
                </td>

                {{-- 지방소득세 (자동계산) --}}
                <td class="px-3 py-2 text-right text-sm text-red-600 bg-red-50/30 bip-local-tax">
                    {{ number_format($payment->local_income_tax) }}
                </td>

                {{-- 공제합계 (자동계산) --}}
                <td class="px-3 py-2 text-right text-sm font-medium text-red-700 bg-red-50/30 bip-total-ded">
                    {{ number_format($payment->total_deductions) }}
                </td>

                {{-- 실지급액 (자동계산) --}}
                <td class="px-3 py-2 text-right text-sm font-medium text-emerald-700 bg-emerald-50/30 bip-net">
                    {{ number_format($payment->net_amount) }}
                </td>

                {{-- 지급일자 --}}
                <td class="px-3 py-2">
                    <input type="date"
                           name="payment_date"
                           value="{{ $payment->payment_date?->format('Y-m-d') ?? '' }}"
                           {{ $isLocked ? 'disabled' : '' }}
                           class="w-full px-2 py-1 border border-gray-200 rounded text-sm focus:ring-1 focus:ring-blue-500 focus:border-blue-500 {{ $isLocked ? 'bg-gray-100 text-gray-500' : '' }}"
                           style="min-width: 110px;">
                </td>

                {{-- 비고 --}}
                <td class="px-3 py-2">
                    <input type="text"
                           name="note"
                           value="{{ $payment->note ?? '' }}"
                           placeholder="비고"
                           {{ $isLocked ? 'disabled' : '' }}
                           class="w-full px-2 py-1 border border-gray-200 rounded text-sm focus:ring-1 focus:ring-blue-500 focus:border-blue-500 {{ $isLocked ? 'bg-gray-100 text-gray-500' : '' }}"
                           style="min-width: 80px;">
                </td>

                {{-- 삭제 --}}
                <td class="px-3 py-2 text-center">
                    @if(!$isLocked)
                        <button type="button" onclick="removeBipRow(this)"
                                class="text-red-400 hover:text-red-600 transition-colors" title="행 삭제">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                            </svg>
                        </button>
                    @else
                        <span class="text-gray-300">&mdash;</span>
                    @endif
                </td>
            </tr>
            @endforeach
        </tbody>
        <tfoot class="bg-gray-100 border-t-2 border-gray-300">
            {{-- 행 추가 버튼 --}}
            <tr>
                <td colspan="12" class="px-3 py-2">
                    <button type="button" onclick="addBipRow()"
                            class="inline-flex items-center gap-1 px-3 py-1.5 text-sm text-blue-600 hover:text-blue-800 hover:bg-blue-50 rounded transition-colors">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                        </svg>
                        행 추가
                    </button>
                </td>
            </tr>
            {{-- 합계 --}}
            <tr class="font-semibold">
                <td class="px-3 py-3 text-center text-sm text-gray-700" colspan="4">합계</td>
                <td class="px-3 py-3 text-right text-sm text-gray-800" id="bipSumGross">
                    {{ number_format($payments->sum('gross_amount')) }}
                </td>
                <td class="px-3 py-3 text-right text-sm text-red-600 bg-red-50/30" id="bipSumIncomeTax">
                    {{ number_format($payments->sum('income_tax')) }}
                </td>
                <td class="px-3 py-3 text-right text-sm text-red-600 bg-red-50/30" id="bipSumLocalTax">
                    {{ number_format($payments->sum('local_income_tax')) }}
                </td>
                <td class="px-3 py-3 text-right text-sm text-red-700 bg-red-50/30" id="bipSumDed">
                    {{ number_format($payments->sum('total_deductions')) }}
                </td>
                <td class="px-3 py-3 text-right text-sm text-emerald-700 bg-emerald-50/30" id="bipSumNet">
                    {{ number_format($payments->sum('net_amount')) }}
                </td>
                <td class="px-3 py-3" colspan="3"></td>
            </tr>
        </tfoot>
    </table>
</x-table-swipe>
