<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar Pesanan | Admin</title>

    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background: #fff8f0;
            color: #000000ff;
            margin: 0;
            padding: 0;
        }

        header {
            background: #ff4d4d;
            color: white;
            text-align: center;
            padding: 20px 0;
            font-size: 22px;
            letter-spacing: 1px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }

        .container {
            width: 90%;
            max-width: 1000px;
            margin: 40px auto;
            background: white;
            padding: 25px;
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }

        a {
            text-decoration: none;
            color: #ff4d4d;
            font-weight: bold;
        }

        a:hover {
            color: #e63946;
        }

        .nav {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            border-radius: 10px;
            overflow: hidden;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 12px 10px;
            text-align: center;
        }

        th {
            background-color: #ffad6fff;
            color: #ffffffff;
            text-transform: uppercase;
        }

        tr:nth-child(even) {
            background-color: #ffffffff;
        }

        tr:hover {
            background-color: #ffeeb3;
            transition: 0.3s;
        }

        .btn-danger {
            background-color: #dc3545;
            padding: 6px 10px;
            border-radius: 6px;
            color: white;
        }

        .btn-danger:hover {
            background-color: #c82333;
        }

        .btn-disabled {
            background-color: #9e9e9e;
            color: white;
            padding: 6px 10px;
            border-radius: 6px;
            cursor: not-allowed;
        }

        select {
            padding: 6px 8px;
            border-radius: 6px;
            border: 1px solid #ccc;
        }

        button {
            background: #ff4d4d;
            color: white;
            border: none;
            padding: 6px 10px;
            border-radius: 6px;
            cursor: pointer;
            margin-left: 5px;
            transition: 0.3s;
        }

        button:hover {
            background: #e63946;
        }
    </style>
</head>

<body>
    <header>
        🍕 Daftar Pesanan Pelanggan
    </header>

    <div class="container">
        <div class="nav">
            <div>
                <a href="/admin/dashboard">🏠 Dashboard</a>
            </div>
            <div>
                <a href="/logout">🚪 Logout</a>
            </div>
        </div>

        <table>
            <tr>
                <th>No</th>
                <th>Nama User</th>
                <th>Pizza</th>
                <th>Jumlah</th>
                <th>Total Harga</th>
                <th>Status</th>
                <th>Aksi</th>
            </tr>

            @foreach($orders as $order)
            <tr>
                <td>{{ $loop->iteration }}</td>
                <td>{{ $order->nama_pelanggan ?? $order->user->name ?? '-' }}</td>
                <td>{{ $order->pizza->nama_pizza ?? 'Tidak Diketahui' }}</td>
                <td>{{ $order->jumlah }}</td>
                <td>Rp {{ number_format($order->total_harga, 0, ',', '.') }}</td>
                <td>
                    <span style="color:
                        {{ $order->status == 'pending' ? '#ff8800' :
                           ($order->status == 'proses' ? '#007bff' : '#28a745') }};
                        font-weight: bold;">
                        {{ ucfirst($order->status) }}
                    </span>
                </td>

                <td>
                    {{-- Jika status belum selesai → boleh diubah --}}
                    @if($order->status !== 'selesai')
                        <form action="{{ route('admin.orders.update', $order->id) }}" 
                              method="POST" 
                              style="display:inline;">
                            @csrf
                            <select name="status">
                                <option value="pending" {{ $order->status == 'pending' ? 'selected' : '' }}>Pending</option>
                                <option value="proses" {{ $order->status == 'proses' ? 'selected' : '' }}>Proses</option>
                                <option value="selesai" {{ $order->status == 'selesai' ? 'selected' : '' }}>Selesai</option>
                            </select>
                            <button type="submit">Ubah</button>
                        </form>
                    @else
                        {{-- Jika selesai → ubah disable --}}
                        <span class="btn-disabled">✔ Selesai</span>
                    @endif

                    {{-- Tombol Hapus --}}
                    <form action="{{ route('admin.orders.destroy', $order->id) }}" 
                          method="POST" 
                          style="display:inline;"
                          onsubmit="return confirm('Yakin ingin menghapus pesanan ini?')">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn-danger">🗑 Hapus</button>
                    </form>

                </td>
            </tr>
            @endforeach
        </table>
    </div>
</body>
</html>
