@extends('layouts.admin')

@section('title', isset($job) ? 'Edit Lowongan' : 'Tambah Lowongan')

@section('content')
<div class="max-w-4xl">
    <div class="bg-white rounded-xl border border-gray-100 p-8">
        <form action="{{ isset($job) ? route('admin.jobs.update', $job) : route('admin.jobs.store') }}" method="POST" enctype="multipart/form-data">
            @csrf
            @if(isset($job)) @method('PUT') @endif

            <div class="space-y-5">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Posisi / Judul *</label>
                        <input type="text" name="title" value="{{ old('title', $job->title ?? '') }}" required
                            class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none">
                        @error('title') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Perusahaan *</label>
                        <input type="text" name="company" value="{{ old('company', $job->company ?? '') }}" required
                            class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none">
                    </div>
                </div>

                {{-- Image Uploads --}}
                <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Logo Perusahaan</label>
                        @if(isset($job) && $job->company_logo)
                        <div class="mb-2 flex items-center space-x-3">
                            <img src="{{ asset('storage/' . $job->company_logo) }}" class="w-16 h-16 object-contain rounded-lg border">
                            <label class="flex items-center space-x-2 text-sm text-red-500">
                                <input type="checkbox" name="remove_logo" value="1"> <span>Hapus logo</span>
                            </label>
                        </div>
                        @endif
                        <input type="file" name="company_logo" accept="image/*"
                            class="w-full border border-gray-200 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-indigo-500 outline-none">
                        <p class="text-xs text-gray-400 mt-1">JPG, PNG, WebP. Maks 2MB</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Gambar Lowongan</label>
                        @if(isset($job) && $job->image)
                        <div class="mb-2 flex items-center space-x-3">
                            <img src="{{ asset('storage/' . $job->image) }}" class="w-16 h-16 object-cover rounded-lg border">
                            <label class="flex items-center space-x-2 text-sm text-red-500">
                                <input type="checkbox" name="remove_image" value="1"> <span>Hapus gambar</span>
                            </label>
                        </div>
                        @endif
                        <input type="file" name="image" accept="image/*"
                            class="w-full border border-gray-200 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-indigo-500 outline-none">
                        <p class="text-xs text-gray-400 mt-1">JPG, PNG, WebP. Maks 2MB</p>
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Lokasi *</label>
                        <input type="text" name="location" value="{{ old('location', $job->location ?? '') }}" required
                            class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Tipe *</label>
                        <select name="type" required
                            class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none">
                            @foreach(['Full-time', 'Part-time', 'Freelance', 'Internship', 'Contract'] as $type)
                            <option value="{{ $type }}" {{ old('type', $job->type ?? '') === $type ? 'selected' : '' }}>{{ $type }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Gaji Minimum (Rp)</label>
                        <input type="number" name="salary_min" value="{{ old('salary_min', $job->salary_min ?? '') }}"
                            class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none" placeholder="3000000">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Gaji Maksimum (Rp)</label>
                        <input type="number" name="salary_max" value="{{ old('salary_max', $job->salary_max ?? '') }}"
                            class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none" placeholder="8000000">
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Deskripsi Pekerjaan *</label>
                    <textarea name="description" rows="5" required
                        class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none">{{ old('description', $job->description ?? '') }}</textarea>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Persyaratan *</label>
                    <textarea name="requirements" rows="5" required
                        class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none">{{ old('requirements', $job->requirements ?? '') }}</textarea>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Benefit</label>
                    <textarea name="benefits" rows="3"
                        class="w-full border border-gray-200 rounded-lg px-4 py-3 focus:ring-2 focus:ring-indigo-500 outline-none">{{ old('benefits', $job->benefits ?? '') }}</textarea>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                    <div class="flex items-end">
                        <label class="flex items-center space-x-3 py-3">
                            <input type="checkbox" name="is_active" value="1"
                                {{ old('is_active', $job->is_active ?? true) ? 'checked' : '' }}
                                class="w-4 h-4 text-indigo-600 rounded focus:ring-indigo-500">
                            <span class="text-sm font-medium text-gray-700">Aktif (Tampilkan di website)</span>
                        </label>
                    </div>
                </div>
            </div>

            <div class="flex items-center space-x-3 mt-8">
                <button type="submit" class="bg-indigo-600 text-white px-6 py-2.5 rounded-lg font-medium hover:outline hover:outline-2 hover:outline-indigo-800">
                    {{ isset($job) ? 'Simpan Perubahan' : 'Tambah Lowongan' }}
                </button>
                <a href="{{ route('admin.jobs.index') }}" class="text-gray-500 hover:text-gray-700 font-medium">Batal</a>
            </div>
        </form>
    </div>

    {{-- Stage Management (only for existing jobs) --}}
    @if(isset($job))
    <div class="bg-white rounded-xl border border-gray-100 p-8 mt-6">
        <div class="flex items-center justify-between mb-6">
            <div>
                <h3 class="text-lg font-semibold text-gray-800">Tahapan Seleksi</h3>
                <p class="text-sm text-gray-500 mt-1">5 tahapan seleksi tetap — klik Edit untuk mengatur formulir</p>
            </div>
        </div>

        @if($job->stages->count() > 0)
        <div class="space-y-3">
            @foreach($job->stages as $stage)
            <div class="flex flex-col sm:flex-row sm:items-center justify-between p-4 bg-gray-50 rounded-lg border {{ $stage->is_active ? 'border-gray-200' : 'border-red-200 bg-red-50 opacity-60' }}">
                <div class="flex items-center space-x-4 mb-3 sm:mb-0">
                    <div class="w-10 h-10 rounded-full bg-indigo-100 text-indigo-700 flex items-center justify-center font-bold text-sm">
                        {{ $stage->stage_number }}
                    </div>
                    <div>
                        <h4 class="font-medium text-gray-800">{{ $stage->name }}</h4>
                        <p class="text-xs text-gray-500">
                            {{ $stage->form_fields ? count($stage->form_fields) . ' field form' : 'Tanpa form (hanya seleksi admin)' }}
                            @if(!$stage->is_active) <span class="text-red-500 font-medium">— Nonaktif</span> @endif
                        </p>
                    </div>
                </div>
                <div class="flex items-center space-x-2">
                    <a href="{{ route('admin.jobs.stages.edit', [$job, $stage]) }}"
                        class="text-indigo-600 hover:text-indigo-800 text-sm font-medium px-3 py-1.5 rounded-lg hover:outline hover:outline-2 hover:outline-indigo-300">
                        Edit
                    </a>
                    <form action="{{ route('admin.jobs.stages.toggle', [$job, $stage]) }}" method="POST" class="inline">
                        @csrf @method('PATCH')
                        <button type="submit" class="text-sm font-medium px-3 py-1.5 rounded-lg {{ $stage->is_active ? 'text-yellow-600 hover:outline hover:outline-2 hover:outline-yellow-200' : 'text-green-600 hover:outline hover:outline-2 hover:outline-green-200' }}">
                            {{ $stage->is_active ? 'Nonaktifkan' : 'Aktifkan' }}
                        </button>
                    </form>
                </div>
            </div>
            @endforeach
        </div>
        @else
        <div class="text-center py-8 bg-gray-50 rounded-lg">
            <p class="text-gray-400 mb-2">Belum ada tahapan seleksi</p>
            <p class="text-sm text-gray-500">Tahapan akan otomatis dibuat saat lowongan disimpan.</p>
        </div>
        @endif
    </div>
    @endif
</div>
@endsection
