<?php $__env->startSection('title', 'Detail Pesanan #' . $pesanan->id); ?>

<?php $__env->startSection('content'); ?>

<?php
    $statusLevel = 0;
    if ($pesanan->status == 'pending') $statusLevel = 1;
    elseif ($pesanan->status == 'diproses') $statusLevel = 3; 
    elseif ($pesanan->status == 'dikirim') $statusLevel = 4;
    elseif ($pesanan->status == 'selesai') $statusLevel = 5;
    
    $isCanceled = ($pesanan->status == 'dibatalkan');

    function isStepActive($step, $level, $canceled) {
        if ($canceled) return false;
        return $level >= $step;
    }
?>

<style>
/* Custom Radio Styles */
.custom-radio input[type="radio"]:checked + div {
    border-color: #22c55e !important;
    background-color: #f0fdf4 !important;
}
.custom-radio input[type="radio"]:checked + div .radio-inner {
    background-color: #22c55e !important;
    border-color: #22c55e !important;
}
.custom-radio input[type="radio"]:checked + div .radio-inner::after {
    content: '';
    display: block;
    width: 6px;
    height: 6px;
    background: white;
    border-radius: 50%;
    margin: 3px auto;
}
</style>

<div class="max-w-6xl mx-auto py-2 text-sm font-sans mb-10">

    <!-- Header -->
    <div class="flex justify-between items-center mb-6">
        <div class="flex items-center gap-3">
            <a href="<?php echo e(route('pesanans.index')); ?>" class="text-gray-400 hover:text-gray-700 transition">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
            </a>
            <h1 class="text-xl font-semibold text-gray-800">Detail Pesanan #ORD-<?php echo e(date('Ymd', strtotime($pesanan->tanggal_pesanan))); ?>-<?php echo e(strtoupper(\Illuminate\Support\Str::random(4))); ?><?php echo e($pesanan->id); ?></h1>
        </div>
        <div class="flex items-center gap-3">
            <span class="px-3 py-1 bg-green-100 text-green-700 text-[11px] font-bold rounded uppercase tracking-wider">
                <?php echo e($pesanan->status); ?>

            </span>
            <a href="<?php echo e(route('pesanans.invoice', $pesanan->id)); ?>" target="_blank" class="bg-white border border-gray-300 text-gray-700 hover:bg-gray-50 px-4 py-1.5 rounded-full text-[11px] font-bold tracking-wider transition inline-flex items-center gap-2">
                <i class="bi bi-file-earmark-pdf text-red-500"></i> CETAK INVOICE
            </a>
            <a href="<?php echo e(route('pesanans.cetak-resi', $pesanan->id)); ?>" target="_blank" class="bg-black hover:bg-gray-800 text-white px-4 py-1.5 rounded-full text-[11px] font-bold tracking-wider transition inline-block text-center">
                CETAK RESI
            </a>
        </div>
    </div>

    <!-- Stepper -->
    <div class="bg-white rounded-xl border border-gray-200 p-8 mb-6 shadow-sm">
        <div class="flex items-center justify-between relative px-6">
            <!-- Line Behind -->
            <div class="absolute left-0 top-1/2 transform -translate-y-1/2 w-full h-[2px] bg-gray-200 z-0"></div>
            
            <!-- Step 1 -->
            <div class="relative z-10 flex flex-col items-center gap-2 bg-white px-4">
                <div class="w-8 h-8 rounded-full border-[3px] flex items-center justify-center <?php echo e(isStepActive(1, $statusLevel, $isCanceled) ? 'bg-green-600 border-green-600 text-white' : 'bg-white border-gray-300 text-gray-300'); ?>">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path></svg>
                </div>
                <span class="text-[10px] font-bold tracking-wider <?php echo e(isStepActive(1, $statusLevel, $isCanceled) ? 'text-green-600' : 'text-gray-400'); ?>">MENUNGGU</span>
            </div>

            <!-- Step 2 -->
            <div class="relative z-10 flex flex-col items-center gap-2 bg-white px-4">
                <div class="w-8 h-8 rounded-full border-[3px] flex items-center justify-center <?php echo e(isStepActive(2, $statusLevel, $isCanceled) ? 'bg-green-600 border-green-600 text-white' : 'bg-white border-gray-300 text-gray-300'); ?>">
                    <?php if(isStepActive(2, $statusLevel, $isCanceled)): ?>
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path></svg>
                    <?php endif; ?>
                </div>
                <span class="text-[10px] font-bold tracking-wider <?php echo e(isStepActive(2, $statusLevel, $isCanceled) ? 'text-green-600' : 'text-gray-400'); ?>">DIBAYAR</span>
            </div>

            <!-- Step 3 -->
            <div class="relative z-10 flex flex-col items-center gap-2 bg-white px-4">
                <div class="w-8 h-8 rounded-full border-[3px] flex items-center justify-center <?php echo e(isStepActive(3, $statusLevel, $isCanceled) ? 'bg-green-600 border-green-600 text-white' : 'bg-white border-gray-300 text-gray-300'); ?>">
                    <?php if(isStepActive(3, $statusLevel, $isCanceled)): ?>
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path></svg>
                    <?php endif; ?>
                </div>
                <span class="text-[10px] font-bold tracking-wider <?php echo e(isStepActive(3, $statusLevel, $isCanceled) ? 'text-green-600' : 'text-gray-400'); ?>">DIPROSES</span>
            </div>

            <!-- Step 4 -->
            <div class="relative z-10 flex flex-col items-center gap-2 bg-white px-4">
                <div class="w-8 h-8 rounded-full border-[3px] flex items-center justify-center <?php echo e(isStepActive(4, $statusLevel, $isCanceled) ? 'bg-green-600 border-green-600 text-white' : 'bg-white border-gray-300 text-gray-300'); ?>">
                    <?php if(isStepActive(4, $statusLevel, $isCanceled)): ?>
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path></svg>
                    <?php endif; ?>
                </div>
                <span class="text-[10px] font-bold tracking-wider <?php echo e(isStepActive(4, $statusLevel, $isCanceled) ? 'text-green-600' : 'text-gray-400'); ?>">DIKIRIM</span>
            </div>

            <!-- Step 5 -->
            <div class="relative z-10 flex flex-col items-center gap-2 bg-white px-4">
                <div class="w-8 h-8 rounded-full border-[3px] flex items-center justify-center <?php echo e(isStepActive(5, $statusLevel, $isCanceled) ? 'bg-green-600 border-green-600 text-white' : 'bg-white border-gray-300 text-gray-300'); ?>">
                    <?php if(isStepActive(5, $statusLevel, $isCanceled)): ?>
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path></svg>
                    <?php endif; ?>
                </div>
                <span class="text-[10px] font-bold tracking-wider <?php echo e(isStepActive(5, $statusLevel, $isCanceled) ? 'text-green-600' : 'text-gray-400'); ?>">SELESAI</span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        
        <!-- Kolom Kiri -->
        <div class="lg:col-span-2 space-y-6">
            
            <!-- Item Pesanan -->
            <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                <h3 class="text-[11px] font-bold text-gray-800 tracking-wider mb-4">ITEM PESANAN</h3>
                
                <table class="w-full text-left mb-2">
                    <thead>
                        <tr class="border-y border-gray-100 text-[10px] text-gray-500 tracking-wider">
                            <th class="py-3 font-semibold w-7/10">PRODUK</th>
                            <th class="py-3 font-semibold text-center w-1/10">QTY</th>
                            <th class="py-3 font-semibold text-right w-1/10">HARGA</th>
                            <th class="py-3 font-semibold text-right w-1/10">TOTAL</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-50 border-b border-gray-100">
                        <?php $__currentLoopData = $pesanan->detailPesanans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>
                            <td class="py-4">
                                <div class="flex items-center gap-4">
                                    <div class="w-12 h-12 bg-gray-100 rounded-lg overflow-hidden border border-gray-200 flex-shrink-0">
                                        <?php if($item->produk && $item->produk->foto): ?>
                                            <img src="<?php echo e(asset('storage/'.$item->produk->foto)); ?>" class="w-full h-full object-cover">
                                        <?php endif; ?>
                                    </div>
                                    <span class="font-medium text-gray-800 text-[13px] leading-tight"><?php echo e($item->produk->nama ?? 'Produk'); ?></span>
                                </div>
                            </td>
                            <td class="py-4 text-center text-blue-600 font-semibold text-xs">× <?php echo e($item->jumlah); ?></td>
                            <td class="py-4 text-right text-gray-600 text-xs font-medium w-max whitespace-nowrap">Rp <?php echo e(number_format($item->harga, 0, ',', '.')); ?></td>
                            <td class="py-4 text-right text-gray-800 font-bold text-xs whitespace-nowrap">Rp <?php echo e(number_format($item->subtotal, 0, ',', '.')); ?></td>
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table>

                <div class="flex flex-col items-end gap-3 text-xs text-gray-600 pt-4">
                    <div class="w-64 flex justify-between">
                        <span>Subtotal</span>
                        <span class="font-medium text-gray-800">Rp <?php echo e(number_format($pesanan->total_harga, 0, ',', '.')); ?></span>
                    </div>
                    <div class="w-64 flex justify-between">
                        <span>Ongkir <span class="text-[10px] text-gray-400">(Estimasi)</span></span>
                        <span class="font-medium text-gray-800">Rp 0</span>
                    </div>
                    <div class="w-64 flex justify-between font-bold text-gray-900 mt-2 pt-3 border-t border-gray-100 text-sm">
                        <span>Total</span>
                        <span>Rp <?php echo e(number_format($pesanan->total_harga, 0, ',', '.')); ?></span>
                    </div>
                </div>
            </div>

            <!-- Informasi Pengiriman -->
            <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                <h3 class="text-[11px] font-bold text-gray-800 tracking-wider mb-5">INFORMASI PENGIRIMAN</h3>
                
                <div class="grid grid-cols-2 gap-6 mb-6">
                    <div>
                        <p class="text-[10px] font-bold text-gray-400 mb-1">PENERIMA</p>
                        <p class="font-semibold text-gray-800"><?php echo e($pesanan->user->nama ?? '-'); ?></p>
                        <p class="text-xs text-gray-500 mt-0.5"><?php echo e($pesanan->user->no_telp ?? '-'); ?></p>
                    </div>
                    <div>
                        <p class="text-[10px] font-bold text-gray-400 mb-1">PEMBELI</p>
                        <p class="font-semibold text-gray-800"><?php echo e($pesanan->user->nama ?? '-'); ?></p>
                        <p class="text-xs text-gray-500 mt-0.5"><?php echo e($pesanan->user->email ?? '-'); ?></p>
                    </div>
                </div>

                <div>
                    <p class="text-[10px] font-bold text-gray-400 mb-1">ALAMAT LENGKAP</p>
                    <p class="text-xs text-gray-700 leading-relaxed uppercase">
                        <?php echo e($pesanan->alamat_tujuan); ?><br>
                        KEL. <?php echo e($pesanan->desa->name ?? '-'); ?>, KEC. <?php echo e($pesanan->kecamatan->name ?? '-'); ?>, <?php echo e($pesanan->regency->name ?? $pesanan->kota); ?><br>
                        <?php echo e($pesanan->provinsi->name ?? '-'); ?> <?php echo e($pesanan->kode_pos); ?>

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

            <!-- Nomor Resi -->
            <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm flex items-center justify-between mb-6">
                <div>
                    <p class="text-[10px] font-bold text-gray-400 mb-1 tracking-wider uppercase">Nomor Resi</p>
                    <p class="font-bold text-gray-900 tracking-wider text-[15px]"><?php echo e($pesanan->no_resi ?? 'BELUM ADA RESI'); ?></p>
                    <?php if($pesanan->status == 'dikirim' || $pesanan->status == 'selesai'): ?>
                        <p class="text-[10px] text-gray-400 mt-1 flex items-center gap-1">
                            Diupdate: <?php echo e(\Carbon\Carbon::parse($pesanan->updated_at)->format('d M Y, H:i')); ?>

                        </p>
                    <?php endif; ?>
                </div>
                <?php if($pesanan->no_resi): ?>
                <button class="text-green-600 text-[11px] font-bold tracking-wider hover:text-green-700 uppercase">Salin</button>
                <?php endif; ?>
            </div>

            <!-- Bukti Pengiriman -->
            <?php if($pesanan->foto_bukti_pengiriman): ?>
            <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                <h3 class="text-[11px] font-bold text-gray-800 tracking-wider mb-4">FOTO BUKTI PENGIRIMAN</h3>
                <div class="w-full h-64 bg-gray-50 rounded-lg overflow-hidden border border-gray-200 flex items-center justify-center">
                    <a href="<?php echo e(asset('storage/' . $pesanan->foto_bukti_pengiriman)); ?>" target="_blank">
                        <img src="<?php echo e(asset('storage/' . $pesanan->foto_bukti_pengiriman)); ?>" alt="Bukti Pengiriman" class="max-w-full max-h-full object-contain hover:scale-105 transition-transform duration-300">
                    </a>
                </div>
                <p class="text-center mt-3 text-xs text-gray-500">Klik gambar untuk memperbesar</p>
            </div>
            <?php endif; ?>

        </div>

        <!-- Kolom Kanan -->
        <div class="space-y-6">
            
            <!-- Info Pesanan -->
            <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                <h3 class="text-[11px] font-bold text-gray-800 tracking-wider mb-5">INFO PESANAN</h3>
                
                <div class="space-y-4">
                    <div class="flex justify-between items-center text-xs">
                        <span class="font-semibold text-gray-500">TANGGAL</span>
                        <span class="text-gray-800 font-medium"><?php echo e(\Carbon\Carbon::parse($pesanan->tanggal_pesanan)->format('d M Y, H:i')); ?></span>
                    </div>
                    <div class="flex justify-between items-center text-xs border-y border-gray-100 py-3">
                        <span class="font-semibold text-gray-500">PEMBAYARAN</span>
                        <span class="text-gray-800 font-bold uppercase"><?php echo e($pesanan->metode_pembayaran ?? '-'); ?></span>
                    </div>
                    <div class="flex justify-between items-center text-xs">
                        <span class="font-semibold text-gray-500">STATUS BAYAR</span>
                        <span class="text-green-600 font-bold tracking-wider">
                            <?php echo e(($pesanan->pembayaran && $pesanan->pembayaran->transaction_status == 'berhasil') || in_array($pesanan->status, ['diproses','dikirim','selesai']) ? 'LUNAS' : 'PENDING'); ?>

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

            <!-- Unified Form: Update Status & Penugasan Kurir -->
            <form action="<?php echo e(route('pesanans.update', $pesanan->id)); ?>" method="POST" class="space-y-6">
                <?php echo csrf_field(); ?>
                <?php echo method_field('PUT'); ?>

                <!-- Update Status Box -->
                <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                    <h3 class="text-[11px] font-bold text-gray-800 tracking-wider mb-4">UPDATE STATUS</h3>
                    
                    <div class="space-y-2">
                        <?php
                            $statuses = [
                                'menunggu' => ['label' => 'Menunggu Bayar', 'level' => 1],
                                'diproses' => ['label' => 'Sedang Dikemas', 'level' => 3],
                                'dikirim' => ['label' => 'Dalam Perjalanan', 'level' => 4],
                                'selesai' => ['label' => 'Pesanan Diterima', 'level' => 5],
                                'dibatalkan' => ['label' => 'Dibatalkan', 'level' => 99],
                                'bermasalah' => ['label' => 'Kendala Pengiriman', 'level' => 99]
                            ];
                        ?>
                        
                        <?php $__currentLoopData = $statuses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $isAdmin = auth()->user()->role === 'admin';
                            $isDisabled = ($data['level'] < $statusLevel) || 
                                          ($pesanan->status == 'selesai' && $key != 'selesai') ||
                                          ($pesanan->status == 'dibatalkan' && $key != 'dibatalkan') ||
                                          ($pesanan->status == 'bermasalah' && $key != 'dibatalkan' && $key != 'bermasalah' && $key != 'selesai') ||
                                          ($isAdmin && in_array($key, ['dikirim', 'selesai']));
                        ?>
                        <label class="block relative <?php echo e($isDisabled && $pesanan->status != $key ? 'cursor-not-allowed opacity-60' : 'cursor-pointer'); ?> custom-radio">
                            <input type="radio" name="status" value="<?php echo e($key); ?>" class="sr-only" <?php echo e($pesanan->status == $key ? 'checked' : ''); ?> <?php echo e($isDisabled && $pesanan->status != $key ? 'disabled' : ''); ?>>
                            <div class="flex items-center p-3 border rounded-lg <?php echo e($isDisabled && $pesanan->status != $key ? 'bg-gray-50 border-gray-100' : 'hover:bg-gray-50 border-gray-200'); ?> transition">
                                <div class="w-[14px] h-[14px] rounded-full border-[1.5px] <?php echo e($isDisabled && $pesanan->status != $key ? 'border-gray-200' : 'border-gray-300'); ?> mr-3 flex-shrink-0 relative radio-inner flex items-center justify-center transition-colors"></div>
                                <span class="text-xs <?php echo e($isDisabled && $pesanan->status != $key ? 'text-gray-400' : 'text-gray-500'); ?>">
                                    <span class="font-medium <?php echo e($isDisabled && $pesanan->status != $key ? 'text-gray-400' : 'text-gray-800'); ?>"><?php echo e(ucfirst($key)); ?></span> &mdash; <?php echo e($data['label']); ?>

                                </span>
                            </div>
                        </label>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </div>
                </div>

                <!-- Penugasan Kurir Box -->
                <div class="bg-white rounded-xl border border-gray-200 p-6 shadow-sm">
                    <h3 class="text-[11px] font-bold text-gray-800 tracking-wider mb-4">PENUGASAN KURIR</h3>
                    
                    <?php if(in_array($pesanan->status, ['diproses', 'dikirim'])): ?>
                    <div class="space-y-3">
                        <select name="kurir_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-xs focus:ring-2 focus:ring-green-500 outline-none">
                            <option value="">-- Tugaskan Kurir --</option>
                            <?php $__currentLoopData = $kurirs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $kurir): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($kurir->id); ?>" <?php echo e($pesanan->kurir_id == $kurir->id ? 'selected' : ''); ?>>
                                    <?php echo e($kurir->nama ?? $kurir->name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <?php elseif($pesanan->status == 'selesai'): ?>
                    <div class="bg-green-50 border border-green-100 rounded-lg p-4 flex flex-col items-center justify-center text-center">
                        <p class="text-[10px] text-green-600 font-bold mb-1">DIKIRIM OLEH</p>
                        <p class="text-[11px] text-green-700 font-medium">
                            <?php echo e($kurirs->firstWhere('id', $pesanan->kurir_id)->nama ?? ($kurirs->firstWhere('id', $pesanan->kurir_id)->name ?? 'Sistem / Kurir Tidak Diketahui')); ?>

                        </p>
                    </div>
                    <?php else: ?>
                    <div class="bg-gray-50 border border-gray-100 rounded-lg p-5 flex items-center justify-center text-center">
                        <p class="text-[10px] text-gray-500 font-medium">Ubah status ke <strong class="text-gray-700">Diproses</strong> atau <strong class="text-gray-700">Dikirim</strong> terlebih dahulu</p>
                    </div>
                    <?php endif; ?>
                </div>

                <!-- Single Submit Button -->
                <button type="submit" class="w-full bg-black hover:bg-gray-800 text-white font-bold py-3.5 rounded-xl text-[11px] tracking-wider transition uppercase shadow-md shadow-gray-200/50">
                    <span class="flex items-center justify-center gap-2">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7H5a2 2 0 00-2 2v9a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-3m-1 4l-3 3m0 0l-3-3m3 3V4"></path></svg>
                        SIMPAN PERUBAHAN
                    </span>
                </button>
            </form>

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

<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\Toko-miftah\resources\views/pesanans/show.blade.php ENDPATH**/ ?>