@extends('layouts.app')

@section('title', '앱 버전 관리')

@section('content')
<div class="max-w-6xl mx-auto">
    <h1 class="text-2xl font-bold mb-6">앱 버전 관리</h1>

    {{-- 성공 메시지 --}}
    @if(session('success'))
        <div class="mb-4 rounded-lg bg-green-50 p-4 text-green-800 border border-green-200">
            {{ session('success') }}
        </div>
    @endif

    {{-- 에러 메시지 --}}
    @if($errors->any())
        <div class="mb-4 rounded-lg bg-red-50 p-4 text-red-800 border border-red-200">
            <ul class="list-disc list-inside">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    {{-- 등록 폼 --}}
    <div class="bg-white rounded-lg shadow p-6 mb-6">
        <h2 class="text-lg font-semibold mb-4">새 버전 등록</h2>
        <form action="{{ route('app-versions.store') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">버전 코드 (정수) *</label>
                    <input type="number" name="version_code" value="{{ old('version_code') }}" required min="1"
                           class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm"
                           placeholder="예: 2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">버전명 *</label>
                    <input type="text" name="version_name" value="{{ old('version_name') }}" required maxlength="20"
                           class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm"
                           placeholder="예: 0.2">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">플랫폼</label>
                    <select name="platform" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
                        <option value="android" {{ old('platform') === 'ios' ? '' : 'selected' }}>Android</option>
                        <option value="ios" {{ old('platform') === 'ios' ? 'selected' : '' }}>iOS</option>
                    </select>
                </div>
                <div class="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-1">변경사항</label>
                    <textarea name="release_notes" rows="3"
                              class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm"
                              placeholder="- 기능 추가&#10;- 버그 수정">{{ old('release_notes') }}</textarea>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">APK 파일</label>
                    <input type="file" name="apk_file" accept=".apk"
                           class="w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-md file:border-0 file:text-sm file:font-medium file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100">
                </div>
                <div class="flex items-end">
                    <label class="inline-flex items-center">
                        <input type="hidden" name="force_update" value="0">
                        <input type="checkbox" name="force_update" value="1" {{ old('force_update') ? 'checked' : '' }}
                               class="rounded border-gray-300 text-blue-600 shadow-sm focus:ring-blue-500">
                        <span class="ml-2 text-sm text-gray-700">강제 업데이트</span>
                    </label>
                </div>
            </div>
            <div class="mt-4">
                <button type="submit" class="inline-flex items-center px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                    등록
                </button>
            </div>
        </form>
    </div>

    {{-- 버전 목록 --}}
    <div class="bg-white rounded-lg shadow 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">버전</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">플랫폼</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">변경사항</th>
                        <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase">강제</th>
                        <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase">활성</th>
                        <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase">다운로드</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">APK</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">배포일</th>
                        <th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase">관리</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($versions as $version)
                        @php $isTrashed = $version->trashed(); @endphp
                        <tr class="{{ $isTrashed ? 'bg-red-50' : ($version->is_active ? '' : 'bg-gray-50 opacity-60') }}">
                            <td class="px-4 py-3 whitespace-nowrap">
                                <div class="text-sm font-medium {{ $isTrashed ? 'text-red-400 line-through' : 'text-gray-900' }}">v{{ $version->version_name }}</div>
                                <div class="text-xs {{ $isTrashed ? 'text-red-300' : 'text-gray-500' }}">code: {{ $version->version_code }}</div>
                                @if($isTrashed)
                                    <div class="text-xs text-red-500 font-medium mt-0.5">삭제됨 {{ $version->deleted_at->format('m-d H:i') }}</div>
                                @endif
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap text-sm {{ $isTrashed ? 'text-red-300' : 'text-gray-500' }}">
                                {{ strtoupper($version->platform) }}
                            </td>
                            <td class="px-4 py-3 text-sm {{ $isTrashed ? 'text-red-300' : 'text-gray-500' }} max-w-xs truncate">
                                {{ $version->release_notes ? \Illuminate\Support\Str::limit($version->release_notes, 60) : '-' }}
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap text-center">
                                @if($isTrashed)
                                    <span class="text-red-300 text-xs">-</span>
                                @elseif($version->force_update)
                                    <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-red-100 text-red-800">필수</span>
                                @else
                                    <span class="text-gray-400 text-xs">-</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap text-center">
                                @if($isTrashed)
                                    <span class="text-red-300 text-xs">-</span>
                                @else
                                    <form action="{{ route('app-versions.toggle', $version->id) }}" method="POST" class="inline">
                                        @csrf
                                        <button type="submit" class="text-sm {{ $version->is_active ? 'text-green-600 hover:text-green-800' : 'text-gray-400 hover:text-gray-600' }}">
                                            {{ $version->is_active ? 'ON' : 'OFF' }}
                                        </button>
                                    </form>
                                @endif
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap text-center text-sm {{ $isTrashed ? 'text-red-300' : 'text-gray-500' }}">
                                {{ number_format($version->download_count) }}
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap text-sm {{ $isTrashed ? 'text-red-300' : 'text-gray-500' }}">
                                @if($version->apk_path)
                                    @if($isTrashed)
                                        <span class="text-red-300" title="{{ $version->apk_original_name }}">
                                            {{ $version->apk_original_name ? \Illuminate\Support\Str::limit($version->apk_original_name, 20) : 'APK' }}
                                        </span>
                                    @else
                                        <a href="{{ route('app-versions.download', $version->id) }}" class="text-green-600 hover:text-green-800 hover:underline" title="{{ $version->apk_original_name }}">
                                            {{ $version->apk_original_name ? \Illuminate\Support\Str::limit($version->apk_original_name, 20) : 'APK' }}
                                        </a>
                                    @endif
                                    @if($version->apk_size)
                                        <span class="text-xs {{ $isTrashed ? 'text-red-200' : 'text-gray-400' }}">({{ number_format($version->apk_size / 1024 / 1024, 1) }}MB)</span>
                                    @endif
                                @else
                                    <span class="{{ $isTrashed ? 'text-red-300' : 'text-gray-400' }}">-</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap text-sm {{ $isTrashed ? 'text-red-300' : 'text-gray-500' }}">
                                {{ $version->published_at?->format('Y-m-d') ?? '-' }}
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap text-center">
                                @if($isTrashed && ($isSuperAdmin ?? false))
                                    <div class="flex items-center justify-center gap-2">
                                        <form action="{{ route('app-versions.restore', $version->id) }}" method="POST" class="inline"
                                              onsubmit="return confirm('v{{ $version->version_name }}을 복구하시겠습니까?')">
                                            @csrf
                                            <button type="submit" class="text-blue-600 hover:text-blue-800 text-sm">복구</button>
                                        </form>
                                        <form action="{{ route('app-versions.force-destroy', $version->id) }}" method="POST" class="inline"
                                              onsubmit="return confirm('v{{ $version->version_name }}을 영구 삭제하시겠습니까?\n이 작업은 되돌릴 수 없습니다.')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="text-red-600 hover:text-red-800 text-sm font-medium">영구삭제</button>
                                        </form>
                                    </div>
                                @elseif(!$isTrashed)
                                    <div class="flex items-center justify-center gap-2">
                                        <button type="button" onclick="openEditModal({{ json_encode([
                                            'id' => $version->id,
                                            'version_code' => $version->version_code,
                                            'version_name' => $version->version_name,
                                            'platform' => $version->platform,
                                            'release_notes' => $version->release_notes,
                                            'force_update' => $version->force_update,
                                            'apk_original_name' => $version->apk_original_name,
                                        ]) }})" class="text-blue-600 hover:text-blue-800 text-sm">수정</button>
                                        <form action="{{ route('app-versions.destroy', $version->id) }}" method="POST" class="inline"
                                              onsubmit="return confirm('v{{ $version->version_name }}을 삭제하시겠습니까?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="text-red-600 hover:text-red-800 text-sm">삭제</button>
                                        </form>
                                    </div>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="px-4 py-8 text-center text-gray-500">등록된 버전이 없습니다.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- 페이지네이션 --}}
        @if($versions->hasPages())
            <div class="px-4 py-3 border-t border-gray-200">
                {{ $versions->links() }}
            </div>
        @endif
    </div>
</div>
{{-- 수정 모달 --}}
<div id="editModal" class="fixed inset-0 bg-gray-500 bg-opacity-75 flex items-center justify-center z-50 hidden">
    <div class="bg-white rounded-lg shadow-xl w-full max-w-lg mx-4" onclick="event.stopPropagation()">
        <div class="flex items-center justify-between px-6 py-4 border-b">
            <h3 class="text-lg font-semibold text-gray-900">버전 수정</h3>
            <button type="button" onclick="closeEditModal()" class="text-gray-400 hover:text-gray-600">&times;</button>
        </div>
        <form id="editForm" method="POST" enctype="multipart/form-data" class="px-6 py-4">
            @csrf
            @method('PUT')
            <div class="space-y-4">
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">버전 코드 (정수) *</label>
                        <input type="number" name="version_code" id="edit_version_code" required min="1"
                               class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">버전명 *</label>
                        <input type="text" name="version_name" id="edit_version_name" required maxlength="20"
                               class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
                    </div>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">플랫폼</label>
                    <select name="platform" id="edit_platform" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm">
                        <option value="android">Android</option>
                        <option value="ios">iOS</option>
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">변경사항</label>
                    <textarea name="release_notes" id="edit_release_notes" rows="3"
                              class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 text-sm"></textarea>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">APK 파일</label>
                    <div id="edit_current_apk" class="text-xs text-gray-500 mb-1"></div>
                    <input type="file" name="apk_file" accept=".apk"
                           class="w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-md file:border-0 file:text-sm file:font-medium file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100">
                    <p class="text-xs text-gray-400 mt-1">새 파일을 선택하면 기존 파일이 교체됩니다.</p>
                </div>
                <div>
                    <label class="inline-flex items-center">
                        <input type="hidden" name="force_update" value="0">
                        <input type="checkbox" name="force_update" id="edit_force_update" value="1"
                               class="rounded border-gray-300 text-blue-600 shadow-sm focus:ring-blue-500">
                        <span class="ml-2 text-sm text-gray-700">강제 업데이트</span>
                    </label>
                </div>
            </div>
            <div class="flex justify-end gap-3 mt-6 pt-4 border-t">
                <button type="button" onclick="closeEditModal()" class="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50">취소</button>
                <button type="submit" class="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-md hover:bg-blue-700">저장</button>
            </div>
        </form>
    </div>
</div>

<script>
function openEditModal(data) {
    const form = document.getElementById('editForm');
    form.action = '/app-versions/' + data.id;

    document.getElementById('edit_version_code').value = data.version_code;
    document.getElementById('edit_version_name').value = data.version_name;
    document.getElementById('edit_platform').value = data.platform;
    document.getElementById('edit_release_notes').value = data.release_notes || '';
    document.getElementById('edit_force_update').checked = !!data.force_update;
    document.getElementById('edit_current_apk').textContent = data.apk_original_name
        ? '현재 파일: ' + data.apk_original_name
        : '업로드된 파일 없음';

    document.getElementById('editModal').classList.remove('hidden');
}

function closeEditModal() {
    document.getElementById('editModal').classList.add('hidden');
}

document.getElementById('editModal').addEventListener('click', function(e) {
    if (e.target === this) closeEditModal();
});
document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') closeEditModal();
});
</script>
@endsection
