<div class="modal fade text-left" id="foto{{ $p->id_booking }}" 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 Foto</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">Tanggal Booking</span>
                                <span class="detail-value">
                                    {{ \Carbon\Carbon::parse($p->tgl_booking)->locale('id')->translatedFormat('d F Y') }}</span>
                            </div>

                            <div class="detail-row">
                                <span class="detail-label">Waktu Booking</span>
                                <span class="detail-value"> {{ \Carbon\Carbon::parse($p->jam_mulai)->format('H:i') }} -
                                    {{ \Carbon\Carbon::parse($p->jam_selesai)->format('H:i') }}</span>
                            </div>

                            <div class="detail-row">
                                <span class="detail-label">Paket Foto</span>
                                <span class="detail-value">{{ $p->paketFoto->nama }}</span>
                            </div>

                            <div class="detail-row">
                                <span class="detail-label">Additional</span>
                                <div class="detail-items-container d-flex flex-column gap-1">
                                    @forelse($p->detailAdditional as $detail)
                                        <div class="detail-value">
                                            <span class="item" style="color: black;">
                                                {{ $detail->additional->nama }}
                                            </span>
                                            <br>
                                            <small class="amount">x{{ $detail->qty }}</small>
                                        </div>
                                    @empty
                                        <span class="detail-value">-</span>
                                    @endforelse
                                </div>
                            </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">{{ $p->pelanggan->nama }}</span>
                            </div>

                            <div class="detail-row">
                                <span class="detail-label">WhatsApp</span>
                                <span class="detail-value">{{ $p->pelanggan->no_wa }}</span>
                            </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">{{ $p->no_invoice }}</span>
                            </div>
                            <div class="detail-row align-items-center">
                                <span class="detail-label">Status</span><span
                                    class="badge {{ $p->status_label->class }}">
                                    {{ $p->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($p->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 ($p->bukti_bayar)
                                        {{-- Klik hanya pada gambar --}}
                                        <img src="{{ asset($p->bukti_bayar) }}" class="proof-img"
                                            style="cursor: pointer;"
                                            onclick="showImage('{{ asset($p->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 class="modal-footer border-top-0 pt-2 px-2">
                    <div class="d-flex w-100 gap-2">
                        <button type="button" class="btn btn-danger flex-fill tolak"
                            onclick="prosesTanpaDialog(this, 'tolak', '{{ $p->id_booking }}')">
                            Tolak Pesanan
                        </button>

                        <button type="button" class="btn btn-success flex-fill terima"
                            onclick="prosesTanpaDialog(this, 'terima', '{{ $p->id_booking }}')">
                            Terima Pesanan
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{{-- MODAL GAMBAR --}}
<div class="modal fade" id="modalImagePreview" tabindex="-1" aria-hidden="true">
    <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" src="" class="img-fluid rounded shadow-lg"
                    style="max-height: 85vh; object-fit: contain;">
            </div>
        </div>
    </div>
</div>

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

        // 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>
    function prosesTanpaDialog(btn, jenis, id) {
        // 1. Efek Loading pada tombol yang diklik
        const originalText = btn.innerHTML;
        btn.innerHTML = '<span class="spinner-border spinner-border-sm"></span>';
        btn.disabled = true;

        // 2. Eksekusi Fetch
        fetch(`{{ url('/admin/pesanan-foto/update-status') }}/${id}`, {
                method: 'PUT',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'
                },
                body: JSON.stringify({
                    jenis: jenis
                })
            })
            .then(response => {
                if (!response.ok) throw new Error('Gagal menghubungi server');
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    // 3. Langsung buka WhatsApp di tab baru
                    window.open(data.wa_url, '_blank');
                    // 4. Reload halaman utama untuk update status di tabel
                    location.reload();
                } else {
                    alert("Error: " + data.message);
                    btn.innerHTML = originalText;
                    btn.disabled = false;
                }
            })
            .catch(error => {
                console.error(error);
                alert("Terjadi kesalahan sistem. Cek Console.");
                btn.innerHTML = originalText;
                btn.disabled = false;
            });
    }
</script>
