<x-app-layout>
    @section('page-title', $pageTitle)
    <div class="d-flex align-items-center mb-4">
        <a href="{{ route('admin.pengumuman.index') }}" class="btn btn-outline-secondary me-3">
            <i class="bi bi-arrow-left"></i>
        </a>
        <h3 class="my-0 fw-bold">Formulir Edit Pengumuman</h3>
    </div>

    <div class="row justify-content-center">
        <div class="col-md-10">
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">
                    <div class="card-body">
                        <form action="{{ route('admin.pengumuman.update', $pengumuman->id) }}" method="POST">
                            @csrf
                            @method('PUT')
                            <div class="mb-3">
                                <label for="title" class="form-label">Judul Pengumuman</label>
                                <input type="text" name="title" class="form-control" id="title"
                                    value="{{ old('title', $pengumuman->title) }}" required>
                            </div>
                            <div class="mb-3">
                                <label for="type" class="form-label">Tipe Pengumuman</label>
                                <select name="type" class="form-select" id="type" required>
                                    <option value="">Pilih tipe...</option>
                                    <option value="info" @if (old('type', $pengumuman->type) == 'info') selected @endif>Info
                                    </option>
                                    <option value="success" @if (old('type', $pengumuman->type) == 'success') selected @endif>Success
                                    </option>
                                    <option value="warning" @if (old('type', $pengumuman->type) == 'warning') selected @endif>Warning
                                    </option>
                                    <option value="danger" @if (old('type', $pengumuman->type) == 'danger') selected @endif>Danger
                                    </option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label for="content" class="form-label">Isi Pengumuman</label>
                                <textarea name="content" class="form-control" id="content" rows="5" required>{{ old('content', $pengumuman->content) }}</textarea>
                            </div>
                            <hr>
                            <div class="d-flex justify-content-end">
                                <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        document.getElementById('formEditPengumuman').addEventListener('submit', function(e) {
            e.preventDefault();

            modernSwal.fire({
                title: 'Menyimpan Perubahan...',
                timer: 800,
                didOpen: () => Swal.showLoading()
            }).then(() => {
                Toast.fire({
                    icon: 'success',
                    title: 'Berhasil',
                    text: 'Pengumuman berhasil diperbarui.'
                });

                // Redirect setelah 1.5 detik
                setTimeout(() => {
                    window.location.href = "{{ route('admin.pengumuman.index') }}";
                }, 1500);
            });
        });
    </script>
    @endpush
</x-app-layout>