@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 Data Alumni</h4>
            <p class="text-muted small">Input data historis untuk memperkuat akurasi algoritma Random Forest.</p>
        </div>

        <div class="card border-0 shadow-sm rounded-4">
            <div class="card-body p-5">
                <form id="formCreateAlumni">
                    <div class="row g-4">
                        <div class="col-md-12 form-group-custom">
                            <label>Nama Lengkap Alumni</label>
                            <input type="text" id="nama_alumni" class="form-control input-custom" placeholder="Contoh: Ahmad Fauzi" required>
                        </div>
                        <div class="col-md-12 form-group-custom">
                            <label>Perguruan Tinggi</label>
                            <input type="text" id="perguruan_tinggi" class="form-control input-custom" placeholder="Contoh: Universitas Indonesia" required>
                        </div>
                        <div class="col-md-12 form-group-custom">
                            <label>Jurusan Kuliah yang Diambil</label>
                            <input type="text" id="jurusan_diambil" class="form-control input-custom" placeholder="Contoh: Teknik Informatika" required>
                        </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 Alumni</button>
                        <a href="{{ route('admin.alumni') }}" class="btn btn-light px-4" style="border-radius: 10px;">Batal</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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

        const payload = {
            nama_alumni: document.getElementById('nama_alumni').value,
            perguruan_tinggi: document.getElementById('perguruan_tinggi').value,
            jurusan_diambil: document.getElementById('jurusan_diambil').value
        };

        try {
            const response = await fetch('/api/admin/alumni', {
                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.alumni') }}"; });
            }
        } catch (error) { Swal.fire('Error!', 'Gagal menghubungi server.', 'error'); } 
        finally { btn.disabled = false; }
    });
</script>
@endsection