{{-- RIWAYAT BUKET --}}
<div class="modal fade text-left" id="riwayatBuket{{ $rb->id_transaksi }}" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title" id="myModalLabel1">Detail Pesanan Buket</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>

            <div class="modal-body">
                <div class="row gx-3">

                    <div class="col-12 col-md-6">

                        <h6 class="modal-section-title">Rincian Pesanan</h6>
                        <div class="detail-order">
                            <div class="detail-row">
                                <span class="detail-label">Nama Buket</span>
                                <span class="detail-value">{{ $rb->buket->nama }}</span>
                            </div>

                            <div class="detail-row">
                                <span class="detail-label">Request Khusus</span>
                                @if ($rb->request && $rb->request != '')
                                    <span class="detail-value">{{ $rb->request }}</span>
                                @else
                                    <span class="detail-value">-</span>
                                @endif
                            </div>

                            <div class="detail-row">
                                <span class="detail-label">Ucapan</span>
                                @if ($rb->ucapan && $rb->ucapan != '')
                                    <span class="detail-value">{{ $rb->ucapan }}</span>
                                @else
                                    <span class="detail-value">-</span>
                                @endif
                            </div>
                        </div>

                        <h6 class="modal-section-title">Informasi Pemesan</h6>
                        <div class="detail-customer">
                            <div class="detail-row">
                                <span class="detail-label">Nama Lengkap</span>
                                <span class="detail-value">{{ $rb->pelanggan->nama }}</span>
                            </div>

                            <div class="detail-row">
                                <span class="detail-label">WhatsApp</span>
                                <span class="detail-value">{{ $rb->pelanggan->no_wa }}</span>
                            </div>
                            <div class="detail-row d-flex align-items-start mb-3">
                                <span class="detail-label" style="min-width: 150px;">Tanggal Pengambilan</span>
                                <div class="detail-content">
                                    <div class="detail-value">
                                        {{-- Menambahkan 'l' untuk nama hari --}}
                                        {{ \Carbon\Carbon::parse($rb->tgl_ambil)->locale('id')->translatedFormat('l, d F Y') }}
                                    </div>
                                    <div class="detail-value">

                                        {{ \Carbon\Carbon::parse($rb->tgl_ambil)->format('H:i') }} WIB
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-12 col-md-6 mt-4 mt-md-0">
                        <h6 class="modal-section-title">Pembayaran</h6>

                        <div class="payment-box">
                            <div class="detail-row align-items-center">
                                <span class="detail-label">No. Invoice</span>
                                <span class="badge bg-success-subtle rounded-pill px-3">{{ $rb->no_invoice }}</span>
                            </div>
                            <div class="detail-row align-items-center">
                                <span class="detail-label">Status</span><span
                                    class="badge {{ $rb->status_label->class }}">
                                    {{ $rb->status_label->text }}
                                </span>

                            </div>

                            <div class="detail-row align-items-center">
                                <span class="detail-label">Total</span>
                                <span class="detail-value">Rp {{ number_format($rb->total_bayar, 0, ',', '.') }}</span>
                            </div>

                            <div class="detail-row align-items-start">
                                <span class="detail-label d-block ">Bukti Pembayaran</span>

                                <div class="proof-img-wrapper">
                                    @if ($rb->bukti_bayar)
                                        {{-- Klik hanya pada gambar --}}
                                        <img src="{{ asset('storage/' . $rb->bukti_bayar) }}" class="proof-img"
                                            style="cursor: pointer;"
                                            onclick="showImageBuket('{{ asset('storage/' . $rb->bukti_bayar) }}')">
                                    @else
                                        {{-- Div pengganti kalau tidak ada foto --}}
                                        <div class="custom-img-box d-flex align-items-center justify-content-center text-muted border rounded"
                                            style="height: 100px; background: #f8f9fa;">
                                            <small>Tidak ada bukti pembayaran</small>
                                        </div>
                                    @endif
                                </div>
                            </div>
                        </div>

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

{{-- MODAL GAMBAR --}}
<div class="modal fade" id="modalImagePreviewBuket" tabindex="-1" aria-hidden="true" style="z-index: 1060 !important;">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content bg-transparent border-0 shadow-none">
            {{-- Tombol close putih agar terlihat di background gelap --}}
            <div class="text-end mb-2">
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"
                    aria-label="Close"></button>
            </div>
            <div class="modal-body p-0 text-center">
                <img id="img-preview-target-buket" src="" class="img-fluid rounded shadow-lg"
                    style="max-height: 85vh; object-fit: contain;">
            </div>
        </div>
    </div>
</div>

<script>
    function showImageBuket(src) {
        const modalElement = document.getElementById('modalImagePreviewBuket');
        const modalImg = document.getElementById('img-preview-target-buket');

        // 1. Set sumber gambar
        modalImg.src = src;

        // 2. Tampilkan modal (Cara Bootstrap 5 yang lebih aman)
        const myModal = new bootstrap.Modal(modalElement);
        myModal.show();
    }
</script>
<script>
    // Tambahkan parameter ke-4: 'kategori'
    function prosesTanpaDialog(btn, jenis, id, kategori) {

        // 1. Efek Loading
        const originalText = btn.innerHTML;
        btn.innerHTML = '<span class="spinner-border spinner-border-sm"></span>';
        btn.disabled = true;

        // 2. Fetch
        fetch(`{{ url('/admin/riwayat-pesanan/update-status') }}/${id}`, {
                method: 'PUT',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'
                },
                // Kirim 'jenis' DAN 'kategori'
                body: JSON.stringify({
                    jenis: jenis,
                    kategori: kategori
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    location.reload();
                } else {
                    alert("Gagal: " + (data.message || "Terjadi kesalahan"));
                    btn.innerHTML = originalText;
                    btn.disabled = false;
                }
            })
            .catch(error => {
                console.error(error);
                alert("Error sistem.");
                btn.innerHTML = originalText;
                btn.disabled = false;
            });
    }
</script>
