@extends('layouts.app')

@section('title', 'Update Profil - ' . ($siteName ?? 'Bursa Kerja'))

@section('content')
<div class="max-w-3xl mx-auto px-4 py-8">
    {{-- Back to Dashboard --}}
    <a href="{{ route('dashboard') }}" class="inline-flex items-center text-sm text-gray-500 hover:text-indigo-600 mb-6">
        <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7"/></svg>
        Kembali ke Dashboard
    </a>

    <div class="bg-white rounded-xl border border-gray-100 p-6 md:p-8 animate-fade-in-up">
        <h1 class="text-2xl font-bold text-gray-800 mb-1">Update Profil</h1>
        <p class="text-gray-500 text-sm mb-6">Lengkapi data diri Anda untuk mempermudah proses lamaran</p>

        @if($errors->any())
        <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6">
            <ul class="list-disc list-inside text-sm">
                @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif

        <form action="{{ route('profile.update') }}" method="POST">
            @csrf

            {{-- Nama Lengkap --}}
            <div class="mb-5">
                <label for="name" class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap <span class="text-red-500">*</span></label>
                <input type="text" name="name" id="name" value="{{ old('name', $applicant->name) }}" required
                    class="w-full border border-gray-300 rounded-lg px-4 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none">
            </div>

            {{-- Email (readonly) --}}
            <div class="mb-5">
                <label for="email" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
                <input type="email" id="email" value="{{ $applicant->email }}" disabled
                    class="w-full border border-gray-200 rounded-lg px-4 py-2.5 bg-gray-50 text-gray-500 cursor-not-allowed">
                <p class="text-xs text-gray-400 mt-1">Email tidak dapat diubah</p>
            </div>

            {{-- WhatsApp --}}
            <div class="mb-5">
                <label for="whatsapp" class="block text-sm font-medium text-gray-700 mb-1">No. WhatsApp <span class="text-red-500">*</span></label>
                <input type="text" name="whatsapp" id="whatsapp" value="{{ old('whatsapp', $applicant->whatsapp) }}" required
                    class="w-full border border-gray-300 rounded-lg px-4 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none"
                    placeholder="08xxxxxxxxxx">
            </div>

            {{-- Pendidikan --}}
            <div class="mb-5">
                <label for="education" class="block text-sm font-medium text-gray-700 mb-1">Pendidikan Terakhir <span class="text-red-500">*</span></label>
                <select name="education" id="education" required
                    class="w-full border border-gray-300 rounded-lg px-4 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none">
                    <option value="">Pilih Pendidikan</option>
                    @foreach(['SD', 'SMP', 'SMA/SMK', 'D1', 'D2', 'D3', 'D4/S1', 'S2', 'S3'] as $edu)
                    <option value="{{ $edu }}" {{ old('education', $applicant->education) == $edu ? 'selected' : '' }}>{{ $edu }}</option>
                    @endforeach
                </select>
            </div>

            <hr class="my-6 border-gray-100">

            <h2 class="text-lg font-semibold text-gray-800 mb-4">Data Tambahan</h2>

            {{-- Tanggal Lahir --}}
            <div class="mb-5">
                <label for="birth_date" class="block text-sm font-medium text-gray-700 mb-1">Tanggal Lahir</label>
                <input type="date" name="birth_date" id="birth_date"
                    value="{{ old('birth_date', $applicant->birth_date?->format('Y-m-d')) }}"
                    max="{{ now()->subYears(15)->format('Y-m-d') }}"
                    class="w-full border border-gray-300 rounded-lg px-4 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none">
            </div>

            {{-- Jenis Kelamin --}}
            <div class="mb-5">
                <label class="block text-sm font-medium text-gray-700 mb-2">Jenis Kelamin</label>
                <div class="flex items-center space-x-6">
                    <label class="flex items-center cursor-pointer">
                        <input type="radio" name="gender" value="Laki-laki"
                            {{ old('gender', $applicant->gender) == 'Laki-laki' ? 'checked' : '' }}
                            class="w-4 h-4 text-indigo-600 border-gray-300 focus:ring-indigo-500">
                        <span class="ml-2 text-gray-700">Laki-laki</span>
                    </label>
                    <label class="flex items-center cursor-pointer">
                        <input type="radio" name="gender" value="Perempuan"
                            {{ old('gender', $applicant->gender) == 'Perempuan' ? 'checked' : '' }}
                            class="w-4 h-4 text-indigo-600 border-gray-300 focus:ring-indigo-500">
                        <span class="ml-2 text-gray-700">Perempuan</span>
                    </label>
                </div>
            </div>

            {{-- Provinsi --}}
            <div class="mb-5">
                <label for="province" class="block text-sm font-medium text-gray-700 mb-1">Provinsi</label>
                <select name="province" id="province"
                    class="w-full border border-gray-300 rounded-lg px-4 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none">
                    <option value="">Pilih Provinsi</option>
                    @foreach($provinces as $prov)
                    <option value="{{ $prov }}" {{ old('province', $applicant->province) == $prov ? 'selected' : '' }}>{{ $prov }}</option>
                    @endforeach
                </select>
            </div>

            {{-- Kota/Kabupaten --}}
            <div class="mb-5">
                <label for="city" class="block text-sm font-medium text-gray-700 mb-1">Kota / Kabupaten</label>
                <select name="city" id="city"
                    class="w-full border border-gray-300 rounded-lg px-4 py-2.5 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none">
                    <option value="">Pilih Kota/Kabupaten</option>
                </select>
            </div>

            {{-- Submit --}}
            <div class="flex items-center justify-between mt-8">
                <a href="{{ route('dashboard') }}" class="text-gray-500 hover:text-gray-700 text-sm font-medium">Batal</a>
                <button type="submit"
                    class="bg-indigo-600 text-white px-8 py-2.5 rounded-lg font-medium hover:outline hover:outline-2 hover:outline-indigo-800 text-sm pulse-cta">
                    Simpan Perubahan
                </button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('styles')
<style>
    select { appearance: auto; }
</style>
@endpush

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function () {
    const provinceSelect = document.getElementById('province');
    const citySelect = document.getElementById('city');
    const savedCity = @json(old('city', $applicant->city));

    function loadCities(province, selectedCity) {
        citySelect.innerHTML = '<option value="">Memuat...</option>';
        if (!province) {
            citySelect.innerHTML = '<option value="">Pilih Kota/Kabupaten</option>';
            return;
        }

        fetch('/api/cities/' + encodeURIComponent(province))
            .then(res => res.json())
            .then(cities => {
                citySelect.innerHTML = '<option value="">Pilih Kota/Kabupaten</option>';
                cities.forEach(city => {
                    const opt = document.createElement('option');
                    opt.value = city;
                    opt.textContent = city;
                    if (selectedCity && city === selectedCity) {
                        opt.selected = true;
                    }
                    citySelect.appendChild(opt);
                });
            })
            .catch(() => {
                citySelect.innerHTML = '<option value="">Pilih Kota/Kabupaten</option>';
            });
    }

    // Load cities on province change
    provinceSelect.addEventListener('change', function () {
        loadCities(this.value, null);
    });

    // Load cities on page load if province is already selected
    if (provinceSelect.value) {
        loadCities(provinceSelect.value, savedCity);
    }
});
</script>
@endpush
