
@extends('layouts.frontend')

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

@section('content')
<div class="container py-5">
    <h2 class="mb-4 fw-bold text-primary">Riwayat Pesanan</h2>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fa fa-check-circle me-2"></i> {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="card border-0 shadow-sm rounded">
        <div class="card-body">
            @if($transaksis->isEmpty())
                <div class="text-center py-5">
                    <i class="fas fa-shopping-basket fa-4x text-muted mb-3"></i>
                    <p class="text-muted">Belum ada pesanan.</p>
                    <a href="{{ route('shop') }}" class="btn btn-primary rounded-pill">Mulai Belanja</a>
                </div>
            @else
                <div class="table-responsive">
                    <table class="table table-hover align-middle">
    <thead class="bg-light">
        <tr>
            <th>Invoice</th>
            <th>Tanggal</th>
            <th>Produk</th>
            <th>Total</th>
            <th>Status</th>
            <th>Aksi</th>
        </tr>
    </thead>
    <tbody>
        @foreach($transaksis as $trx)
        <tr>
            <td class="fw-bold text-primary">#{{ $trx->kode_invoice }}</td>
            <td>{{ \Carbon\Carbon::parse($trx->tanggal_transaksi)->format('d M Y') }}</td>
            <td>
                {{-- Tampilkan 1 produk utama saja agar tabel rapi --}}
                <div class="d-flex align-items-center mb-1">
                    @php $firstItem = $trx->details->first(); @endphp
                    <img src="{{ $firstItem->produk->foto_produk ? asset('storage/'.$firstItem->produk->foto_produk) : asset('template/frontend/img/fruite-item-5.jpg') }}" width="50" class="rounded me-2">
                    <div>
                        <small class="d-block fw-bold">{{ $firstItem->produk->nama_produk }}</small>
                        @if($trx->details->count() > 1)
                            <small class="text-muted">+ {{ $trx->details->count() - 1 }} produk lainnya</small>
                        @else
                            <small class="text-muted">{{ $firstItem->jumlah }} x Rp {{ number_format($firstItem->harga_satuan, 0, ',', '.') }}</small>
                        @endif
                    </div>
                </div>
            </td>
            <td class="fw-bold">Rp {{ number_format($trx->total_harga, 0, ',', '.') }}</td>
            <td>
                @if($trx->status == 'menunggu_konfirmasi')
                    <span class="badge bg-warning text-dark"><i class="fas fa-clock me-1"></i> Menunggu Konfirmasi</span>
                @elseif($trx->status == 'diproses')
                    <span class="badge bg-info text-white"><i class="fas fa-cog me-1"></i> Sedang Diproses</span>
                @elseif($trx->status == 'dikirim')
                    <span class="badge bg-primary"><i class="fas fa-truck me-1"></i> Sedang Dikirim</span>
                @elseif($trx->status == 'selesai')
                    <span class="badge bg-success"><i class="fas fa-check-circle me-1"></i> Selesai</span>
                @else
                    <span class="badge bg-danger">Dibatalkan</span>
                @endif
            </td>
            <td>
                <div class="d-flex gap-2">
                    {{-- Tombol Detail (Trigger Modal) --}}
                    <button type="button" class="btn btn-sm btn-outline-secondary rounded-pill" data-bs-toggle="modal" data-bs-target="#detailModal{{ $trx->id }}">
                        Detail
                    </button>

                    {{-- LOGIKA TOMBOL SELESAI --}}
                    @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-sm btn-success rounded-pill">
                                <i class="fas fa-check me-1"></i> Diterima
                            </button>
                        </form>
                    @endif
                </div>

                {{-- MODAL DETAIL PESANAN --}}
                <div class="modal fade" id="detailModal{{ $trx->id }}" tabindex="-1" aria-hidden="true">
                    <div class="modal-dialog modal-lg">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">Detail Pesanan #{{ $trx->kode_invoice }}</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <p><strong>Status:</strong> {{ strtoupper($trx->status) }}</p>
                                <p><strong>Alamat Pengiriman:</strong><br>{{ $trx->alamat_pengiriman }}</p>
                                <hr>
                                <h6>Daftar Produk:</h6>
                                <ul class="list-group">
                                    @foreach($trx->details as $d)
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <img src="{{ $d->produk->foto_produk ? asset('storage/'.$d->produk->foto_produk) : asset('template/frontend/img/fruite-item-5.jpg') }}" width="40" class="me-2 rounded">
                                            <div>
                                                {{ $d->produk->nama_produk }}
                                                <small class="d-block text-muted">{{ $d->jumlah }} x Rp {{ number_format($d->harga_satuan, 0, ',', '.') }}</small>
                                            </div>
                                        </div>
                                        <span class="fw-bold">Rp {{ number_format($d->subtotal, 0, ',', '.') }}</span>
                                    </li>
                                    @endforeach
                                </ul>
                            </div>
                            <div class="modal-footer justify-content-between">
                                <span class="fw-bold fs-5">Total: Rp {{ number_format($trx->total_harga, 0, ',', '.') }}</span>
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection