@extends('layouts.frontend')

@section('title', 'Pesanan Saya')

@section('content')
    <div class="container py-5 mt-4">
        {{-- Header Section --}}
        <div class="d-flex justify-content-between align-items-center mb-5">
            <div>
                <h2 class="fw-bold text-dark mb-0">Riwayat Pesanan</h2>
                <p class="text-muted small mb-0">Pantau status belanjaan Anda di sini</p>
            </div>
        </div>


        @if ($transaksis->isEmpty())
            {{-- Empty State --}}
            <div class="text-center py-5">
                <div class="bg-light rounded-circle d-inline-flex align-items-center justify-content-center mb-4"
                    style="width: 100px; height: 100px;">
                    <i class="fas fa-receipt fa-3x text-secondary opacity-50"></i>
                </div>
                <h5 class="fw-bold text-dark">Belum ada pesanan</h5>
                <p class="text-muted mb-4">Yuk, mulai isi keranjang belanja Anda dengan produk segar!</p>
                <a href="{{ route('shop') }}" class="btn btn-outline-primary rounded-pill px-4">Mulai Belanja</a>
            </div>
        @else
            {{-- List Transaksi --}}
            <div class="row g-4">
                @foreach ($transaksis as $trx)
                    <div class="col-12">
                        <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
                            {{-- Invoice & Status --}}
                            <div
                                class="card-header bg-white border-bottom border-light p-4 d-flex flex-wrap justify-content-between align-items-center">
                                <div class="d-flex align-items-center gap-3">
                                    <div class="bg-light p-2 rounded text-center" style="width: 50px;">
                                        <i class="fas fa-file-invoice text-primary fs-4"></i>
                                    </div>
                                    <div>
                                        <h6 class="fw-bold mb-0 text-dark">#{{ $trx->kode_invoice }}</h6>
                                        <small
                                            class="text-muted">{{ \Carbon\Carbon::parse($trx->tanggal_transaksi)->format('d M Y • H:i') }}</small>
                                    </div>
                                </div>

                                @php
                                    $statusClass = match ($trx->status) {
                                        'menunggu_konfirmasi', 'menunggu konfirmasi' => 'bg-warning text-dark bg-opacity-25 border border-warning',
                                        'diproses' => 'bg-info text-dark bg-opacity-25 border border-info',
                                        'dikirim' => 'bg-primary bg-opacity-10 border border-primary',
                                        'selesai' => 'bg-success bg-opacity-10 border border-success',
                                        'batal' => 'bg-danger bg-opacity-10 border border-danger',
                                        default => 'bg-secondary text-secondary bg-opacity-10'
                                    };
                                @endphp
                                <span class="badge {{ $statusClass }} px-3 py-2 rounded-pill mt-2 mt-sm-0">
                                    {{ ucwords(str_replace('_', ' ', $trx->status)) }}
                                </span>
                            </div>

                            <div class="card-body p-4">
                                <div class="row align-items-center">

                                    {{-- Preview Produk --}}
                                    <div class="col-md-7 mb-3 mb-md-0">
                                        @php $firstItem = $trx->detailTransaksis->first(); @endphp
                                        @if($firstItem)
                                            <div class="d-flex align-items-center">
                                                <img src="{{ $firstItem->produk->foto_produk ? asset('storage/' . $firstItem->produk->foto_produk) : 'https://images.unsplash.com/photo-1586201375761-83865001e31c?q=80&w=200&auto=format&fit=crop' }}"
                                                    class="rounded-3 object-fit-cover me-3 shadow-sm" width="60" height="60"
                                                    alt="Produk">

                                                <div>
                                                    <h6 class="fw-bold text-dark mb-1">{{ $firstItem->produk->nama_produk }}</h6>
                                                    <p class="text-muted small mb-0">
                                                        {{ $firstItem->jumlah }} kg x Rp
                                                        {{ number_format($firstItem->harga_satuan, 0, ',', '.') }}
                                                        @if ($trx->detailTransaksis->count() > 1)
                                                            <span class="text-primary fw-bold ms-1">+
                                                                {{ $trx->detailTransaksis->count() - 1 }} produk lainnya</span>
                                                        @endif
                                                    </p>
                                                </div>
                                            </div>
                                        @endif
                                    </div>

                                    {{-- Total & Button --}}
                                    <div class="col-md-5 text-md-end border-start-md ps-md-4">
                                        <small class="text-muted d-block mb-1">Total Tagihan</small>
                                        <h5 class="fw-bold text-dark mb-3">Rp {{ number_format($trx->total_harga, 0, ',', '.') }}
                                        </h5>

                                        <div class="d-flex justify-content-md-end gap-2">
                                            <button class="btn btn-light btn-sm rounded-pill px-3 border" data-bs-toggle="modal"
                                                data-bs-target="#detailModal{{ $trx->id }}">
                                                Lihat Detail
                                            </button>

                                            @if ($trx->status == 'dikirim')
                                                <form action="{{ route('pesanan.selesai', $trx->id) }}" method="POST"
                                                    onsubmit="return confirm('Apakah Anda yakin barang sudah diterima dengan baik?')">
                                                    @csrf
                                                    <button type="submit" class="btn btn-success btn-sm rounded-pill px-3 shadow-sm">
                                                        <i class="fas fa-check me-1"></i> Pesanan Diterima
                                                    </button>
                                                </form>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    {{-- MODAL DETAIL --}}
                    <div class="modal fade" id="detailModal{{ $trx->id }}" tabindex="-1" aria-hidden="true">
                        <div class="modal-dialog modal-dialog-centered modal-lg">
                            <div class="modal-content border-0 rounded-4 shadow-lg overflow-hidden">

                                {{-- Header: Putih Bersih --}}
                                <div class="modal-header border-bottom-0 pt-4 px-4 pb-0">
                                    <div>
                                        <h5 class="modal-title fw-bold text-dark" style="font-size: 1.25rem;">Detail Pesanan</h5>
                                        <p class="text-muted small mb-0">Invoice: <span
                                                class="font-monospace">#{{ $trx->kode_invoice }}</span></p>
                                    </div>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>

                                <div class="modal-body p-4">

                                    {{-- 1. STATUS BANNER (Meniru Style Referensi) --}}
                                    @php
                                        $statusConfig = match ($trx->status) {
                                            'menunggu_konfirmasi', 'menunggu konfirmasi' => ['bg' => '#fff3cd', 'text' => '#856404', 'icon' => 'fa-clock', 'title' => 'Menunggu Konfirmasi', 'desc' => 'Menunggu penjual menerima pesanan Anda.'],
                                            'diproses' => ['bg' => '#cff4fc', 'text' => '#055160', 'icon' => 'fa-box-open', 'title' => 'Sedang Diproses', 'desc' => 'Penjual sedang mengemas barang Anda.'],
                                            'dikirim' => ['bg' => '#cfe2ff', 'text' => '#084298', 'icon' => 'fa-truck', 'title' => 'Dalam Pengiriman', 'desc' => 'Kurir sedang menuju ke lokasi Anda.'],
                                            'selesai' => ['bg' => '#d1e7dd', 'text' => '#0f5132', 'icon' => 'fa-check-circle', 'title' => 'Transaksi Selesai', 'desc' => 'Terima kasih telah berbelanja produk lokal!'],
                                            'batal' => ['bg' => '#f8d7da', 'text' => '#842029', 'icon' => 'fa-times-circle', 'title' => 'Dibatalkan', 'desc' => 'Transaksi ini telah dibatalkan.'],
                                            default => ['bg' => '#e2e3e5', 'text' => '#41464b', 'icon' => 'fa-question-circle', 'title' => 'Status Tidak Dikenal', 'desc' => '']
                                        };
                                    @endphp

                                    <div class="d-flex align-items-center p-3 rounded-3 mb-4"
                                        style="background-color: {{ $statusConfig['bg'] }}; color: {{ $statusConfig['text'] }};">
                                        <i class="fas {{ $statusConfig['icon'] }} fs-3 me-3"></i>
                                        <div>
                                            <h6 class="fw-bold mb-0">{{ $statusConfig['title'] }}</h6>
                                            <small style="opacity: 0.9;">{{ $statusConfig['desc'] }}</small>
                                        </div>
                                    </div>

                                    <div class="row g-4 mb-4">
                                        {{-- 2. INFO PENJUAL (Kiri) --}}
                                        <div class="col-md-6">
                                            <label class="small text-secondary fw-bold text-uppercase mb-2"
                                                style="font-size: 0.75rem; letter-spacing: 0.5px;">Dijual Oleh</label>
                                            <div class="bg-light p-3 rounded-3 d-flex align-items-center justify-content-between">
                                                <div class="d-flex align-items-center">
                                                    <div class="bg-white rounded-circle shadow-sm d-flex align-items-center justify-content-center me-3"
                                                        style="width: 45px; height: 45px;">
                                                        <i class="fas fa-store text-primary"></i>
                                                    </div>
                                                    <div>
                                                        <h6 class="fw-bold text-dark mb-0" style="font-size: 0.95rem;">
                                                            {{ $trx->petani->nama_lengkap ?? 'Petani' }}</h6>
                                                        <small class="text-muted"
                                                            style="font-size: 0.85rem;">{{ $trx->petani->nama_usaha ?? 'Tani Desa Seller' }}</small>
                                                    </div>
                                                </div>
                                                @if($trx->petani)
                                                    <a href="https://wa.me/{{ $trx->petani->no_hp }}" target="_blank"
                                                        class="btn btn-success btn-sm rounded-circle d-flex align-items-center justify-content-center"
                                                        style="width: 35px; height: 35px;" data-bs-toggle="tooltip"
                                                        title="Chat WhatsApp">
                                                        <i class="fab fa-whatsapp"></i>
                                                    </a>
                                                @endif
                                            </div>
                                        </div>

                                        {{-- 3. INFO PENGIRIMAN (Kanan) --}}
                                        <div class="col-md-6">
                                            <label class="small text-secondary fw-bold text-uppercase mb-2"
                                                style="font-size: 0.75rem; letter-spacing: 0.5px;">Info Pengiriman</label>
                                            <div class="bg-light p-3 rounded-3 h-100">
                                                <div class="d-flex mb-2">
                                                    <i class="fas fa-map-marker-alt text-danger mt-1 me-2"></i>
                                                    <div>
                                                        <h6 class="fw-bold text-dark mb-0" style="font-size: 0.95rem;">Alamat Tujuan
                                                        </h6>
                                                        <small class="text-muted d-block text-truncate"
                                                            style="max-width: 200px;">{{ $trx->alamat_pengiriman }}</small>
                                                    </div>
                                                </div>
                                                <div class="ms-4 ps-1 border-top pt-2 mt-2">
                                                    <small class="text-muted">Metode Bayar: <span class="fw-bold text-dark">Cash on
                                                            Delivery (COD)</span></small>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    {{-- 4. RINCIAN BARANG (Style Card List) --}}
                                    <label class="small text-secondary fw-bold text-uppercase mb-2"
                                        style="font-size: 0.75rem; letter-spacing: 0.5px;">Rincian Barang</label>
                                    <div class="border rounded-3 overflow-hidden mb-4">
                                        @foreach ($trx->detailTransaksis as $d)
                                            <div class="d-flex align-items-center justify-content-between p-3 bg-white border-bottom">
                                                <div class="d-flex align-items-center">
                                                    <img src="{{ $d->produk->foto_produk ? asset('storage/' . $d->produk->foto_produk) : 'https://images.unsplash.com/photo-1586201375761-83865001e31c?q=80&w=200&auto=format&fit=crop' }}"
                                                        class="rounded-3 object-fit-cover me-3"
                                                        style="width: 60px; height: 60px; background-color: #f8f9fa;">
                                                    <div>
                                                        <h6 class="mb-1 fw-bold text-dark">{{ $d->produk->nama_produk }}</h6>
                                                        <small class="text-muted d-block">{{ $d->jumlah }} kg x Rp
                                                            {{ number_format($d->harga_satuan, 0, ',', '.') }}</small>
                                                    </div>
                                                </div>
                                                <div class="fw-bold text-dark">
                                                    Rp {{ number_format($d->subtotal, 0, ',', '.') }}
                                                </div>
                                            </div>
                                        @endforeach
                                    </div>

                                    {{-- 5. TOTAL BLOCK (Hijau Solid seperti referensi) --}}
                                    @php
                                        // Menggunakan warna background sesuai status agar konsisten, atau default hijau fresh
                                        $footerBg = ($trx->status == 'batal') ? '#dc3545' : (($trx->status == 'menunggu_konfirmasi') ? '#ffc107' : '#82c91e');
                                        $footerText = ($trx->status == 'menunggu_konfirmasi') ? '#000' : '#fff';
                                    @endphp
                                    <div class="p-3 rounded-3" style="background-color: {{ $footerBg }}; color: {{ $footerText }};">
                                        <div class="d-flex justify-content-between align-items-center mb-1">
                                            <span style="opacity: 0.9;">Subtotal Produk</span>
                                            <span class="fw-bold">Rp {{ number_format($trx->total_harga, 0, ',', '.') }}</span>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-center">
                                            <span style="opacity: 0.9;">Biaya Pengiriman (COD)</span>
                                            <span class="fw-bold">Gratis</span>
                                        </div>
                                        <hr class="my-2" style="opacity: 0.3;">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <span class="fw-bold fs-5">Total Pembayaran</span>
                                            <span class="fw-bold fs-5">Rp {{ number_format($trx->total_harga, 0, ',', '.') }}</span>
                                        </div>
                                    </div>

                                </div>

                                <div class="modal-footer border-top-0 pt-0 pb-4 px-4">
                                    <button type="button" class="btn btn-light border rounded-pill px-4 w-100"
                                        data-bs-dismiss="modal">Tutup</button>
                                </div>
                            </div>
                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </div>
@endsection

@push('style')
    <style>
        @media (min-width: 768px) {
            .border-start-md {
                border-left: 1px solid #dee2e6 !important;
            }
        }
    </style>
@endpush