@extends('admin.layout')

@section('content')
<link rel="stylesheet" href="{{ asset('css/fitur-admin.css') }}">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<div class="dashboard-section d-flex justify-content-center">
    <div style="width: 100%; max-width: 700px;">
        <div class="mb-4 text-center">
            <h4 class="fw-bold text-dark">Tambah Prospek Kerja Baru</h4>
            <p class="text-muted small">Tambahkan karir untuk direkomendasikan pada jurusan tertentu.</p>
        </div>

        <div class="card border-0 shadow-sm rounded-4">
            <div class="card-body p-5">
                <form id="formCreateProspek">
                    <div class="row g-4">
                        <div class="col-md-12 form-group-custom">
                            <label class="fw-bold small text-muted">Kaitkan dengan Jurusan</label>
                            <select id="jurusan_id" class="form-select input-custom" required>
                                <option value="" disabled selected>Memuat daftar jurusan...</option>
                            </select>
                        </div>
                        <div class="col-md-12 form-group-custom">
                            <label class="fw-bold small text-muted">Nama Profesi</label>
                            <input type="text" id="nama_profesi" class="form-control input-custom" placeholder="Contoh: Arsitek Profesional" required>
                        </div>
                        <div class="col-md-12 form-group-custom">
                            <label class="fw-bold small text-muted">Range Gaji (Opsional)</label>
                            <input type="text" id="range_gaji" class="form-control input-custom" placeholder="Contoh: Rp 6.000.000 - Rp 15.000.000">
                        </div>
                        <div class="col-md-12 form-group-custom">
                            <label class="fw-bold small text-muted">Deskripsi Pekerjaan (Opsional)</label>
                            <textarea id="deskripsi_pekerjaan" class="form-control input-custom" rows="3" placeholder="Jelaskan peran pekerjaan ini secara singkat..."></textarea>
                        </div>
                    </div>
                    <div class="mt-5 d-flex gap-2 justify-content-end">
                        <button type="submit" id="btnSave" class="btn text-white px-5 shadow-sm" style="background-color: var(--primary-green); border-radius: 10px;">Simpan Data</button>
                        <a href="{{ route('admin.prospek-kerja') }}" class="btn btn-light px-4" style="border-radius: 10px;">Batal</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    async function loadJurusan() {
        try {
            const response = await fetch('/api/admin/jurusan', {
                headers: { 'Authorization': 'Bearer ' + localStorage.getItem('access_token') }
            });
            const result = await response.json();
            const select = document.getElementById('jurusan_id');
            select.innerHTML = '<option value="" disabled selected>Pilih Jurusan Terkait...</option>';
            
            result.data.forEach(item => {
                select.innerHTML += `<option value="${item.id}">${item.nama_jurusan}</option>`;
            });
        } catch (error) {
            document.getElementById('jurusan_id').innerHTML = '<option disabled>Gagal memuat jurusan</option>';
        }
    }

    document.getElementById('formCreateProspek').addEventListener('submit', async (e) => {
        e.preventDefault();
        const btn = document.getElementById('btnSave');
        btn.disabled = true;

        const payload = {
            jurusan_id: document.getElementById('jurusan_id').value,
            nama_profesi: document.getElementById('nama_profesi').value,
            range_gaji: document.getElementById('range_gaji').value,
            deskripsi_pekerjaan: document.getElementById('deskripsi_pekerjaan').value
        };

        try {
            const response = await fetch('/api/admin/prospek-kerja', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + localStorage.getItem('access_token'),
                    'Accept': 'application/json'
                },
                body: JSON.stringify(payload)
            });

            if (response.ok) {
                Swal.fire({ icon: 'success', title: 'Berhasil!', confirmButtonColor: '#2D6A4F' })
                .then(() => { window.location.href = "{{ route('admin.prospek-kerja') }}"; });
            } else {
                Swal.fire('Error!', 'Periksa kembali isian Anda.', 'error');
            }
        } catch (error) { Swal.fire('Error!', 'Gagal menghubungi server.', 'error'); } 
        finally { btn.disabled = false; }
    });

    document.addEventListener('DOMContentLoaded', loadJurusan);
</script>
@endsection