@extends('adminlte.layouts.app')

@section('content')
<div class="content-wrapper">

    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-3">
                <div class="col-sm-6">
                    <h1 class="m-0 fw-bold text-gradient-primary">
                        <i class="fas fa-edit me-2"></i>Edit Buku
                    </h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item">
                            <a href="{{ route('buku.index') }}">Buku</a>
                        </li>
                        <li class="breadcrumb-item active">Edit</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">

            <div class="card shadow-xl rounded-3 border-0">
                <div class="card-header bg-gradient-primary text-white py-3 px-4">
                    <h3 class="card-title m-0 fw-semibold">
                        <i class="fas fa-book-open me-1"></i> Form Edit Buku
                    </h3>
                </div>

                <div class="card-body px-4 py-4">

                    {{-- Error Validasi --}}
                    @if ($errors->any())
                        <div class="alert alert-danger shadow-sm">
                            <ul class="mb-0">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form id="formBuku" action="{{ route('buku.update', $buku->id) }}" method="POST">
                        @csrf
                        @method('PUT')

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-semibold">Judul Buku</label>
                                <input type="text" name="judul" class="form-control shadow-sm"
                                    value="{{ old('judul', $buku->judul) }}" required>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-semibold">Pengarang</label>
                                <input type="text" name="pengarang" class="form-control shadow-sm"
                                    value="{{ old('pengarang', $buku->pengarang) }}" required>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-semibold">Penerbit</label>
                                <input type="text" name="penerbit" class="form-control shadow-sm"
                                    value="{{ old('penerbit', $buku->penerbit) }}" required>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-semibold">Tahun Terbit</label>
                                <input type="number" name="tahun_terbit" class="form-control shadow-sm"
                                    value="{{ old('tahun_terbit', $buku->tahun_terbit) }}" required>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-semibold">ISBN</label>
                                <input type="text" name="isbn" class="form-control shadow-sm"
                                    value="{{ old('isbn', $buku->isbn) }}" required>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-semibold">Kategori</label>
                                <select name="kategori" class="form-select shadow-sm" required>
                                    <option value="">-- Pilih Kategori --</option>
                                    <option value="Novel" {{ old('kategori', $buku->kategori) == 'Novel' ? 'selected' : '' }}>Novel</option>
                                    <option value="Cerita" {{ old('kategori', $buku->kategori) == 'Cerita' ? 'selected' : '' }}>Cerita</option>
                                    <option value="Pelajaran" {{ old('kategori', $buku->kategori) == 'Pelajaran' ? 'selected' : '' }}>Pelajaran</option>
                                </select>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-semibold">Jumlah Buku</label>
                                <input type="number" name="jumlah" class="form-control shadow-sm"
                                    value="{{ old('jumlah', $buku->jumlah) }}" required>
                            </div>
                        </div>

                        <div class="d-flex justify-content-between mt-4">
                            <a href="{{ route('buku.index') }}"
                                class="btn btn-secondary shadow-sm hover-scale">
                                <i class="fas fa-arrow-left me-1"></i> Kembali
                            </a>

                            <button type="submit"
                                class="btn btn-primary shadow-sm hover-scale">
                                <i class="fas fa-save me-1"></i> Update
                            </button>
                        </div>
                    </form>

                </div>
            </div>

        </div>
    </section>
</div>

{{-- SWEETALERT --}}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
document.getElementById('formBuku').addEventListener('submit', function (e) {
    e.preventDefault();

    Swal.fire({
        title: 'Update Data Buku?',
        text: 'Perubahan akan disimpan',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Update',
        cancelButtonText: 'Batal'
    }).then((result) => {
        if (result.isConfirmed) {
            this.submit();
        }
    });
});
</script>

<style>
.text-gradient-primary {
    background: linear-gradient(90deg, #6a11cb, #2575fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
.hover-scale {
    transition: 0.2s;
}
.hover-scale:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 8px 20px rgba(0,0,0,0.15);
}
</style>
@endsection
