@extends('layout.app')

@section('content')
<div class="row animate-pro">
    <div class="col-12">
        <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
            <div class="card-body p-4 p-md-5">
                
                <div class="d-flex justify-content-between align-items-center mb-5">
                    <div>
                        <h4 class="fw-800 mb-1">Daftar Pesanan Pelanggan</h4>
                        <p class="text-muted small mb-0">Total {{ $orders->count() }} pesanan barang masuk untuk diproses.</p>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover align-middle border-light">
                        <thead class="bg-light">
                            <tr>
                                <th class="border-0 rounded-start px-4 py-3 text-muted small fw-800">PEMBELI</th>
                                <th class="border-0 py-3 text-muted small fw-800">PRODUK</th>
                                <th class="border-0 py-3 text-muted small fw-800">WILAYAH KIRIM</th>
                                <th class="border-0 py-3 text-muted small fw-800">TOTAL HARGA</th>
                                <th class="border-0 py-3 text-muted small fw-800">STATUS</th>
                                <th class="border-0 rounded-end px-4 py-3 text-muted small fw-800 text-end">AKSI</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($orders as $order)
                            <tr>
                                <td class="px-4 py-4">
                                    <div class="d-flex align-items-center gap-3">
                                        <div class="avatar-mini-pro">{{ substr($order->user->name, 0, 1) }}</div>
                                        <div>
                                            <div class="fw-bold text-dark">{{ $order->user->name }}</div>
                                            <div class="text-muted" style="font-size: 11px;">{{ $order->user->email }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="fw-bold text-dark" style="font-size: 13px;">{{ $order->items->count() }} Produk</div>
                                    <div class="text-muted small text-truncate" style="max-width: 150px;">
                                        {{ $order->items->first()->product->name ?? '-' }}
                                    </div>
                                </td>
                                <td>
                                    @if($order->address)
                                        <div class="d-flex align-items-center gap-2">
                                            <div>
                                                <div class="fw-800 text-dark" style="font-size: 13px;">{{ $j->address->regency->name ?? $order->address->regency->name ?? '-' }}</div>
                                                <div class="text-muted small">{{ $j->address->province->name ?? $order->address->province->name ?? '-' }}</div>
                                            </div>
                                            @php
                                                $queryMap = urlencode(
                                                    ($order->address->alamat_lengkap ?? '') . ', ' . 
                                                    ($order->address->village->name ?? '') . ', ' . 
                                                    ($order->address->district->name ?? '') . ', ' . 
                                                    ($order->address->regency->name ?? '') . ', ' . 
                                                    ($order->address->province->name ?? '')
                                                );
                                            @endphp
                                            <a href="https://www.google.com/maps?q={{ $queryMap }}" target="_blank" class="text-primary opacity-75 hover-scale">
                                                <i class="ti ti-map-2 fs-5"></i>
                                            </a>
                                        </div>
                                    @else
                                        <div class="text-muted small fst-italic">No Address</div>
                                    @endif
                                </td>
                                <td class="fw-bold text-success">
                                    Rp {{ number_format($order->total_harga, 0, ',', '.') }}
                                </td>
                                <td>
                                    @php
                                        $badgeColor = match($order->status) {
                                            'menunggu'  => 'bg-soft-warning text-warning',
                                            'diproses'  => 'bg-soft-primary text-primary',
                                            'dikirim'   => 'bg-soft-info text-info',
                                            'selesai'   => 'bg-soft-success text-success',
                                            'dibatalkan'=> 'bg-soft-danger text-danger',
                                            default     => 'bg-light text-muted'
                                        };
                                    @endphp
                                    <span class="badge {{ $badgeColor }} rounded-pill px-3 py-1 fw-bold text-uppercase" style="font-size: 10px;">
                                        {{ $order->status }}
                                    </span>
                                </td>
                                <td class="px-4 text-end">
                                    <div class="d-flex align-items-center justify-content-end gap-2">
                                        <button class="btn btn-sm btn-light rounded-3 px-3 py-2 fw-bold" type="button" data-bs-toggle="modal" data-bs-target="#modalOrder{{ $order->id }}">
                                            Detail
                                        </button>
                                        <form action="{{ route('admin.orders.status', $order->id) }}" method="POST" class="d-inline">
                                            @csrf
                                            @method('PATCH')
                                            <select name="status" class="form-select form-select-sm border-0 bg-light rounded-pill px-3 py-2 fw-bold" 
                                                    onchange="this.form.submit()" style="font-size: 12px; cursor: pointer;">
                                                <option value="menunggu"   {{ $order->status === 'menunggu'   ? 'selected' : '' }}>🟡 Menunggu</option>
                                                <option value="diproses"   {{ $order->status === 'diproses'   ? 'selected' : '' }}>🔵 Diproses</option>
                                                <option value="dikirim"    {{ $order->status === 'dikirim'    ? 'selected' : '' }}>🚚 Dikirim</option>
                                                <option value="selesai"    {{ $order->status === 'selesai'    ? 'selected' : '' }}>🟢 Selesai</option>
                                                <option value="dibatalkan" {{ $order->status === 'dibatalkan' ? 'selected' : '' }}>🛑 Batal</option>
                                            </select>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                            @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">Belum ada pesanan barang masuk.</td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

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

<style>
    .bg-soft-info { background: #e0f2fe; color: #0369a1; }
    .bg-soft-warning { background: #fef3c7; color: #92400e; }
    .bg-soft-success { background: #dcfce7; color: #15803d; }
    .bg-soft-primary { background: #e0e7ff; color: #4338ca; }
    .bg-soft-danger { background: #fee2e2; color: #b91c1c; }
    .fw-800 { font-weight: 800; }
    .avatar-mini-pro {
        width: 38px; height: 38px;
        border-radius: 12px;
        background: var(--pro-primary);
        color: #fff;
        display: flex; align-items: center; justify-content: center;
        font-weight: 800; font-size: 14px;
    }
    .hover-scale:hover { transform: scale(1.2); transition: all 0.2s; }
</style>
@push('modals')
@foreach($orders as $order)
    {{-- MODAL DETAIL ORDER --}}
    <div class="modal fade" id="modalOrder{{ $order->id }}" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content border-0 shadow-lg" style="border-radius: 20px;">
                <div class="modal-header border-0 p-4">
                    <h5 class="fw-800 mb-0">Detail Pesanan #{{ $order->id }}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body p-4 pt-0">
                    <div class="row g-4">
                        <div class="col-md-7">
                            {{-- Daftar Produk --}}
                            <div class="mb-4">
                                <label class="small text-muted fw-bold d-block mb-3 text-uppercase">Produk Yang Dibeli:</label>
                                @foreach($order->items as $item)
                                <div class="d-flex align-items-center gap-3 mb-3 bg-light p-3 rounded-4">
                                    <img src="{{ asset('storage/'.$item->product->image) }}" class="rounded-3" width="55" height="55" style="object-fit: cover;">
                                    <div class="flex-grow-1">
                                        <div class="fw-bold text-dark small">{{ $item->product->name }}</div>
                                        <div class="text-muted" style="font-size: 11px;">{{ $item->jumlah }} unit x Rp {{ number_format($item->harga, 0, ',', '.') }}</div>
                                    </div>
                                    <div class="fw-bold text-dark small">Rp {{ number_format($item->subtotal, 0, ',', '.') }}</div>
                                </div>
                                @endforeach
                            </div>

                            {{-- Info Pengiriman --}}
                            <label class="small text-muted fw-bold d-block mb-1">DATA PENGIRIMAN:</label>
                            @if($order->address)
                                <div class="p-3 bg-white border rounded-4">
                                    <div class="row g-3">
                                        <div class="col-6">
                                            <div class="small fw-bold text-muted mb-1">Penerima</div>
                                            <div class="small fw-bold">{{ $order->address->nama_penerima }}</div>
                                        </div>
                                        <div class="col-6">
                                            <div class="small fw-bold text-muted mb-1">Telepon</div>
                                            <div class="small fw-bold">{{ $order->address->no_hp }}</div>
                                        </div>
                                        <div class="col-6 border-top pt-2">
                                            <div class="small fw-bold text-muted mb-1">KOTA/KAB</div>
                                            <div class="small fw-bold text-dark">{{ $order->address->regency->name ?? '-' }}</div>
                                        </div>
                                        <div class="col-6 border-top pt-2">
                                            <div class="small fw-bold text-muted mb-1">PROVINSI</div>
                                            <div class="small fw-bold text-dark">{{ $order->address->province->name ?? '-' }}</div>
                                        </div>
                                        <div class="col-12 border-top pt-3">
                                            <div class="small fw-bold text-muted mb-1">ALAMAT TUJUAN:</div>
                                            <p class="small text-muted mb-3">{{ $order->address->alamat_lengkap }}</p>
                                            @php
                                                $queryMap = urlencode(
                                                    ($order->address->alamat_lengkap ?? '') . ', ' . 
                                                    ($order->address->village->name ?? '') . ', ' . 
                                                    ($order->address->district->name ?? '') . ', ' . 
                                                    ($order->address->regency->name ?? '') . ', ' . 
                                                    ($order->address->province->name ?? '')
                                                );
                                            @endphp
                                            <a href="https://www.google.com/maps?q={{ $queryMap }}" target="_blank" class="btn btn-sm btn-primary rounded-pill px-4 fw-bold py-3 w-100 shadow-sm border-0" style="background: linear-gradient(135deg, #4f46e5, #0ea5e9);">
                                                <i class="ti ti-map-pin me-1"></i> BUKA NAVIGASI GOOGLE MAPS
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            @endif
                        </div>
                        <div class="col-md-5">
                            <div class="bg-light rounded-4 p-4 h-100">
                                <h6 class="fw-800 text-dark mb-4 text-center">Ringkasan Biaya</h6>
                                <div class="d-flex justify-content-between mb-2">
                                    <span class="text-muted small">Subtotal</span>
                                    <span class="fw-bold small">Rp {{ number_format($order->total_harga, 0, ',', '.') }}</span>
                                </div>
                                <div class="d-flex justify-content-between mb-4">
                                    <span class="text-muted small">Biaya Pengiriman</span>
                                    <span class="text-success small fw-bold">GRATIS</span>
                                </div>
                                <hr>
                                <div class="d-flex justify-content-between align-items-center mt-4">
                                    <span class="fw-800">Total Akhir</span>
                                    <span class="fw-800 text-success fs-5">Rp {{ number_format($order->total_harga, 0, ',', '.') }}</span>
                                </div>
                                
                                <div class="mt-5">
                                    <label class="small text-muted fw-bold d-block mb-3 text-uppercase text-center">Lacak Status Pembayaran:</label>
                                    @if($order->payment)
                                        <div class="text-center">
                                            <div class="badge {{ $order->payment->status === 'lunas' ? 'bg-soft-success text-success' : 'bg-soft-warning text-warning' }} px-4 py-2 rounded-pill fw-bold fs-12">
                                                PEMBAYARAN {{ strtoupper($order->payment->status) }}
                                            </div>
                                        </div>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-0 p-4 pt-0">
                    <a href="https://wa.me/{{ preg_replace('/[^0-9]/', '', $order->user->phone) }}" target="_blank" class="btn btn-success w-100 py-3 rounded-pill fw-bold shadow-sm">
                        <i class="ti ti-brand-whatsapp me-2"></i> HUBUNGI PEMBELI VIA WHATSAPP
                    </a>
                </div>
            </div>
        </div>
    </div>
@endforeach
@endpush
@endsection