{{-- 통계 카드 --}}
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-4">
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">전체 고객</div>
        <div class="text-2xl font-bold text-gray-800">{{ number_format($stats['total']) }}건</div>
    </div>
    <div class="bg-blue-50 rounded-lg shadow-sm p-4">
        <div class="text-sm text-blue-600">영업중</div>
        <div class="text-2xl font-bold text-blue-800">{{ number_format($stats['active']) }}건</div>
    </div>
    <div class="bg-teal-50 rounded-lg shadow-sm p-4">
        <div class="text-sm text-teal-600">완료</div>
        <div class="text-2xl font-bold text-teal-800">{{ number_format($stats['completed']) }}건</div>
    </div>
    <div class="bg-emerald-50 rounded-lg shadow-sm p-4">
        <div class="text-sm text-emerald-600">인계완료</div>
        <div class="text-2xl font-bold text-emerald-800">{{ number_format($stats['handover']) }}건</div>
    </div>
</div>

{{-- 필터 영역 --}}
<div class="mb-4">
    <form method="GET" class="flex flex-wrap gap-2 sm:gap-3 items-center bg-white p-3 rounded-lg shadow-sm">
        {{-- 상태 필터 버튼 --}}
        <div class="flex gap-1 flex-shrink-0">
            <a href="{{ route('sales.admin-prospects.index', array_merge(request()->except('status', 'page'), [])) }}"
               class="px-3 py-2 rounded-lg text-sm font-medium transition {{ !request('status') ? 'bg-gray-800 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' }}">
                전체
            </a>
            <a href="{{ route('sales.admin-prospects.index', array_merge(request()->except('page'), ['status' => 'active'])) }}"
               class="px-3 py-2 rounded-lg text-sm font-medium transition {{ request('status') === 'active' ? 'bg-blue-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' }}">
                영업중
            </a>
            <a href="{{ route('sales.admin-prospects.index', array_merge(request()->except('page'), ['status' => 'completed'])) }}"
               class="px-3 py-2 rounded-lg text-sm font-medium transition {{ request('status') === 'completed' ? 'bg-teal-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' }}">
                완료
            </a>
            <a href="{{ route('sales.admin-prospects.index', array_merge(request()->except('page'), ['status' => 'handover'])) }}"
               class="px-3 py-2 rounded-lg text-sm font-medium transition {{ request('status') === 'handover' ? 'bg-emerald-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' }}">
                인계완료
            </a>
        </div>

        {{-- 구분선 --}}
        <div class="hidden sm:block w-px h-8 bg-gray-300"></div>

        {{-- 현재 status 유지 --}}
        @if(request('status'))
        <input type="hidden" name="status" value="{{ request('status') }}">
        @endif

        {{-- 영업파트너 선택 --}}
        <div class="w-40 flex-shrink-0">
            <select name="registered_by" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm">
                <option value="">전체 파트너</option>
                @foreach($salesPartners as $partner)
                    <option value="{{ $partner->id }}" {{ request('registered_by') == $partner->id ? 'selected' : '' }}>
                        {{ $partner->name }}
                    </option>
                @endforeach
            </select>
        </div>

        {{-- 검색 입력 --}}
        <div class="flex-1 min-w-0">
            <input type="text"
                   name="search"
                   value="{{ request('search') }}"
                   placeholder="업체명, 사업자번호, 대표자, 연락처 검색..."
                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm">
        </div>

        {{-- 검색 버튼 --}}
        <button type="submit" class="bg-gray-700 hover:bg-gray-800 text-white px-4 py-2 rounded-lg transition text-sm flex-shrink-0">
            검색
        </button>
    </form>
</div>

{{-- 테이블 --}}
<div class="bg-white rounded-lg shadow-sm overflow-hidden flex-1 flex flex-col min-h-0">
    <div class="overflow-x-auto">
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">업체명</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">담당 파트너</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">구분</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">담당 매니저</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">영업 진행률</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">매니저 진행률</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">계약일</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">1차 납입</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">2차 납입</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">개발 상태</th>
                    <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">인계일</th>
                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">상태</th>
                    <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">관리</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                @forelse($prospects as $prospect)
                <tr class="hover:bg-gray-50">
                    <td class="px-4 py-3 whitespace-nowrap">
                        <div class="font-medium text-gray-900">{{ $prospect->company_name }}</div>
                        <div class="text-xs text-gray-500">{{ $prospect->business_number }}</div>
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap">
                        @if($prospect->registeredBy)
                            <span class="text-sm text-gray-900">{{ $prospect->registeredBy->name }}</span>
                        @else
                            <span class="text-sm text-gray-400">-</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap text-center">
                        @if(($prospect->partner_type ?? 'individual') === 'corporate')
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-indigo-100 text-indigo-700">단체</span>
                        @else
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-gray-100 text-gray-600">개인</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap">
                        @if($prospect->manager_user)
                            <span class="text-sm text-gray-900">{{ $prospect->manager_user->name }}</span>
                        @else
                            <span class="text-sm text-gray-400">미지정</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap text-center">
                        <div class="flex items-center justify-center gap-2">
                            <div class="w-16 bg-gray-200 rounded-full h-2">
                                <div class="bg-blue-500 h-2 rounded-full" style="width: {{ $prospect->sales_progress }}%"></div>
                            </div>
                            <span class="text-xs text-gray-600">{{ $prospect->sales_progress }}%</span>
                        </div>
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap text-center">
                        <div class="flex items-center justify-center gap-2">
                            <div class="w-16 bg-gray-200 rounded-full h-2">
                                <div class="bg-green-500 h-2 rounded-full" style="width: {{ $prospect->manager_progress }}%"></div>
                            </div>
                            <span class="text-xs text-gray-600">{{ $prospect->manager_progress }}%</span>
                        </div>
                    </td>
                    @php
                        $commissionDisabled = $prospect->sales_progress < 100 || $prospect->manager_progress < 100;
                        $isCorporate = ($prospect->partner_type ?? 'individual') === 'corporate';
                        $disabledClass = $commissionDisabled ? 'opacity-40 cursor-not-allowed bg-gray-100' : '';
                    @endphp
                    {{-- 계약일 --}}
                    <td class="px-1 py-2 whitespace-nowrap text-center">
                        <input type="date"
                            class="w-28 h-7 text-xs px-1 border-2 border-gray-300 rounded cursor-pointer hover:border-blue-400 focus:outline-none focus:border-blue-500 {{ $prospect->contracted_at ? 'text-blue-600 font-medium bg-blue-50 border-blue-400' : 'text-gray-500 bg-gray-50' }}"
                            value="{{ $prospect->contracted_at ? \Carbon\Carbon::parse($prospect->contracted_at)->format('Y-m-d') : '' }}"
                            onchange="saveContractedDate({{ $prospect->id }}, this.value, this)">
                    </td>
                    {{-- 1차 납입완료 --}}
                    <td class="px-1 py-2 whitespace-nowrap text-center">
                        <input type="date"
                            class="w-28 h-7 text-xs px-1 border-2 border-gray-300 rounded {{ $commissionDisabled ? $disabledClass : 'cursor-pointer hover:border-emerald-400' }} focus:outline-none focus:border-emerald-500 {{ !$commissionDisabled && $prospect->commission?->first_payment_at ? 'text-emerald-600 font-medium bg-emerald-50 border-emerald-400' : (!$commissionDisabled ? 'text-gray-500 bg-gray-50' : '') }}"
                            value="{{ $prospect->commission?->first_payment_at?->format('Y-m-d') }}"
                            data-prospect-id="{{ $prospect->id }}"
                            data-field="first_payment_at"
                            {{ $commissionDisabled ? 'disabled' : '' }}
                            onchange="saveCommissionDate({{ $prospect->id }}, 'first_payment_at', this.value)">
                    </td>
                    {{-- 2차 납입완료 --}}
                    <td class="px-1 py-2 whitespace-nowrap text-center">
                        <input type="date"
                            class="w-28 h-7 text-xs px-1 border-2 border-gray-300 rounded {{ $commissionDisabled ? $disabledClass : 'cursor-pointer hover:border-emerald-400' }} focus:outline-none focus:border-emerald-500 {{ !$commissionDisabled && $prospect->commission?->second_payment_at ? 'text-emerald-600 font-medium bg-emerald-50 border-emerald-400' : (!$commissionDisabled ? 'text-gray-500 bg-gray-50' : '') }}"
                            value="{{ $prospect->commission?->second_payment_at?->format('Y-m-d') }}"
                            data-prospect-id="{{ $prospect->id }}"
                            data-field="second_payment_at"
                            {{ $commissionDisabled ? 'disabled' : '' }}
                            onchange="saveCommissionDate({{ $prospect->id }}, 'second_payment_at', this.value)">
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap text-center">
                        <select
                            data-hq-status="{{ $prospect->id }}"
                            onchange="updateHqStatus({{ $prospect->id }}, this.value)"
                            class="text-xs font-medium rounded-lg px-2 py-1 border cursor-pointer
                                @if($prospect->hq_status === 'handover') bg-emerald-100 text-emerald-700 border-emerald-300
                                @elseif($prospect->hq_status === 'pending') bg-gray-100 text-gray-600 border-gray-300
                                @else bg-purple-100 text-purple-700 border-purple-300 @endif">
                            @foreach(\App\Models\Sales\SalesTenantManagement::$hqStatusLabels as $status => $label)
                                <option value="{{ $status }}" {{ $prospect->hq_status === $status ? 'selected' : '' }}>
                                    {{ $label }}
                                </option>
                            @endforeach
                        </select>
                    </td>
                    <td class="px-2 py-3 whitespace-nowrap text-center" id="handover-cell-{{ $prospect->id }}">
                        @if($prospect->hq_status === 'handover')
                            <input type="date"
                                id="handover-date-{{ $prospect->id }}"
                                class="w-28 h-7 text-xs px-1 border-2 border-emerald-300 rounded cursor-pointer hover:border-emerald-400 focus:outline-none focus:border-emerald-500 text-emerald-600 font-medium bg-emerald-50"
                                value="{{ $prospect->handover_at ? \Carbon\Carbon::parse($prospect->handover_at)->format('Y-m-d') : '' }}"
                                onchange="saveHandoverDate({{ $prospect->id }}, this.value)">
                        @else
                            <span class="text-sm text-gray-400">-</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap">
                        @if(in_array($prospect->status, ['active', 'completed']))
                        <button type="button"
                            id="status-btn-{{ $prospect->id }}"
                            onclick="toggleProspectStatus({{ $prospect->id }}, event)"
                            class="px-2 py-1 text-xs font-medium rounded-full cursor-pointer hover:opacity-80 transition {{ $prospect->status_color }}">
                            {{ $prospect->status_label }}
                        </button>
                        @else
                        <span class="px-2 py-1 text-xs font-medium rounded-full {{ $prospect->status_color }}">
                            {{ $prospect->status_label }}
                        </span>
                        @endif
                    </td>
                    <td class="px-4 py-3 whitespace-nowrap text-right text-sm font-medium">
                        <div class="flex items-center justify-end gap-2">
                            <button type="button" onclick="openDetailModal({{ $prospect->id }})" class="text-blue-600 hover:text-blue-900">상세</button>
                            @if($isSuperAdmin)
                            <button type="button"
                                onclick="openDeleteModal({{ $prospect->id }}, '{{ addslashes($prospect->company_name) }}')"
                                class="text-red-500 hover:text-red-700"
                                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="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                </svg>
                            </button>
                            @endif
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="13" class="px-6 py-12 text-center text-gray-500">
                        등록된 고객이 없습니다.
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    {{-- 페이지네이션 --}}
    @if($prospects->hasPages())
    <div class="px-6 py-4 border-t border-gray-200">
        {{ $prospects->withQueryString()->links() }}
    </div>
    @endif
</div>
