@extends('layouts.app')

@section('title', '영업권 수정')

@section('content')
<div class="max-w-2xl mx-auto">
    <!-- 페이지 헤더 -->
    <div class="mb-6">
        <a href="{{ route('sales.prospects.show', $prospect->id) }}" 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">영업권 수정</h1>
        <p class="text-sm text-gray-500 mt-1">{{ $prospect->company_name }} ({{ $prospect->business_number }})</p>
    </div>

    <!-- 알림 메시지 -->
    @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

    <!-- 폼 -->
    <form action="{{ route('sales.prospects.update', $prospect->id) }}" method="POST" enctype="multipart/form-data" class="bg-white rounded-lg shadow-sm p-6 space-y-6">
        @csrf
        @method('PUT')

        <!-- 사업자번호 (수정 불가) -->
        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">사업자번호</label>
            <input type="text" value="{{ $prospect->business_number }}" disabled
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-gray-100 text-gray-500">
            <p class="mt-1 text-xs text-gray-500">사업자번호는 수정할 수 없습니다</p>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">회사명 <span class="text-red-500">*</span></label>
                <input type="text" name="company_name" value="{{ old('company_name', $prospect->company_name) }}" required
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 @error('company_name') border-red-500 @enderror">
                @error('company_name')
                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                @enderror
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">대표자명</label>
                <input type="text" name="ceo_name" value="{{ old('ceo_name', $prospect->ceo_name) }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">연락처</label>
                <input type="text" name="contact_phone" value="{{ old('contact_phone', $prospect->contact_phone) }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">이메일</label>
                <input type="email" name="contact_email" value="{{ old('contact_email', $prospect->contact_email) }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
            </div>
        </div>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">주소</label>
            <input type="text" name="address" value="{{ old('address', $prospect->address) }}"
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">명함 이미지</label>
            @if($prospect->hasBusinessCard())
            <div class="mb-2 p-2 bg-gray-50 rounded-lg" id="business_card_preview">
                <div class="flex items-start gap-3">
                    <img src="{{ $prospect->business_card_url }}" alt="현재 명함" class="max-h-32 rounded">
                    <button type="button" onclick="deleteAttachment('business_card')"
                            class="text-red-500 hover:text-red-700 text-sm flex items-center gap-1">
                        <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>
                </div>
                <p class="text-xs text-gray-500 mt-1">새 이미지를 업로드하면 기존 이미지가 교체됩니다</p>
            </div>
            @endif
            <input type="file" name="business_card" accept="image/*"
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
            <p class="mt-1 text-xs text-gray-500">JPG, PNG 형식 (최대 5MB)</p>
        </div>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">메모</label>
            <textarea name="memo" rows="3"
                      class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">{{ old('memo', $prospect->memo) }}</textarea>
        </div>

        <!-- 영업권 상태 정보 -->
        <div class="bg-gray-50 border border-gray-200 rounded-lg p-4">
            <h3 class="font-medium text-gray-800 mb-2">영업권 상태</h3>
            <dl class="grid grid-cols-2 gap-2 text-sm">
                <dt class="text-gray-500">상태</dt>
                <dd class="font-medium">
                    <span class="px-2 py-1 text-xs font-medium rounded-full {{ $prospect->status_color }}">
                        {{ $prospect->status_label }}
                    </span>
                </dd>
                <dt class="text-gray-500">등록일</dt>
                <dd class="font-medium">{{ $prospect->registered_at->format('Y-m-d') }}</dd>
                <dt class="text-gray-500">만료일</dt>
                <dd class="font-medium">{{ $prospect->expires_at->format('Y-m-d') }}</dd>
                <dt class="text-gray-500">등록자</dt>
                <dd class="font-medium">{{ $prospect->registeredBy?->name ?? '-' }}</dd>
            </dl>
        </div>

        <div class="flex justify-end gap-3 pt-4 border-t">
            <a href="{{ route('sales.prospects.show', $prospect->id) }}"
               class="px-6 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition">
                취소
            </a>
            <button type="submit"
                    class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                수정
            </button>
        </div>
    </form>
</div>

@push('scripts')
<script>
function deleteAttachment(type) {
    if (!confirm('이미지를 삭제하시겠습니까?')) {
        return;
    }

    fetch('{{ route("sales.prospects.delete-attachment", $prospect->id) }}?type=' + type, {
        method: 'DELETE',
        headers: {
            'X-CSRF-TOKEN': '{{ csrf_token() }}',
            'Accept': 'application/json',
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            document.getElementById(type + '_preview').remove();
        } else {
            alert(data.message || '삭제에 실패했습니다.');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('삭제 중 오류가 발생했습니다.');
    });
}
</script>
@endpush
@endsection
