@extends('layouts.app')

@section('title', '계획 수정')

@section('content')
<!-- 페이지 헤더 -->
<div class="flex items-center gap-4 mb-6">
    <a href="{{ route('roadmap.plans.show', $plan->id) }}" class="text-gray-500 hover:text-gray-700">
        ← 계획 상세
    </a>
    <h1 class="text-2xl font-bold text-gray-800">계획 수정</h1>
</div>

<!-- 폼 -->
<div class="bg-white rounded-lg shadow-sm p-6 max-w-3xl">
    <form id="planForm">
        <!-- 제목 -->
        <div class="mb-6">
            <label for="title" class="block text-sm font-medium text-gray-700 mb-2">
                계획 제목 <span class="text-red-500">*</span>
            </label>
            <input type="text" id="title" name="title" required
                   value="{{ $plan->title }}"
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
        </div>

        <!-- 설명 -->
        <div class="mb-6">
            <label for="description" class="block text-sm font-medium text-gray-700 mb-2">설명</label>
            <textarea id="description" name="description" rows="3"
                      class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">{{ $plan->description }}</textarea>
        </div>

        <!-- 상세 내용 -->
        <div class="mb-6">
            <label for="content" class="block text-sm font-medium text-gray-700 mb-2">상세 내용</label>
            <textarea id="content" name="content" rows="8"
                      class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">{{ $plan->content }}</textarea>
        </div>

        <!-- 2열 레이아웃 -->
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-6">
            <div>
                <label for="category" class="block text-sm font-medium text-gray-700 mb-2">카테고리</label>
                <select id="category" name="category"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    @foreach($categories as $value => $label)
                    <option value="{{ $value }}" {{ $plan->category === $value ? 'selected' : '' }}>{{ $label }}</option>
                    @endforeach
                </select>
            </div>

            <div>
                <label for="status" class="block text-sm font-medium text-gray-700 mb-2">상태</label>
                <select id="status" name="status"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    @foreach($statuses as $value => $label)
                    <option value="{{ $value }}" {{ $plan->status === $value ? 'selected' : '' }}>{{ $label }}</option>
                    @endforeach
                </select>
            </div>

            <div>
                <label for="priority" class="block text-sm font-medium text-gray-700 mb-2">우선순위</label>
                <select id="priority" name="priority"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    @foreach($priorities as $value => $label)
                    <option value="{{ $value }}" {{ $plan->priority === $value ? 'selected' : '' }}>{{ $label }}</option>
                    @endforeach
                </select>
            </div>

            <div>
                <label for="phase" class="block text-sm font-medium text-gray-700 mb-2">Phase</label>
                <select id="phase" name="phase"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    @foreach($phases as $value => $label)
                    <option value="{{ $value }}" {{ $plan->phase === $value ? 'selected' : '' }}>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
        </div>

        <!-- 기간 + 진행률 + 색상 -->
        <div class="grid grid-cols-1 sm:grid-cols-4 gap-4 mb-6">
            <div>
                <label for="start_date" class="block text-sm font-medium text-gray-700 mb-2">시작일</label>
                <input type="date" id="start_date" name="start_date"
                       value="{{ $plan->start_date?->format('Y-m-d') }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
            </div>
            <div>
                <label for="end_date" class="block text-sm font-medium text-gray-700 mb-2">종료일</label>
                <input type="date" id="end_date" name="end_date"
                       value="{{ $plan->end_date?->format('Y-m-d') }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
            </div>
            <div>
                <label for="progress" class="block text-sm font-medium text-gray-700 mb-2">진행률 (%)</label>
                <input type="number" id="progress" name="progress" min="0" max="100"
                       value="{{ $plan->progress }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
            </div>
            <div>
                <label for="color" class="block text-sm font-medium text-gray-700 mb-2">색상</label>
                <div class="flex items-center gap-2">
                    <input type="color" id="color" name="color" value="{{ $plan->color }}"
                           class="w-10 h-10 border border-gray-300 rounded cursor-pointer">
                    <span id="colorHex" class="text-sm text-gray-500">{{ $plan->color }}</span>
                </div>
            </div>
        </div>

        <!-- 버튼 -->
        <div class="flex gap-4">
            <button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white px-6 py-2 rounded-lg transition">
                수정
            </button>
            <a href="{{ route('roadmap.plans.show', $plan->id) }}" class="bg-gray-300 hover:bg-gray-400 text-gray-700 px-6 py-2 rounded-lg transition">
                취소
            </a>
        </div>
    </form>
</div>
@endsection

@push('scripts')
<script>
    document.getElementById('color').addEventListener('input', function() {
        document.getElementById('colorHex').textContent = this.value;
    });

    document.getElementById('planForm').addEventListener('submit', async function(e) {
        e.preventDefault();

        const formData = new FormData(this);
        const data = Object.fromEntries(formData.entries());

        try {
            const response = await fetch('/api/admin/roadmap/plans/{{ $plan->id }}', {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Accept': 'application/json'
                },
                body: JSON.stringify(data)
            });

            const result = await response.json();

            if (result.success) {
                showToast(result.message, 'success');
                window.location.href = '{{ route('roadmap.plans.show', $plan->id) }}';
            } else {
                showToast(result.message || '수정에 실패했습니다.', 'error');
            }
        } catch (error) {
            console.error('Error:', error);
            showToast('수정 중 오류가 발생했습니다.', 'error');
        }
    });
</script>
@endpush
