

<?php $__env->startSection('title', 'Verifikasi Pembayaran Member'); ?>
<?php $__env->startSection('page_heading', 'Detail Examination Struk Transaksi'); ?>

<?php $__env->startSection('content'); ?>
<div class="space-y-6">

    <div class="flex items-center justify-between">
        <a href="<?php echo e(url('/admin/transaksi')); ?>" class="text-xs font-bold text-gray-500 hover:text-gray-900 transition flex items-center gap-1">
            ⬅️ Kembali ke Daftar Riwayat Transaksi
        </a>
        <span class="px-3 py-1 border text-xs font-mono font-bold rounded-lg uppercase shadow-sm
            <?php echo e($transaksi->status == 'menunggu' ? 'bg-amber-50 text-amber-700 border-amber-200' : ''); ?>

            <?php echo e($transaksi->status == 'proses' ? 'bg-blue-50 text-blue-700 border-blue-200' : ''); ?>

            <?php echo e($transaksi->status == 'dikirim' ? 'bg-purple-50 text-purple-700 border-purple-200' : ''); ?>

            <?php echo e($transaksi->status == 'selesai' ? 'bg-emerald-50 text-emerald-700 border-emerald-200' : ''); ?>

            <?php echo e($transaksi->status == 'ditolak' ? 'bg-red-50 text-red-700 border-red-200' : ''); ?>">
            Status Database: <?php echo e($transaksi->status); ?>

        </span>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 items-start">

        <div class="space-y-6">
            <div class="bg-white border border-gray-200 rounded-xl p-6 shadow-sm space-y-3">
                <h3 class="font-bold text-gray-800 text-sm border-b border-gray-100 pb-2">Identitas Pembeli</h3>
                <div class="text-xs grid grid-cols-2 gap-2 font-medium">
                    <span class="text-gray-400">Nama Pelanggan</span>
                    <span class="text-gray-900 text-right font-bold"><?php echo e($transaksi->pelanggan->nama); ?></span>

                    <span class="text-gray-400">ID Transaksi</span>
                    <span class="text-blue-600 text-right font-mono font-bold">#TRX-<?php echo e($transaksi->id); ?></span>

                    <span class="text-gray-400">Waktu Masuk</span>
                    <span class="text-gray-900 text-right font-mono"><?php echo e($transaksi->created_at->format('d M Y H:i')); ?> WIB</span>
                </div>
            </div>

            <div class="bg-white border border-gray-200 rounded-xl shadow-sm overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                    <h3 class="font-bold text-gray-800 text-sm">Rincian Belanjaan</h3>
                </div>
                <table class="w-full text-left text-xs text-gray-600">
                    <thead class="bg-gray-100 text-[10px] text-gray-700 uppercase font-bold border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-2.5">Nama Item</th>
                            <th class="px-6 py-2.5 text-center">Qty</th>
                            <th class="px-6 py-2.5 text-right">Subtotal</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200 font-medium text-gray-900">
                        <?php $subtotalAwalBarang = 0; ?>
                        <?php $__currentLoopData = $transaksi->detailTransaksis; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $detail): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php $subtotalAwalBarang += $detail->subtotal; ?>
                        <tr>
                            <td class="px-6 py-3"><?php echo e($detail->barang->nama_barang ?? 'Produk Terhapus'); ?></td>
                            <td class="px-6 py-3 text-center font-mono"><?php echo e($detail->qty); ?></td>
                            <td class="px-6 py-3 text-right font-bold">Rp <?php echo e(number_format($detail->subtotal, 0, ',', '.')); ?></td>
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        <?php if($transaksi->promo_id): ?>
                        <tr class="bg-emerald-50/60 text-emerald-700 border-t border-gray-100">
                            <td colspan="2" class="px-6 py-2.5 text-xs font-bold">
                                🎟️ Kupon Diskon Digunakan
                            </td>
                            <td class="px-6 py-2.5 text-right text-xs font-black">
                                - Rp <?php echo e(number_format($subtotalAwalBarang - $transaksi->total_bayar, 0, ',', '.')); ?>

                            </td>
                        </tr>
                        <?php endif; ?>

                        <tr class="bg-blue-50/50 font-black text-blue-700 border-t border-blue-200">
                            <td colspan="2" class="px-6 py-3 text-sm">Total Akhir Wajib Bayar</td>
                            <td class="px-6 py-3 text-right text-sm">Rp <?php echo e(number_format($transaksi->total_bayar, 0, ',', '.')); ?></td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="bg-white border border-gray-200 rounded-xl p-6 shadow-sm">

                <?php if(session('success')): ?>
                <div class="mb-4 bg-emerald-50 border border-emerald-200 text-emerald-800 text-[11px] p-3 rounded-lg font-semibold shadow-sm">
                    🎉 <?php echo e(session('success')); ?>

                </div>
                <?php endif; ?>

                <?php if($transaksi->status == 'menunggu'): ?>
                <h3 class="font-bold text-gray-800 text-sm mb-3">Tindakan 1: Validasi Pembayaran</h3>
                <form action="/admin/transaksi/proses/<?php echo e($transaksi->id); ?>" method="POST" class="space-y-4">
                    <?php echo csrf_field(); ?>
                    <div>
                        <label class="block text-xs font-bold text-gray-700 mb-1">Ambil Keputusan Struk</label>
                        <select name="tindakan" id="tindakan-select" onchange="toggleKeterangan()" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-xs bg-white font-medium focus:ring-2 focus:ring-blue-500 outline-none">
                            <option value="proses">✅ Terima Pembayaran (Lanjut Kirim)</option>
                            <option value="ditolak">❌ Tolak Pembayaran (Struk Palsu/Salah)</option>
                        </select>
                    </div>
                    <div id="box-keterangan" class="hidden">
                        <label class="block text-xs font-bold text-gray-700 mb-1">Alasan Penolakan</label>
                        <textarea name="keterangan" id="keterangan-text" rows="3" placeholder="Contoh: Gambar blur, nominal transfer tidak sesuai..." class="w-full px-3 py-2 border border-gray-300 rounded-lg text-xs outline-none focus:ring-2 focus:ring-blue-500"></textarea>
                    </div>
                    <button type="submit" class="w-full bg-slate-900 text-white font-black text-xs py-2.5 rounded-lg hover:bg-slate-800 transition shadow-md">
                        🚀 Simpan & Update Status CRM
                    </button>
                </form>

                <?php elseif($transaksi->status == 'proses'): ?>
                <?php
                // DETEKSI LOGISTIK: Ambil log status pengiriman jika sudah pernah di-submit admin
                $sudahDikirim = $transaksi->logStatuses->where('status', 'Barang Sedang Dikirim')->first();
                ?>

                <?php if(!$sudahDikirim): ?>
                <h3 class="font-bold text-gray-800 text-sm mb-1 text-emerald-600">Lunas — Siap Dikirim</h3>
                <p class="text-[10px] text-gray-400 mb-4 font-medium">Pembayaran aman. Silakan serahkan sembako ke kurir toko lalu aktifkan pelacakan di bawah ini.</p>

                <form action="/admin/transaksi/proses/<?php echo e($transaksi->id); ?>" method="POST" class="space-y-4">
                    <?php echo csrf_field(); ?>
                    <div>
                        <label class="block text-xs font-bold text-gray-700 mb-1">Informasi Pengiriman / Catatan Lacak Kurir</label>
                        <textarea name="keterangan" rows="3" placeholder="Contoh: Paket sedang dibawa oleh Kurir Budi (0812xxxx). Estimasi tiba 30 menit." required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-xs outline-none focus:ring-2 focus:ring-blue-500 font-medium"></textarea>
                    </div>

                    <button type="submit" name="tindakan" value="dikirim" class="w-full bg-blue-600 text-white font-black text-xs py-2.5 rounded-lg hover:bg-blue-700 transition shadow-md uppercase tracking-wider">
                        🚚 Lepas Paket & Kabari Pelanggan
                    </button>
                </form>
                <?php else: ?>
                <h3 class="font-bold text-gray-800 text-sm mb-2 text-indigo-600">📦 Paket Dalam Pengiriman Logistik</h3>
                <div class="bg-slate-50 border border-gray-100 rounded-lg p-4 space-y-2">
                    <div class="flex items-center gap-2">
                        <span class="w-2 h-2 rounded-full bg-indigo-600 animate-pulse"></span>
                        <span class="text-xs font-black text-gray-800 uppercase">Status Kurir: Aktif</span>
                    </div>
                    <p class="text-[11px] text-gray-600 font-medium">
                        Catatan Terakhir: <span class="italic text-gray-500">"<?php echo e($sudahDikirim->keterangan); ?>"</span>
                    </p>
                    <div class="border-t border-gray-200 pt-2 mt-2 text-[10px] text-gray-400 font-medium">
                        ℹ️ Seluruh kendali operasional admin pada nota ini telah dikunci. Pelanggan saat ini sedang memantau pergerakan kurir secara real-time dari aplikasi mereka.
                    </div>
                </div>
                <?php endif; ?>

                <?php else: ?>
                <h3 class="font-bold text-gray-800 text-sm mb-2">Histori Alur Logistik CRM</h3>
                <div class="bg-slate-50 border border-gray-100 rounded-lg p-4 space-y-2">
                    <div class="flex items-center gap-2">
                        <span class="w-2 h-2 rounded-full bg-blue-600 animate-pulse"></span>
                        <span class="text-xs font-black text-gray-800 capitalize">Status Paket: <?php echo e($transaksi->status); ?></span>
                    </div>
                    <p class="text-[11px] text-gray-500 italic font-medium">
                        "Seluruh kendali operasional admin pada nota ini telah dikunci resmi oleh sistem CRM."
                    </p>
                </div>
                <?php endif; ?>

            </div>
        </div>

        <div class="bg-white border border-gray-200 rounded-xl p-6 shadow-sm space-y-3">
            <h3 class="font-bold text-gray-800 text-sm border-b border-gray-100 pb-2">Berkas Struk Digital</h3>
            <p class="text-[11px] text-gray-400 font-medium">Periksa keaslian gambar di bawah ini dengan seksama sebelum melakukan konfirmasi.</p>

            <div class="border border-gray-200 rounded-xl overflow-hidden bg-slate-100 flex items-center justify-center p-2 min-h-[300px]">
                <?php if($transaksi->bukti_transfer): ?>
                <img src="<?php echo e(asset('images/' . $transaksi->bukti_transfer)); ?>" alt="Struk Transfer Pelanggan" class="max-w-full h-auto rounded-lg shadow-sm hover:scale-105 transition duration-300">
                <?php else: ?>
                <div class="text-center p-8 text-gray-400 font-medium text-xs">
                    📷 Pelanggan belum mengunggah gambar struk pembayaran.
                </div>
                <?php endif; ?>
            </div>
        </div>

    </div>
</div>

<script>
    function toggleKeterangan() {
        const select = document.getElementById('tindakan-select');
        const box = document.getElementById('box-keterangan');
        const input = document.getElementById('keterangan-text');

        if (select.value === 'ditolak') {
            box.classList.remove('hidden');
            input.setAttribute('required', 'required');
        } else {
            box.classList.add('hidden');
            input.removeAttribute('required');
        }
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.backend', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH D:\SKRIPSI CRM 2026 II\PROGRAM CRM MEGA MART\megamart-crm\resources\views/admin/verifikasi_bayar.blade.php ENDPATH**/ ?>