@extends('layouts.main')

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

@section('content')
    <div class="grid gap-5 mx-auto w-full lg:gap-7.5">
        <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="pb-2.5 border card border-agi-100">
                <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 gap-2 items-center">
                        <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="grid gap-5 card-body">
                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <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="text-sm alert text-danger">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>
                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <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="text-sm alert text-danger">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>
                    @if (isset($daftarPustaka->attachment))
                        <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                            <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 flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <label class="form-label max-w-56">
                            Kategori
                        </label>


                        <div class="flex flex-wrap items-baseline w-full">
                            <select class="w-full select tomselect" 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="text-sm alert text-danger">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <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="text-sm alert text-danger">{{ $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
