<x-app-layout>
    <x-slot name="header">
        <div class="flex items-center justify-between">
            <h2 class="text-2xl font-bold text-stone-800">
                <i class="fas fa-credit-card mr-2 text-amber-600"></i>
                {{ __('Detail Pembayaran') }}
            </h2>
            <div class="flex gap-3">
                <span class="px-4 py-2 bg-amber-50 text-amber-700 rounded-lg text-sm font-medium border border-amber-200">
                    <i class="fas fa-receipt mr-2"></i>
                    Order #{{ $payment->order->order_id }}
                </span>
            </div>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-4xl mx-auto sm:px-6 lg:px-8">

            <!-- Main Card -->
            <div class="bg-white rounded-2xl shadow-lg border border-amber-100 overflow-hidden">

                <!-- Payment Status Header -->
                <div class="p-6 border-b border-amber-100 bg-gradient-to-r from-amber-50 via-orange-50 to-amber-50">
                    <div class="flex flex-col sm:flex-row justify-between items-center gap-4">
                        <div class="flex items-center gap-3">
                            <div class="w-14 h-14 bg-white rounded-xl flex items-center justify-center shadow-md border border-amber-200">
                                @if($payment->order->status == 'process')
                                <i class="fas fa-truck text-2xl text-amber-600"></i>
                                @elseif($payment->order->status == 'paid')
                                <i class="fas fa-check-circle text-2xl text-green-600"></i>
                                @elseif($payment->order->status == 'shipped')
                                <i class="fas fa-shipping-fast text-2xl text-amber-600"></i>
                                @elseif($payment->order->status == 'delivered')
                                <i class="fas fa-home text-2xl text-green-600"></i>
                                @elseif($payment->order->status == 'completed')
                                <i class="fas fa-check-double text-2xl text-green-600"></i>
                                @elseif($payment->order->status == 'cancelled')
                                <i class="fas fa-times-circle text-2xl text-red-500"></i>
                                @else
                                <i class="fas fa-hourglass-half text-2xl text-amber-500"></i>
                                @endif
                            </div>
                            <div>
                                @php
                                $statusText = [
                                'process' => 'Pesanan Diproses',
                                'paid' => 'Pembayaran Berhasil',
                                'shipped' => 'Pesanan Dikirim',
                                'delivered' => 'Pesanan Diterima',
                                'completed' => 'Pesanan Selesai',
                                'cancelled' => 'Pesanan Dibatalkan'
                                ];
                                $statusColor = [
                                'process' => 'text-amber-700',
                                'paid' => 'text-green-700',
                                'shipped' => 'text-amber-700',
                                'delivered' => 'text-green-700',
                                'completed' => 'text-green-700',
                                'cancelled' => 'text-red-700'
                                ];
                                @endphp
                                <h3 class="text-lg font-bold {{ $statusColor[$payment->order->status] ?? 'text-amber-700' }}">
                                    {{ $statusText[$payment->order->status] ?? 'Menunggu Pembayaran' }}
                                </h3>
                                <p class="text-sm text-stone-600 mt-1">
                                    @if($payment->order->status == 'process')
                                    Pesanan Anda sedang kami proses
                                    @elseif($payment->order->status == 'paid')
                                    Terima kasih, pembayaran Anda telah dikonfirmasi
                                    @elseif($payment->order->status == 'shipped')
                                    Pesanan Anda dalam perjalanan
                                    @elseif($payment->order->status == 'delivered')
                                    Pesanan telah diterima, terima kasih
                                    @elseif($payment->order->status == 'completed')
                                    Pesanan selesai, terima kasih telah berbelanja
                                    @elseif($payment->order->status == 'cancelled')
                                    Pesanan telah dibatalkan
                                    @endif
                                </p>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="p-6">
                    <!-- Payment Details Grid -->
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                        <!-- Left Column - Payment Details -->
                        <div class="space-y-6">
                            <div>
                                <h3 class="text-lg font-bold text-stone-800 mb-4 flex items-center">
                                    <i class="fas fa-info-circle mr-2 text-amber-600"></i>
                                    Detail Pembayaran
                                </h3>
                                <div class="bg-amber-50/30 rounded-xl p-5 space-y-4 border border-amber-100">
                                    <div class="flex justify-between items-center pb-3 border-b border-amber-100">
                                        <span class="text-sm text-stone-600">Nomor Order</span>
                                        <span class="text-base font-bold text-amber-700 bg-amber-100 px-4 py-2 rounded-lg">
                                            #{{ $payment->order->order_id }}
                                        </span>
                                    </div>
                                    <div class="flex justify-between items-center">
                                        <span class="text-sm text-stone-600">Metode Pembayaran</span>
                                        <span class="text-base font-semibold bg-white px-4 py-2 rounded-lg border border-amber-200">
                                            <i class="fas fa-money-bill-wave mr-1 text-amber-600"></i>
                                            {{ strtoupper($payment->payment_method) == 'CASH' ? 'Cash on Delivery (COD)' : strtoupper($payment->payment_method) }}
                                        </span>
                                    </div>
                                    <div class="flex justify-between items-center">
                                        <span class="text-sm text-stone-600">Total Pembayaran</span>
                                        <span class="text-2xl font-bold text-amber-700">
                                            Rp {{ number_format($payment->amount, 0, ',', '.') }}
                                        </span>
                                    </div>
                                    <div class="flex justify-between items-center pt-3 border-t border-amber-100">
                                        <span class="text-sm text-stone-600">Status</span>
                                        @if($payment->order->status == 'process')
                                        <span class="px-4 py-2 bg-amber-100 text-amber-700 rounded-full text-sm font-semibold border border-amber-200">
                                            <i class="fas fa-spinner mr-1"></i> Diproses
                                        </span>
                                        @elseif($payment->order->status == 'paid')
                                        <span class="px-4 py-2 bg-green-100 text-green-700 rounded-full text-sm font-semibold border border-green-200">
                                            <i class="fas fa-check-circle mr-1"></i> Lunas
                                        </span>
                                        @elseif($payment->order->status == 'shipped')
                                        <span class="px-4 py-2 bg-amber-100 text-amber-700 rounded-full text-sm font-semibold border border-amber-200">
                                            <i class="fas fa-shipping-fast mr-1"></i> Dikirim
                                        </span>
                                        @elseif($payment->order->status == 'delivered' || $payment->order->status == 'completed')
                                        <span class="px-4 py-2 bg-green-100 text-green-700 rounded-full text-sm font-semibold border border-green-200">
                                            <i class="fas fa-check-double mr-1"></i> Selesai
                                        </span>
                                        @elseif($payment->order->status == 'cancelled')
                                        <span class="px-4 py-2 bg-red-100 text-red-700 rounded-full text-sm font-semibold border border-red-200">
                                            <i class="fas fa-times-circle mr-1"></i> Dibatalkan
                                        </span>
                                        @else
                                        <span class="px-4 py-2 bg-amber-100 text-amber-700 rounded-full text-sm font-semibold border border-amber-200">
                                            <i class="fas fa-clock mr-1"></i> Menunggu
                                        </span>
                                        @endif
                                    </div>
                                </div>
                            </div>

                            @if($payment->order->status == 'process')
                            <div class="bg-amber-50/50 rounded-xl p-5 border border-amber-200">
                                <h4 class="text-sm font-bold text-amber-800 mb-2 flex items-center">
                                    <i class="fas fa-info-circle mr-2"></i>
                                    Informasi COD
                                </h4>
                                <ul class="space-y-2 text-sm text-amber-700">
                                    <li class="flex items-start gap-2">
                                        <i class="fas fa-check-circle mt-0.5 text-xs"></i>
                                        <span>Bayar saat pesanan sampai di tempat Anda</span>
                                    </li>
                                    <li class="flex items-start gap-2">
                                        <i class="fas fa-check-circle mt-0.5 text-xs"></i>
                                        <span>Siapkan uang pas untuk memudahkan transaksi</span>
                                    </li>
                                    <li class="flex items-start gap-2">
                                        <i class="fas fa-check-circle mt-0.5 text-xs"></i>
                                        <span>Pesanan akan segera diproses oleh admin</span>
                                    </li>
                                </ul>
                            </div>
                            @endif
                        </div>

                        <!-- Right Column - Additional Info -->
                        <div class="space-y-6">
                            @if($payment->midtrans_payment_url && !$payment->isPaid())
                            <div>
                                <h3 class="text-lg font-bold text-stone-800 mb-4 flex items-center">
                                    <i class="fas fa-qrcode mr-2 text-amber-600"></i>
                                    Scan QR untuk Bayar
                                </h3>
                                <div class="bg-gradient-to-br from-amber-50 to-orange-50 rounded-xl p-6 border border-amber-200 text-center">
                                    <div class="inline-block p-4 bg-white rounded-xl shadow-md border border-amber-200 mb-4">
                                        <div id="qrcode"></div>
                                    </div>
                                    <p class="text-sm text-stone-600 mb-4">
                                        Scan dengan aplikasi e-wallet atau m-banking
                                    </p>
                                    <button onclick="openPaymentPopup()"
                                        class="btn-primary w-full inline-flex items-center justify-center px-6 py-3 font-medium rounded-xl shadow-md hover:shadow-lg transition-all duration-200">
                                        <i class="fas fa-external-link-alt mr-2"></i>
                                        Bayar Sekarang
                                    </button>
                                </div>
                            </div>
                            @endif

                            <!-- Shipping Address Summary -->
                            @if($payment->order->user->addresses->count() > 0)
                            @php $address = $payment->order->user->addresses->first(); @endphp
                            <div>
                                <h3 class="text-lg font-bold text-stone-800 mb-4 flex items-center">
                                    <i class="fas fa-map-marker-alt mr-2 text-amber-600"></i>
                                    Alamat Pengiriman
                                </h3>
                                <div class="bg-amber-50/30 rounded-xl p-5 border border-amber-100">
                                    <p class="font-semibold text-stone-800">{{ $payment->order->user->name }}</p>
                                    <p class="text-sm text-stone-600 mt-2">{{ $address->street }}, {{ $address->address_detail }}</p>
                                    <p class="text-sm text-stone-600">
                                        {{ $address->village?->name }}, {{ $address->district?->name }},
                                        {{ $address->city?->name }}, {{ $address->province?->name }}
                                    </p>
                                </div>
                            </div>
                            @endif
                        </div>
                    </div>

                    <!-- Order Items -->
                    <div class="mt-8">
                        <h3 class="text-lg font-bold text-stone-800 mb-4 flex items-center">
                            <i class="fas fa-shopping-bag mr-2 text-amber-600"></i>
                            Produk yang Dibeli
                            <span class="ml-auto text-sm font-normal text-stone-500">
                                {{ $payment->order->items->count() }} item
                            </span>
                        </h3>

                        <div class="bg-white rounded-xl border border-amber-100 divide-y divide-amber-100">
                            @foreach($payment->order->items as $item)
                            <div class="flex items-center gap-4 p-4 hover:bg-amber-50/30 transition-colors duration-200">
                                <div class="flex-shrink-0 w-16 h-16 bg-gradient-to-br from-amber-100 to-orange-100 rounded-xl overflow-hidden border border-amber-200">
                                    @php
                                    $productImage = $item->product->image ?? null;
                                    $imageUrl = null;

                                    if ($productImage) {
                                    if (is_array($productImage) && count($productImage) > 0) {
                                    $imageUrl = asset('storage/' . $productImage[0]);
                                    } elseif (is_string($productImage)) {
                                    $decoded = json_decode($productImage, true);
                                    if (is_array($decoded) && count($decoded) > 0) {
                                    $imageUrl = asset('storage/' . $decoded[0]);
                                    } else {
                                    $imageUrl = asset('storage/' . $productImage);
                                    }
                                    }
                                    }
                                    @endphp

                                    @if($imageUrl)
                                    <img src="{{ $imageUrl }}"
                                        alt="{{ $item->product->product_name }}"
                                        class="w-full h-full object-cover">
                                    @else
                                    <div class="w-full h-full flex items-center justify-center">
                                        <i class="fas fa-box text-2xl text-amber-300"></i>
                                    </div>
                                    @endif
                                </div>

                                <div class="flex-1 min-w-0">
                                    <h4 class="text-base font-semibold text-stone-800">
                                        {{ $item->product->product_name }}
                                    </h4>
                                    <div class="flex items-center gap-3 mt-1">
                                        <span class="text-sm text-stone-600">
                                            <i class="fas fa-times mr-1"></i>{{ $item->quantity }}
                                        </span>
                                        <span class="text-stone-300">|</span>
                                        <span class="text-sm text-stone-600">
                                            Rp {{ number_format($item->subtotal / $item->quantity, 0, ',', '.') }}/pcs
                                        </span>
                                    </div>
                                </div>

                                <div class="text-right">
                                    <p class="text-base font-bold text-amber-700">
                                        Rp {{ number_format($item->subtotal, 0, ',', '.') }}
                                    </p>
                                </div>
                            </div>
                            @endforeach

                            <!-- Total Summary -->
                            <div class="p-5 bg-gradient-to-r from-amber-50/50 via-orange-50/30 to-amber-50/50">
                                <div class="flex justify-between items-center">
                                    <span class="text-sm font-semibold text-stone-700">Subtotal Produk</span>
                                    <span class="text-base font-bold text-stone-800">Rp {{ number_format($payment->order->items->sum('subtotal'), 0, ',', '.') }}</span>
                                </div>
                                <div class="flex justify-between items-center mt-2">
                                    <div class="flex flex-col">
                                        <span class="text-sm font-semibold text-stone-700">Biaya Pengiriman</span>
                                        @if($payment->order->courier_name)
                                        <span class="text-xs text-stone-500 mt-0.5">
                                            <i class="fas fa-truck mr-1"></i>{{ $payment->order->courier_name }}
                                        </span>
                                        @endif
                                    </div>
                                    @if((int)$payment->order->shipping_cost > 0)
                                    <span class="text-base font-bold text-stone-800">
                                        Rp {{ number_format($payment->order->shipping_cost, 0, ',', '.') }}
                                    </span>
                                    @else
                                    <span class="text-base font-bold text-green-600 flex items-center">
                                        <i class="fas fa-check-circle mr-1"></i>Gratis
                                    </span>
                                    @endif
                                </div>
                                <div class="flex justify-between items-center mt-4 pt-4 border-t border-amber-200">
                                    <span class="text-base font-bold text-stone-800">Total Pembayaran</span>
                                    <span class="text-2xl font-extrabold text-amber-700">
                                        Rp {{ number_format($payment->amount, 0, ',', '.') }}
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Actions -->
                    <div class="mt-8 flex flex-col sm:flex-row justify-between gap-4">
                        <a href="{{ route('order.index') }}"
                            class="inline-flex items-center justify-center px-6 py-3 bg-stone-100 hover:bg-stone-200 text-stone-700 font-medium rounded-xl border border-stone-200 transition-all duration-200">
                            <i class="fas fa-arrow-left mr-2"></i>
                            Lihat Semua Pesanan
                        </a>

                        @if($payment->order->status == 'process')
                        <button onclick="window.location.reload()"
                            class="btn-primary inline-flex items-center justify-center px-6 py-3 font-medium rounded-xl shadow-md hover:shadow-lg transition-all duration-200">
                            <i class="fas fa-sync-alt mr-2"></i>
                            Refresh Status
                        </button>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal Popup untuk Midtrans (jika diperlukan) -->
    @if($payment->midtrans_payment_url && !$payment->isPaid())
    <div id="paymentModal" class="hidden fixed inset-0 bg-black bg-opacity-50 backdrop-blur-sm overflow-y-auto h-full w-full z-50">
        <!-- Modal content -->
        <div class="relative top-10 mx-auto p-5 w-11/12 md:w-3/4 lg:w-2/3">
            <div class="bg-white rounded-2xl shadow-2xl border border-amber-200 overflow-hidden">
                <div class="flex justify-between items-center p-5 border-b border-amber-100 bg-gradient-to-r from-amber-50 to-orange-50">
                    <h3 class="text-lg font-bold text-stone-800 flex items-center">
                        <i class="fas fa-credit-card mr-2 text-amber-600"></i>
                        Halaman Pembayaran
                    </h3>
                    <button onclick="closePaymentPopup()" class="w-10 h-10 bg-white rounded-lg flex items-center justify-center hover:bg-amber-50 transition-colors border border-amber-200">
                        <i class="fas fa-times text-stone-600"></i>
                    </button>
                </div>
                <div class="p-1">
                    <iframe id="paymentFrame" class="w-full h-[600px] rounded-b-lg" frameborder="0"></iframe>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
    <script>
        new QRCode(document.getElementById("qrcode"), {
            text: "{{ $payment->midtrans_payment_url }}",
            width: 180,
            height: 180,
            colorDark: "#d97706",
            colorLight: "#ffffff",
        });

        function openPaymentPopup() {
            const modal = document.getElementById('paymentModal');
            const iframe = document.getElementById('paymentFrame');
            iframe.src = "{{ $payment->midtrans_payment_url }}";
            modal.classList.remove('hidden');
        }

        function closePaymentPopup() {
            const modal = document.getElementById('paymentModal');
            const iframe = document.getElementById('paymentFrame');
            iframe.src = '';
            modal.classList.add('hidden');
        }
    </script>
    @endif

    <style>
        /* Animation styles */
        @keyframes slideIn {
            from {
                transform: translateX(100%);
                opacity: 0;
            }

            to {
                transform: translateX(0);
                opacity: 1;
            }
        }

        .animate-slideIn {
            animation: slideIn 0.3s ease-out;
        }

        /* Additional brand color overrides */
        .bg-indigo-50 {
            background-color: #fffbeb !important;
        }

        .bg-indigo-100 {
            background-color: #fef3c7 !important;
        }

        .text-indigo-600 {
            color: #d97706 !important;
        }

        .text-indigo-700 {
            color: #b45309 !important;
        }

        .border-indigo-200 {
            border-color: #fde68a !important;
        }

        .from-indigo-50 {
            --tw-gradient-from: #fffbeb !important;
        }

        .to-purple-50 {
            --tw-gradient-to: #fff7ed !important;
        }

        .via-indigo-100 {
            --tw-gradient-via: #fef3c7 !important;
        }
    </style>
</x-app-layout>