{{-- 구독관리 탭 (서버 렌더링 Blade) --}}
<div class="space-y-6">
    {{-- 통계 카드 --}}
    <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
        <div class="bg-white rounded-lg shadow-sm p-4 border-l-4 border-teal-500">
            <p class="text-sm text-gray-500">활성 구독</p>
            <p class="text-xl font-bold text-teal-600">{{ $stats['activeCount'] }}건</p>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4 border-l-4 border-indigo-500">
            <p class="text-sm text-gray-500">월 반복 수익(MRR)</p>
            <p class="text-xl font-bold text-indigo-600">{{ number_format($stats['monthlyRecurring']) }}원</p>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4 border-l-4 border-emerald-500">
            <p class="text-sm text-gray-500">연 반복 수익(ARR)</p>
            <p class="text-xl font-bold text-emerald-600">{{ number_format($stats['yearlyRecurring']) }}원</p>
        </div>
        <div class="bg-white rounded-lg shadow-sm p-4 border-l-4 border-gray-400">
            <p class="text-sm text-gray-500">구독 상품</p>
            <p class="text-xl font-bold text-gray-700">{{ $stats['totalProducts'] }}개</p>
        </div>
    </div>

    {{-- 검색/필터 --}}
    <div class="bg-white rounded-lg shadow-sm p-4">
        <form hx-get="{{ route('finance.settlement.subscription') }}"
              hx-target="#subscription-content"
              hx-trigger="submit"
              class="flex flex-wrap items-end gap-3">
            <div class="w-[calc(50%-6px)] sm:w-auto sm:min-w-[160px]">
                <label class="block text-xs font-medium text-gray-500 mb-1">검색</label>
                <input type="text" name="search" placeholder="고객사명"
                       value="{{ request('search') }}"
                       class="w-full rounded-lg border-gray-300 text-sm focus:border-teal-500 focus:ring-teal-500">
            </div>
            <div class="flex items-end gap-1 w-full sm:w-auto">
                <button type="submit" class="flex-1 sm:flex-none px-4 py-2 bg-teal-600 hover:bg-teal-700 text-white text-sm rounded-lg transition-colors">
                    조회
                </button>
            </div>
        </form>
    </div>

    {{-- 테이블 --}}
    <div class="bg-white rounded-lg shadow-sm overflow-hidden">
        <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-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>
                        <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>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse ($managements as $mgmt)
                        @php
                            $companyName = $mgmt->tenant?->company_name ?? $mgmt->tenantProspect?->company_name ?? '-';
                            $partnerName = $mgmt->salesPartner?->user?->name ?? '-';
                            $subscriptionProducts = $mgmt->contractProducts->where('subscription_fee', '>', 0);
                            $monthlyTotal = $subscriptionProducts->sum('subscription_fee');

                            $statusColor = match ($mgmt->hq_status) {
                                'review' => 'bg-purple-100 text-purple-700',
                                'planning' => 'bg-blue-100 text-blue-700',
                                'coding' => 'bg-indigo-100 text-indigo-700',
                                'dev_test' => 'bg-cyan-100 text-cyan-700',
                                'dev_done' => 'bg-teal-100 text-teal-700',
                                'int_test' => 'bg-amber-100 text-amber-700',
                                'handover' => 'bg-green-100 text-green-700',
                                default => 'bg-gray-100 text-gray-700',
                            };

                            $contractStatusColor = match ($mgmt->status) {
                                'contracted' => 'bg-blue-100 text-blue-700',
                                'onboarding' => 'bg-amber-100 text-amber-700',
                                'active' => 'bg-green-100 text-green-700',
                                default => 'bg-gray-100 text-gray-700',
                            };
                            $contractStatusLabel = \App\Models\Sales\SalesTenantManagement::$statusLabels[$mgmt->status] ?? $mgmt->status;
                        @endphp
                        <tr class="hover:bg-gray-50">
                            {{-- 고객사 --}}
                            <td class="px-4 py-3">
                                <div class="text-sm font-medium text-gray-900">{{ $companyName }}</div>
                            </td>

                            {{-- 담당 파트너 --}}
                            <td class="px-4 py-3 text-sm text-gray-900">{{ $partnerName }}</td>

                            {{-- 구독 상품 --}}
                            <td class="px-4 py-3">
                                <div class="space-y-1">
                                    @foreach ($subscriptionProducts as $cp)
                                        <div class="flex items-center gap-2">
                                            <span class="text-sm text-gray-900">{{ $cp->product?->name ?? $cp->category?->name ?? '상품' }}</span>
                                            <span class="text-xs text-gray-500">{{ number_format($cp->subscription_fee) }}원/월</span>
                                        </div>
                                    @endforeach
                                </div>
                            </td>

                            {{-- 월 구독료 합계 --}}
                            <td class="px-4 py-3 text-sm text-right font-medium text-teal-600">
                                {{ number_format($monthlyTotal) }}원
                            </td>

                            {{-- 계약일 --}}
                            <td class="px-4 py-3 text-sm text-center text-gray-600">
                                {{ $mgmt->contracted_at ? \Carbon\Carbon::parse($mgmt->contracted_at)->format('Y-m-d') : '-' }}
                            </td>

                            {{-- 개발상태 --}}
                            <td class="px-4 py-3 text-center">
                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium {{ $statusColor }}">
                                    {{ $mgmt->hq_status_label }}
                                </span>
                            </td>

                            {{-- 입금상태 --}}
                            <td class="px-4 py-3 text-center">
                                <div class="flex flex-col items-center gap-1">
                                    <span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium {{ $mgmt->deposit_status === 'paid' ? 'bg-green-100 text-green-700' : 'bg-yellow-100 text-yellow-700' }}">
                                        계약금 {{ $mgmt->deposit_status === 'paid' ? '완료' : '대기' }}
                                    </span>
                                    <span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium {{ $mgmt->balance_status === 'paid' ? 'bg-green-100 text-green-700' : 'bg-yellow-100 text-yellow-700' }}">
                                        잔금 {{ $mgmt->balance_status === 'paid' ? '완료' : '대기' }}
                                    </span>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-4 py-8 text-center text-gray-500">
                                구독 상품이 있는 고객사가 없습니다.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
