{{-- 입금 등록 폼 --}}
<form id="payment-form" onsubmit="event.preventDefault(); submitPayment();">
    @csrf

    {{-- 테넌트 선택 --}}
    <div class="mb-4">
        <label class="block text-sm font-medium text-gray-700 mb-1">테넌트 선택 <span class="text-red-500">*</span></label>
        @if ($management)
            <input type="hidden" name="management_id" value="{{ $management->id }}">
            <div class="px-4 py-3 bg-gray-50 rounded-lg">
                <div class="font-medium text-gray-900">{{ $management->tenant->name ?? $management->tenant->company_name }}</div>
                <div class="text-sm text-gray-500">영업파트너: {{ $management->salesPartner?->user?->name ?? '-' }}</div>
            </div>
        @else
            <select name="management_id"
                    onchange="onTenantSelect(this.value)"
                    required
                    class="w-full rounded-lg border-gray-300 focus:border-emerald-500 focus:ring-emerald-500">
                <option value="">-- 테넌트 선택 --</option>
                @foreach ($pendingTenants as $tenant)
                    <option value="{{ $tenant->id }}">
                        {{ $tenant->tenant->name ?? $tenant->tenant->company_name }}
                        @if ($tenant->deposit_status === 'pending')
                            (계약금 대기)
                        @elseif ($tenant->balance_status === 'pending')
                            (잔금 대기)
                        @endif
                    </option>
                @endforeach
            </select>
        @endif
    </div>

    @if ($management)
        {{-- 계약 상품 정보 --}}
        @if ($management->contractProducts->count() > 0)
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 mb-1">계약 상품</label>
                <div class="border rounded-lg overflow-hidden">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">상품명</th>
                                <th class="px-4 py-2 text-right text-xs font-medium text-gray-500">개발비</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @foreach ($management->contractProducts as $product)
                                <tr>
                                    <td class="px-4 py-2 text-sm text-gray-900">{{ $product->product?->name ?? '-' }}</td>
                                    <td class="px-4 py-2 text-sm text-right text-gray-900">{{ number_format($product->registration_fee ?? 0) }}원</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot class="bg-gray-50">
                            <tr>
                                <td class="px-4 py-2 text-sm font-medium text-gray-900">총 개발비</td>
                                <td class="px-4 py-2 text-sm text-right font-bold text-emerald-600">
                                    {{ number_format($management->contractProducts->sum('registration_fee')) }}원
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        @endif

        {{-- 현재 입금 상태 --}}
        <div class="mb-4 p-4 bg-gray-50 rounded-lg">
            <div class="grid grid-cols-2 gap-4">
                <div>
                    <span class="text-sm text-gray-500">계약금</span>
                    <div class="font-medium {{ $management->deposit_status === 'paid' ? 'text-green-600' : 'text-yellow-600' }}">
                        {{ $management->deposit_status === 'paid' ? '입금완료' : '대기' }}
                        @if ($management->deposit_amount)
                            ({{ number_format($management->deposit_amount) }}원)
                        @endif
                    </div>
                </div>
                <div>
                    <span class="text-sm text-gray-500">잔금</span>
                    <div class="font-medium {{ $management->balance_status === 'paid' ? 'text-green-600' : 'text-yellow-600' }}">
                        {{ $management->balance_status === 'paid' ? '입금완료' : '대기' }}
                        @if ($management->balance_amount)
                            ({{ number_format($management->balance_amount) }}원)
                        @endif
                    </div>
                </div>
            </div>
        </div>
    @endif

    {{-- 입금 구분 --}}
    <div class="mb-4">
        <label class="block text-sm font-medium text-gray-700 mb-1">입금 구분 <span class="text-red-500">*</span></label>
        <div class="flex gap-4">
            <label class="inline-flex items-center">
                <input type="radio" name="payment_type" value="deposit" required
                       {{ ($management && $management->deposit_status === 'paid') ? 'disabled' : '' }}
                       class="text-emerald-600 focus:ring-emerald-500">
                <span class="ml-2 text-sm text-gray-700">계약금</span>
            </label>
            <label class="inline-flex items-center">
                <input type="radio" name="payment_type" value="balance" required
                       {{ ($management && $management->balance_status === 'paid') ? 'disabled' : '' }}
                       class="text-emerald-600 focus:ring-emerald-500">
                <span class="ml-2 text-sm text-gray-700">잔금</span>
            </label>
        </div>
    </div>

    {{-- 입금액 --}}
    <div class="mb-4">
        <label class="block text-sm font-medium text-gray-700 mb-1">입금액 <span class="text-red-500">*</span></label>
        <div class="relative">
            <input type="number"
                   name="payment_amount"
                   required
                   min="0"
                   step="1"
                   @if ($management)
                       value="{{ $management->contractProducts->sum('registration_fee') / 2 }}"
                   @endif
                   class="w-full rounded-lg border-gray-300 focus:border-emerald-500 focus:ring-emerald-500 pr-12">
            <span class="absolute inset-y-0 right-0 flex items-center pr-4 text-gray-500">원</span>
        </div>
        <p class="text-xs text-gray-500 mt-1">총 개발비의 50%를 입금받습니다.</p>
    </div>

    {{-- 입금일 --}}
    <div class="mb-4">
        <label class="block text-sm font-medium text-gray-700 mb-1">입금일 <span class="text-red-500">*</span></label>
        <input type="date"
               name="payment_date"
               required
               value="{{ now()->format('Y-m-d') }}"
               class="w-full rounded-lg border-gray-300 focus:border-emerald-500 focus:ring-emerald-500">
    </div>

    {{-- 수당 미리보기 --}}
    @if ($management && $management->salesPartner)
        @php
            $totalFee = $management->contractProducts->sum('registration_fee') ?: 0;
            $baseAmount = $totalFee / 2;
            $partnerRate = $management->salesPartner->commission_rate ?? 20;
            $managerRate = $management->salesPartner->manager_commission_rate ?? 5;
            $partnerCommission = $baseAmount * ($partnerRate / 100);
            $managerCommission = $management->manager_user_id ? $baseAmount * ($managerRate / 100) : 0;
        @endphp
        <div class="mb-4 p-4 bg-emerald-50 rounded-lg">
            <h4 class="text-sm font-medium text-emerald-800 mb-2">수당 미리보기</h4>
            <div class="space-y-1 text-sm">
                <div class="flex justify-between">
                    <span class="text-gray-600">기준액 (개발비의 50%)</span>
                    <span class="font-medium">{{ number_format($baseAmount) }}원</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600">영업파트너 수당 ({{ $partnerRate }}%)</span>
                    <span class="font-medium text-emerald-600">{{ number_format($partnerCommission) }}원</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600">매니저 수당 ({{ $managerRate }}%)</span>
                    <span class="font-medium text-blue-600">{{ number_format($managerCommission) }}원</span>
                </div>
                <div class="border-t border-emerald-200 pt-1 mt-1 flex justify-between">
                    <span class="font-medium text-gray-700">총 수당</span>
                    <span class="font-bold text-emerald-700">{{ number_format($partnerCommission + $managerCommission) }}원</span>
                </div>
            </div>
        </div>
    @endif

    {{-- 버튼 --}}
    <div class="flex justify-end gap-2 mt-6">
        <button type="button"
                onclick="closePaymentModal()"
                class="px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 rounded-lg transition-colors">
            취소
        </button>
        <button type="submit"
                class="px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg transition-colors">
            입금 등록
        </button>
    </div>
</form>
