@extends('layout.app')

@section('content')
<div class="card shadow border-0">
    <div class="card-header bg-white d-flex justify-content-between align-items-center py-3">
        <h5 class="mb-0 fw-bold"><i class="ti ti-shopping-cart me-2"></i>Kelola Pesanan</h5>
        <span class="badge bg-primary rounded-pill">{{ $orders->count() }} Pesanan</span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-4">No</th>
                        <th>Pembeli</th>
                        <th>Produk</th>
                        <th>Total</th>
                        <th>Pembayaran</th>
                        <th>Status</th>
                        <th class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($orders as $order)
                    <tr>
                        <td class="ps-4">{{ $loop->iteration }}</td>
                        <td>
                            <div class="fw-bold">{{ $order->user->name }}</div>
                            <div class="small text-muted">{{ $order->user->email }}</div>
                        </td>
                        <td>
                            @foreach($order->items as $item)
                            <div class="small">{{ $item->product->name }} <span class="text-muted">x{{ $item->jumlah }}</span></div>
                            @endforeach
                        </td>
                        <td class="fw-bold text-success">
                            Rp {{ number_format($order->total_harga, 0, ',', '.') }}
                        </td>
                        <td>
                            @if($order->payment)
                                <span class="badge {{ $order->payment->status === 'lunas' ? 'bg-success' : 'bg-warning text-dark' }}">
                                    {{ ucfirst($order->payment->status) }}
                                </span>
                            @else
                                <span class="badge bg-secondary">Belum Ada</span>
                            @endif
                        </td>
                        <td>
                            @php
                                $badgeColor = match($order->status) {
                                    'menunggu'  => 'bg-warning text-dark',
                                    'diproses'  => 'bg-primary',
                                    'dikirim'   => 'bg-info text-dark',
                                    'selesai'   => 'bg-success',
                                    'dibatalkan'=> 'bg-danger',
                                    default     => 'bg-secondary'
                                };
                            @endphp
                            <span class="badge {{ $badgeColor }}">{{ ucfirst($order->status) }}</span>
                        </td>
                        <td class="text-center">
                            {{-- Tombol Detail (modal) --}}
                            <button class="btn btn-sm btn-light border" 
                                    data-bs-toggle="modal" 
                                    data-bs-target="#modalOrder{{ $order->id }}">
                                <i class="ti ti-eye"></i>
                            </button>

                            {{-- Dropdown Update Status --}}
                            <div class="d-inline-block">
                                <form action="{{ route('admin.orders.status', $order->id) }}" method="POST" class="d-inline">
                                    @csrf
                                    <select name="status" class="form-select form-select-sm d-inline-block w-auto ms-1"
                                            onchange="this.form.submit()">
                                        <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' : '' }}>Dibatalkan</option>
                                    </select>
                                </form>
                            </div>
                        </td>
                    </tr>

                    {{-- MODAL DETAIL ORDER --}}
                    <div class="modal fade" id="modalOrder{{ $order->id }}" tabindex="-1">
                        <div class="modal-dialog modal-dialog-centered">
                            <div class="modal-content border-0 shadow">
                                <div class="modal-header border-0 pb-0">
                                    <h6 class="modal-title fw-bold">Detail Order #{{ $order->id }}</h6>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    {{-- Info Pembeli --}}
                                    <div class="bg-light rounded-3 p-3 mb-3">
                                        <div class="small text-muted mb-1">Pembeli</div>
                                        <div class="fw-bold">{{ $order->user->name }}</div>
                                        <div class="small">{{ $order->user->email }}</div>
                                    </div>

                                    {{-- Produk --}}
                                    <div class="mb-3">
                                        <div class="small text-muted mb-2">Produk Dipesan</div>
                                        @foreach($order->items as $item)
                                        <div class="d-flex align-items-center gap-3 mb-2">
                                            <img src="{{ asset('storage/'.$item->product->image) }}" width="45" height="45" class="rounded-3 border" style="object-fit:cover;">
                                            <div class="flex-grow-1">
                                                <div class="fw-bold small">{{ $item->product->name }}</div>
                                                <div class="small text-muted">{{ $item->jumlah }} x Rp {{ number_format($item->harga, 0, ',', '.') }}</div>
                                            </div>
                                            <div class="fw-bold small">Rp {{ number_format($item->subtotal, 0, ',', '.') }}</div>
                                        </div>
                                        @endforeach
                                    </div>

                                    {{-- Alamat --}}
                                    @if($order->address)
                                    <div class="bg-light rounded-3 p-3 mb-3">
                                        <div class="small text-muted mb-1">Alamat Pengiriman</div>
                                        <div class="fw-bold small">{{ $order->address->nama_penerima }} — {{ $order->address->no_hp }}</div>
                                        <div class="small text-muted">{{ $order->address->alamat_lengkap }}</div>
                                    </div>
                                    @endif

                                    {{-- Total --}}
                                    <div class="d-flex justify-content-between align-items-center border-top pt-3">
                                        <span class="fw-bold">Total Bayar</span>
                                        <span class="fw-bold text-success fs-5">Rp {{ number_format($order->total_harga, 0, ',', '.') }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    @empty
                    <tr>
                        <td colspan="7" class="text-center py-5 text-muted">
                            <i class="ti ti-inbox fs-1 d-block mb-2 opacity-50"></i>
                            Belum ada pesanan masuk.
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection