@extends('layouts.user')

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

@section('content')
<style>
    body {
        font-family: 'Poppins', sans-serif;
        background: #f9f9f9;
        margin: 0;
        padding: 0;
        color: #333;
    }

    .header {
        background: #ff6b6b;
        color: white;
        padding: 20px;
        text-align: center;
        font-size: 24px;
        font-weight: bold;
        position: relative;
    }

    .header a {
        position: absolute;
        left: 20px;
        top: 20px;
        background: white;
        color: #ff6b6b;
        padding: 8px 12px;
        border-radius: 8px;
        text-decoration: none;
        font-weight: bold;
    }

    .header a:hover {
        background: #ffe0e0;
    }

    .content {
        max-width: 1000px;
        margin: 30px auto;
        padding: 0 15px;
    }

    .order-card {
        background: white;
        border-radius: 12px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        transition: 0.3s;
    }

    .order-card:hover {
        box-shadow: 0 6px 18px rgba(0,0,0,0.12);
    }

    .order-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
        font-weight: bold;
        font-size: 18px;
        color: #ff6b6b;
    }

    .order-details p {
        margin: 6px 0;
        line-height: 1.4;
    }

    .status {
        padding: 4px 10px;
        border-radius: 6px;
        font-weight: bold;
        display: inline-block;
        text-transform: capitalize;
    }

    .status.pending {
        background: #ffe066;
        color: #444;
    }

    .status.selesai {
        background: #81c784;
        color: white;
    }

    .btn-pay {
        background: #25D366;
        color: white;
        padding: 6px 12px;
        border-radius: 6px;
        text-decoration: none;
        font-weight: bold;
        display: inline-block;
        margin-top: 8px;
    }

    .btn-pay:hover {
        background: #128C7E;
    }
</style>

<div class="header">
    🍕 Riwayat Pesanan Anda
</div>

<div class="content">

    @forelse($orders as $order)
        <div class="order-card">

            <div class="order-header">
                <span>Pesanan {{ $order->id }}</span>
                <span class="status {{ $order->status }}">{{ $order->status }}</span>
            </div>

            <div class="order-details">

                {{-- Cegah error jika relasi pizza null --}}
                <p><strong>Pizza:</strong> 
                    {{ $order->pizza->nama ?? 'Data pizza tidak ditemukan' }}
                </p>

                <p><strong>Jumlah:</strong> {{ $order->jumlah }}</p>

                <p><strong>Total Harga:</strong> 
                    Rp {{ number_format($order->total_harga, 0, ',', '.') }}
                </p>

                <p><strong>Nama Penerima:</strong> {{ $order->nama_pelanggan}}</p>

                <p><strong>Alamat:</strong> {{ $order->alamat }}</p>

                <p><strong>Metode Pembayaran:</strong> 
                    {{ ucfirst($order->metode_pembayaran) }}
                </p>

                <p><strong>Tanggal:</strong> 
                    {{ $order->created_at->format('d M Y H:i') }}
                </p>

                {{-- Tombol Bayar --}}
                @if($order->status == 'pending' && $order->metode_pembayaran == 'qris')
                    <a href="{{ route('user.qris', $order->id) }}" class="btn-pay">
                        Bayar 💳
                    </a>
                @endif

                <form action="{{ route('user.order.delete', $order->id) }}"
      method="POST"
      style="margin-top: 5px;">
    @csrf
    @method('DELETE')

    <button type="submit" onclick="return confirm('Yakin ingin menghapus pesanan ini?')"
        style="
            background:#ff4d4d;
            color:white;
            border:none;
            padding:6px 10px;
            border-radius:6px;
            cursor:pointer;
            font-weight:bold;
        ">
        Hapus 🗑️
    </button>
</form>
            </div>
        </div>

    @empty
        <p style="text-align:center; margin-top:30px;">
            Belum ada pesanan 😋
        </p>
    @endforelse

</div>
@endsection
