@extends('layouts.app')

@section('title', '영업권 상세')

@section('content')
<div class="max-w-4xl mx-auto">
    <!-- 페이지 헤더 -->
    <div class="mb-6 flex justify-between items-start">
        <div>
            <a href="{{ route('sales.prospects.index') }}" class="text-gray-500 hover:text-gray-700 text-sm mb-2 inline-flex items-center">
                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
                </svg>
                목록으로
            </a>
            <h1 class="text-2xl font-bold text-gray-800">{{ $prospect->company_name }}</h1>
            <p class="text-sm text-gray-500 mt-1">
                <span class="px-2 py-1 text-xs font-medium rounded-full {{ $prospect->status_color }}">
                    {{ $prospect->status_label }}
                </span>
                <span class="ml-2">{{ $prospect->business_number }}</span>
            </p>
        </div>
        <div class="flex gap-2">
            @if(!$prospect->isConverted())
            <a href="{{ route('sales.prospects.edit', $prospect->id) }}"
               class="bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-2 rounded-lg transition">
                수정
            </a>
            @if($prospect->isActive())
            <form action="{{ route('sales.prospects.convert', $prospect->id) }}" method="POST"
                  onsubmit="return confirm('테넌트로 전환하시겠습니까?\n\n전환 후에는 수정/삭제가 불가능합니다.')">
                @csrf
                <button type="submit"
                        class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg transition">
                    테넌트 전환
                </button>
            </form>
            @endif
            @endif
        </div>
    </div>

    <!-- 알림 메시지 -->
    @if(session('success'))
    <div class="mb-4 p-4 bg-green-50 border border-green-200 rounded-lg text-green-700">
        {{ session('success') }}
    </div>
    @endif
    @if(session('error'))
    <div class="mb-4 p-4 bg-red-50 border border-red-200 rounded-lg text-red-700">
        {{ session('error') }}
    </div>
    @endif

    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <!-- 기본 정보 -->
        <div class="bg-white rounded-lg shadow-sm p-6">
            <h2 class="text-lg font-semibold text-gray-800 mb-4">회사 정보</h2>
            <dl class="space-y-3">
                <div class="flex justify-between">
                    <dt class="text-gray-500">사업자번호</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->business_number }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">회사명</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->company_name }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">대표자</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->ceo_name ?? '-' }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">연락처</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->contact_phone ?? '-' }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">이메일</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->contact_email ?? '-' }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">주소</dt>
                    <dd class="font-medium text-gray-900 text-right">{{ $prospect->address ?? '-' }}</dd>
                </div>
            </dl>
        </div>

        <!-- 영업권 정보 -->
        <div class="bg-white rounded-lg shadow-sm p-6">
            <h2 class="text-lg font-semibold text-gray-800 mb-4">영업권 정보</h2>
            <dl class="space-y-3">
                <div class="flex justify-between">
                    <dt class="text-gray-500">등록자</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->registeredBy?->name ?? '-' }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">등록일</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->registered_at->format('Y-m-d H:i') }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">만료일</dt>
                    <dd class="font-medium {{ $prospect->isActive() ? 'text-blue-600' : 'text-gray-500' }}">
                        {{ $prospect->expires_at->format('Y-m-d H:i') }}
                        @if($prospect->isActive())
                        <span class="text-sm">(D-{{ $prospect->remaining_days }})</span>
                        @endif
                    </dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">대기 종료일</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->cooldown_ends_at->format('Y-m-d H:i') }}</dd>
                </div>
                @if($prospect->isConverted())
                <div class="flex justify-between">
                    <dt class="text-gray-500">전환일</dt>
                    <dd class="font-medium text-green-600">{{ $prospect->converted_at?->format('Y-m-d H:i') }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">전환 처리자</dt>
                    <dd class="font-medium text-gray-900">{{ $prospect->convertedBy?->name ?? '-' }}</dd>
                </div>
                <div class="flex justify-between">
                    <dt class="text-gray-500">전환된 테넌트</dt>
                    <dd class="font-medium text-blue-600">
                        @if($prospect->tenant)
                        <a href="{{ route('tenants.edit', $prospect->tenant_id) }}" class="hover:underline">
                            {{ $prospect->tenant->company_name }} (ID: {{ $prospect->tenant_id }})
                        </a>
                        @else
                        ID: {{ $prospect->tenant_id }}
                        @endif
                    </dd>
                </div>
                @endif
            </dl>
        </div>
    </div>

    <!-- 첨부 이미지 -->
    @if($prospect->hasBusinessCard())
    <div class="mt-6 bg-white rounded-lg shadow-sm p-6">
        <h2 class="text-lg font-semibold text-gray-800 mb-4">첨부 서류</h2>
        <div class="grid grid-cols-1 gap-6">
            <!-- 명함 이미지 -->
            <div class="text-center">
                <h3 class="text-sm font-medium text-gray-600 mb-2">명함</h3>
                <a href="{{ $prospect->business_card_url }}" target="_blank" class="block">
                    <img src="{{ $prospect->business_card_url }}" alt="명함 이미지" class="max-h-48 mx-auto rounded-lg shadow hover:shadow-lg transition">
                </a>
            </div>
        </div>
    </div>
    @endif

    <!-- 메모 -->
    @if($prospect->memo)
    <div class="mt-6 bg-white rounded-lg shadow-sm p-6">
        <h2 class="text-lg font-semibold text-gray-800 mb-4">메모</h2>
        <p class="text-gray-700 whitespace-pre-line">{{ $prospect->memo }}</p>
    </div>
    @endif

    <!-- 상태별 안내 -->
    <div class="mt-6">
        @if($prospect->isActive())
        <div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
            <h3 class="font-medium text-blue-800 mb-2">영업권 유효</h3>
            <p class="text-sm text-blue-700">
                {{ $prospect->expires_at->format('Y-m-d') }}까지 영업권이 유효합니다.
                남은 기간: <strong>{{ $prospect->remaining_days }}일</strong>
            </p>
        </div>
        @elseif($prospect->isInCooldown())
        <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4">
            <h3 class="font-medium text-yellow-800 mb-2">재등록 대기 기간</h3>
            <p class="text-sm text-yellow-700">
                영업권이 만료되었습니다.
                {{ $prospect->cooldown_ends_at->format('Y-m-d') }} 이후 다른 영업파트너가 재등록할 수 있습니다.
            </p>
        </div>
        @elseif($prospect->isConverted())
        <div class="bg-green-50 border border-green-200 rounded-lg p-4">
            <h3 class="font-medium text-green-800 mb-2">테넌트 전환 완료</h3>
            <p class="text-sm text-green-700">
                {{ $prospect->converted_at?->format('Y-m-d') }}에 테넌트로 전환되었습니다.
            </p>
        </div>
        @else
        <div class="bg-gray-50 border border-gray-200 rounded-lg p-4">
            <h3 class="font-medium text-gray-800 mb-2">영업권 만료</h3>
            <p class="text-sm text-gray-700">
                영업권이 만료되었습니다. 대기 기간이 종료되어 재등록이 가능합니다.
            </p>
        </div>
        @endif
    </div>
</div>
@endsection
