

<?php $__env->startSection('title', 'Detail Nota Pembelian'); ?>

<?php $__env->startSection('content'); ?>
<div class="max-w-3xl mx-auto space-y-6">

    <?php if(session('success')): ?>
    <div class="bg-emerald-50 border border-emerald-200 text-emerald-800 text-xs p-4 rounded-xl font-bold shadow-sm flex items-center gap-2">
        <span>✨</span>
        <p><?php echo e(session('success')); ?></p>
    </div>
    <?php endif; ?>

    <?php if($transaksi->status == 'ditolak'): ?>
    <div class="bg-red-50 border border-red-200 text-red-800 text-xs p-4 rounded-xl font-medium shadow-sm flex flex-col gap-1">
        <span class="block font-black text-[10px] text-red-700 uppercase tracking-wider">❌ PEMBAYARAN DITOLAK ADMIN / DIBATALKAN:</span>
        <?php
        // Mengambil rekaman histori penolakan terakhir dari relasi logStatuses
        $logTolak = $transaksi->logStatuses ? $transaksi->logStatuses->where('status', 'Transaksi Ditolak')->first() : null;
        ?>
        <p class="italic">
            "<?php echo e($logTolak ? $logTolak->keterangan : 'Mohon maaf, transaksi ini telah dibatalkan atau ditolak oleh sistem admin store Mega Mart.'); ?>"
        </p>
    </div>
    <?php endif; ?>

    <div class="bg-white border border-gray-200 rounded-2xl shadow-sm overflow-hidden">
        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
            <div class="flex items-center space-x-3">
                <a href="<?php echo e(route('lacak.paket')); ?>" class="bg-white hover:bg-gray-100 text-gray-700 border border-gray-300 px-3 py-1.5 rounded-lg text-xs font-bold transition flex items-center gap-1.5 shadow-sm">
                    <i class="fa-solid fa-arrow-left"></i> Kembali
                </a>
                <div>
                    <span class="text-[10px] font-bold text-gray-400 uppercase tracking-wider block">Nomor Invoice</span>
                    <h2 class="font-mono text-base font-black text-gray-900">#TRX-<?php echo e($transaksi->id); ?></h2>
                </div>
            </div>

            <span class="px-3 py-1 text-xs font-black rounded-full uppercase tracking-wider shadow-sm
                <?php echo e($transaksi->status == 'menunggu' ? 'bg-amber-100 text-amber-800 border border-amber-200' : ''); ?>

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

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

                <?php echo e($transaksi->status == 'ditolak' ? 'bg-red-100 text-red-800 border border-red-200' : ''); ?>">
                ● <?php echo e($transaksi->status); ?>

            </span>
        </div>

        <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-8">

            <div class="space-y-4">
                <h3 class="font-bold text-xs text-gray-400 uppercase tracking-wider border-b border-gray-100 pb-2">Rincian Komoditas Sembako</h3>

                <div class="divide-y divide-gray-100 max-h-[180px] overflow-y-auto pr-1">
                    <?php $subtotalBarang = 0; ?>
                    <?php $__currentLoopData = $transaksi->detailTransaksis; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $detail): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php $subtotalBarang += $detail->subtotal; ?>
                    <div class="flex justify-between items-center py-2.5 text-xs font-medium text-gray-700">
                        <div>
                            <h4 class="font-bold text-gray-800"><?php echo e($detail->barang->nama_barang ?? 'Produk Tidak Tersedia'); ?></h4>
                            <p class="text-[10px] text-gray-400 mt-0.5"><?php echo e($detail->qty); ?> pcs x Rp <?php echo e(number_format($detail->barang->harga ?? $detail->subtotal, 0, ',', '.')); ?></p>
                        </div>
                        <span class="font-black text-gray-900">Rp <?php echo e(number_format($detail->subtotal, 0, ',', '.')); ?></span>
                    </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </div>

                <?php
                $nominalDiskonVoucher = $subtotalBarang - $transaksi->total_bayar;
                ?>

                <div class="bg-gray-50 border border-gray-200 rounded-xl p-3.5 space-y-2 text-xs font-medium text-gray-600">
                    <div class="flex justify-between">
                        <span>Total Order (Sebelum Diskon):</span>
                        <span class="text-gray-900 font-bold">Rp <?php echo e(number_format($subtotalBarang, 0, ',', '.')); ?></span>
                    </div>

                    <div class="flex justify-between text-emerald-600">
                        <span>Potongan Voucher CRM:</span>
                        <span class="font-bold">
                            <?php if($nominalDiskonVoucher > 0): ?>
                            - Rp <?php echo e(number_format($nominalDiskonVoucher, 0, ',', '.')); ?>

                            <?php else: ?>
                            Rp 0
                            <?php endif; ?>
                        </span>
                    </div>

                    <div class="border-t border-gray-200 pt-2.5 mt-2.5 flex justify-between items-center text-gray-900 text-sm font-black">
                        <span>Total Akhir Wajib Bayar:</span>
                        <span class="text-blue-600 text-base">Rp <?php echo e(number_format($transaksi->total_bayar, 0, ',', '.')); ?></span>
                    </div>
                </div>
            </div>

            <div class="space-y-4">
                <h3 class="font-bold text-xs text-gray-400 uppercase tracking-wider border-b border-gray-100 pb-2">Destinasi Logistik (Delivery Address)</h3>

                <div class="bg-slate-50 border border-slate-200 rounded-xl p-4 text-xs font-medium text-slate-700 space-y-2.5 shadow-inner">
                    <div class="flex items-start gap-2">
                        <span class="text-slate-400 w-4 text-center flex-shrink-0"><i class="fa-solid fa-user"></i></span>
                        <div>
                            <span class="text-[10px] text-slate-400 uppercase font-bold block tracking-wider">Nama Penerima</span>
                            <span class="font-bold text-slate-900 text-sm"><?php echo e($transaksi->pelanggan->nama); ?></span>
                        </div>
                    </div>

                    <div class="flex items-start gap-2">
                        <span class="text-slate-400 w-4 text-center flex-shrink-0"><i class="fa-solid fa-phone"></i></span>
                        <div>
                            <span class="text-[10px] text-slate-400 uppercase font-bold block tracking-wider">Nomor Handphone Aktif</span>
                            <span class="font-mono font-bold text-slate-800"><?php echo e($transaksi->pelanggan->no_hp); ?></span>
                        </div>
                    </div>

                    <div class="flex items-start gap-2 border-t border-slate-200/60 pt-2 mt-2">
                        <span class="text-slate-400 w-4 text-center flex-shrink-0"><i class="fa-solid fa-location-dot"></i></span>
                        <div>
                            <span class="text-[10px] text-slate-400 uppercase font-bold block tracking-wider">Alamat Lengkap Pengiriman</span>
                            <p class="text-slate-600 leading-relaxed mt-0.5 font-semibold text-[11px]"><?php echo e($transaksi->alamat_pengiriman ?? $transaksi->pelanggan->alamat); ?></p>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <div class="bg-white border border-gray-200 rounded-2xl p-6 shadow-sm space-y-4">
        <h3 class="font-bold text-xs text-gray-400 uppercase tracking-wider border-b border-gray-100 pb-2">Payment Method & Verifikasi Transaksi</h3>

        <?php if(!$transaksi->bukti_transfer && $transaksi->status == 'menunggu'): ?>
        <form action="<?php echo e(route('nota.upload', $transaksi->id)); ?>" method="POST" enctype="multipart/form-data" class="space-y-4">
            <?php echo csrf_field(); ?>
            <div>
                <label class="block text-xs font-bold text-gray-600 uppercase mb-1">Pilih Metode Bank Tujuan Transfer</label>
                <select required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-xs outline-none bg-white font-medium">
                    <option value="mandiri">🏦 Bank Mandiri — 111-2222-333 a/n MEGA MART SEMBAKO</option>
                    <option value="bca">🏦 Bank BCA — 888-4444-555 a/n MEGA MART SEMBAKO</option>
                    <option value="bri">🏦 Bank BRI — 000-7777-888 a/n MEGA MART SEMBAKO</option>
                </select>
            </div>

            <div>
                <label class="block text-xs font-bold text-gray-600 uppercase mb-1">Pilih Dokumen Foto Struk Bukti Bayar Asli</label>
                <input type="file" name="bukti_transfer" required class="w-full text-xs text-gray-500 border border-gray-200 p-2 rounded-lg file:bg-blue-50 file:text-blue-700 file:border-0 file:px-3 file:py-1 file:rounded file:mr-3 file:font-bold">
            </div>

            <button type="submit" class="w-full bg-slate-900 text-white text-xs py-2.5 rounded-lg font-bold hover:bg-slate-800 transition shadow-sm">
                📤 Unggah Struk Pembayaran Sekarang
            </button>
        </form>
        <?php else: ?>
        <div class="grid grid-cols-1 sm:grid-cols-3 gap-6 items-center bg-gray-50 border border-gray-200 p-4 rounded-xl">

            <div class="sm:col-span-1 border border-gray-300 rounded-lg overflow-hidden bg-white shadow-sm flex items-center justify-center p-1">
                <?php if($transaksi->bukti_transfer): ?>
                <img src="<?php echo e(asset('images/' . $transaksi->bukti_transfer)); ?>" alt="Bukti Transfer Pelanggan" class="max-w-full h-32 object-contain rounded">
                <?php else: ?>
                <div class="text-xs text-gray-400 font-bold py-8 text-center">🚫 Tidak Ada Struk</div>
                <?php endif; ?>
            </div>

            <div class="sm:col-span-2 text-xs font-medium text-gray-600 space-y-2">
                <div>
                    <span class="text-[10px] text-gray-400 font-bold uppercase block tracking-wider">Metode Pembayaran terpilih</span>
                    <p class="font-bold text-gray-800 text-xs flex items-center gap-1 mt-0.5">
                        🏦 Transfer Antar Bank (Verifikasi Manual Otomatis)
                    </p>
                </div>
                <?php if($transaksi->bukti_transfer): ?>
                <div>
                    <span class="text-[10px] text-gray-400 font-bold uppercase block tracking-wider">Nama File Bukti Bayar</span>
                    <p class="font-mono text-gray-500 text-[11px] break-all"><?php echo e($transaksi->bukti_transfer); ?></p>
                </div>
                <?php endif; ?>
                <div class="bg-blue-100/60 border border-blue-200 text-blue-800 p-2.5 rounded-lg font-semibold flex items-center gap-1.5 text-[11px]">
                    <span>ℹ️</span>
                    <?php if($transaksi->status == 'menunggu'): ?>
                    <p>Dokumen struk transfer berhasil diunggah. Menunggu verifikasi tim administrasi toko untuk pembaruan status logistik.</p>
                    <?php elseif($transaksi->status == 'proses'): ?>
                    <p class="text-blue-900 font-bold">Pembayaran diverifikasi! Pesanan Anda sedang dipersiapkan dan diserahkan ke kurir pengiriman.</p>
                    <?php elseif($transaksi->status == 'ditolak'): ?>
                    <p class="text-red-900 font-bold">Transaksi Dibatalkan/Ditolak. Tidak dapat memproses atau memperbarui dokumen bukti pembayaran.</p>
                    <?php else: ?>
                    <p class="text-emerald-900 font-bold">Transaksi Selesai. Seluruh administrasi komoditas sembako telah dinyatakan valid.</p>
                    <?php endif; ?>
                </div>
            </div>

        </div>
        <?php endif; ?>
    </div>

    <div class="text-center pt-2">
        <a href="<?php echo e(route('lacak.paket')); ?>" class="text-xs font-bold text-blue-600 hover:underline flex items-center justify-center gap-1">
            &larr; Kembali ke Status Pesanan Saya
        </a>
    </div>

</div>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.frontend', 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/nota_detail.blade.php ENDPATH**/ ?>