@extends('layouts.main')

@section('breadcrumbs')
    {{-- {{ Breadcrumbs::render(request()->route()->getName()) }} --}}
@endsection

@section('content')
    <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
        <form
            action="{{ isset($daftarPustaka->id) ? route('daftar-pustaka.update', $daftarPustaka->id) : route('daftar-pustaka.store') }}"
            method="POST" enctype="multipart/form-data">
            @csrf
            @if (isset($daftarPustaka->id))
                <input type="hidden" name="id" value="{{ $daftarPustaka->id }}">
                @method('PUT')
            @endif

            <div class="card border border-agi-100 pb-2.5">
                <div class="card-header bg-agi-50" id="basic_settings">
                    <h3 class="card-title">
                        {{ isset($daftarPustaka->id) ? 'Edit' : 'Tambah' }} Daftar Pustaka
                    </h3>
                    <div class="flex items-center gap-2">
                        <a href="{{ route('daftar-pustaka.index') }}" class="btn btn-xs btn-info"><i
                                class="ki-filled ki-exit-left"></i> Back</a>
                    </div>
                </div>
                <div class="card-body grid gap-5">
                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Judul
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input class="input  @error('judul') border-danger bg-danger-light @enderror" type="text"
                                name="judul" value="{{ $daftarPustaka->judul ?? old('judul') }}">
                            @error('judul')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>
                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Upload File
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input class="input @error('attachment') border-danger bg-danger-light @enderror" type="file"
                                name="attachment">
                            @error('attachment')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>
                    @if (isset($daftarPustaka->attachment))
                        <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                            <label class="form-label max-w-56">
                                File
                            </label>
                            <div class="flex flex-wrap items-baseline w-full">
                                {{-- ambil nama file pathnya hilangkan --}}
                                <a href="{{ asset($daftarPustaka->attachment) }}" class="badge badge-outline badge-md badge-info">{{ basename($daftarPustaka->attachment) }} &nbsp;
                                   <i class="ki-filled ki-cloud-download"></i>
                                </a>
                            </div>
                        </div>
                    @endif

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Kategori
                        </label>


                        <div class="flex flex-wrap items-baseline w-full">
                            <select class="select tomselect w-full" name="category_id">
                                <option value="">Pilih Kategori</option>
                                @if (isset($categories))
                                    @foreach ($categories as $item)
                                        <option value="{{ $item->id }}"
                                            {{ old('category_id', $daftarPustaka->category_id ?? '') == $item->id ? 'selected' : '' }}>
                                            {{ $item->name }}
                                        </option>
                                    @endforeach
                                @endif
                            </select>
                            @error('category_id')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Deskripsi
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <textarea name="deskripsi" class="textarea" id="" cols="30" rows="10">{{ $daftarPustaka->deskripsi ?? old('deskripsi') }}</textarea>
                            @error('deskripsi')
                                <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>


                    <div class="flex justify-end">
                        <button type="submit" class="btn btn-primary">
                        {{ isset($daftarPustaka->id) ? 'Update' : 'Simpan' }}
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
@endsection
