@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">
            @csrf
            <div class="card border border-agi-100 pb-2.5">
                <div class="card-header bg-agi-50" id="basic_settings">
                    <h3 class="card-title">
                        {{ $daftarPustaka->judul ?? '' }}
                    </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=" min-w-3xl">
                        <div class="p-4 h-full flex flex-col">
                            <div class="flex justify-between items-center mb-4">
                                <a href="{{ asset('storage/' . $daftarPustaka->attachment)}}" class="btn btn-primary btn-sm">
                                    <i class="ki-duotone ki-cloud-download me-1"><span class="path1"></span><span
                                            class="path2"></span></i>
                                    Download File
                                </a>
                            </div>
                            @php

                                $fileExtension = pathinfo($daftarPustaka->attachment, PATHINFO_EXTENSION);
                                // cek extension
                                $isPdf = $fileExtension == 'pdf';
                                $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
                                $isImage = in_array(strtolower($fileExtension), $imageExtensions);
                                $fileUrl = asset('storage/' . $daftarPustaka->attachment);
                            @endphp

                            @if ($isPdf)
                                <iframe src="{{$fileUrl}}"
                                    width="100%" height="600px" frameborder="0"></iframe>
                            @elseif ($isImage)
                                <img src="{{ $fileUrl }}" class="w-full  object-contain rounded" />
                            @else
                                <p class="text-red-500">File tidak bisa ditampilkan, silakan <a href="{{ $fileUrl }}"
                                        class="text-blue-500 underline" download>unduh di sini</a>.</p>
                            @endif


                        </div>
                    </div>

                    <div class="border border-t">
                    </div>
                    <div>
                        {{ $daftarPustaka->deskripsi ?? '' }}
                    </div>
                </div>
            </div>
        </form>
    </div>
@endsection

{{-- @push('scripts')
    <script src="{{ asset('vendor/pdfobject.min.js') }}"></script>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            let url = @json($daftarPustaka->attachment);
            console.log(url);

            const fileExtension = url.split('.').pop().toLowerCase();
            const previewContent = document.getElementById('previewContent');

            if (['pdf'].includes(fileExtension)) {
                if (window.innerWidth < 768) {
                    document.getElementById('pdfFrame').style.display = 'block';
                    document.getElementById('pdfFrame').src = urlStorage;
                } else {
                    PDFObject.embed(urlStorage, "#previewContent");
                }
            } else if (['jpg', 'jpeg', 'png', 'gif'].includes(fileExtension)) {
                previewContent.innerHTML =
                    `<img src="${url}" alt="Preview" class="max-w-full max-h-full object-contain">`;
            } else {
                previewContent.innerHTML = '<p class="text-center">Unsupported file type</p>';
            }
        });
    </script>
@endpush --}}
