@extends('layouts.admin')

@section('title', 'Pesanan Masuk')
@section('page-title', 'Daftar Pesanan Masuk')

@section('content')
<div class="card">
    <div class="card-header">
        <h4>Kelola Pesanan</h4>
    </div>
    <div class="card-body">
        @if(session('success'))
            <div class="alert alert-success">{{ session('success') }}</div>
        @endif

        <div class="table-responsive">
            <table class="table table-hover" id="table1">
                <thead>
                    <tr>
                        <th>Invoice</th>
                        <th>Pembeli</th>
                        <th>Barang</th>
                        <th>Alamat</th>
                        <th>Total</th>
                        <th>Status</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($pesanans as $pesanan)
                    <tr>
                        <td class="text-primary font-bold">#{{ $pesanan->kode_invoice }}</td>
                        <td>
                            {{ $pesanan->pembeli->nama_lengkap }}<br>
                            <small class="text-muted">{{ $pesanan->pembeli->no_hp }}</small>
                        </td>
                        <td>
                            <ul class="list-unstyled mb-0">
                                @foreach($pesanan->details as $detail)
                                    <li>- {{ $detail->produk->nama_produk }} ({{ $detail->jumlah }})</li>
                                @endforeach
                            </ul>
                        </td>
                        <td><small>{{ Str::limit($pesanan->alamat_pengiriman, 30) }}</small></td>
                        <td class="font-bold">Rp {{ number_format($pesanan->total_harga, 0, ',', '.') }}</td>
                        <td>
                            @if($pesanan->status == 'menunggu_konfirmasi')
                                <span class="badge bg-warning">Perlu Konfirmasi</span>
                            @elseif($pesanan->status == 'diproses')
                                <span class="badge bg-info">Diproses</span>
                            @elseif($pesanan->status == 'dikirim')
                                <span class="badge bg-primary">Dikirim</span>
                            @elseif($pesanan->status == 'selesai')
                                <span class="badge bg-success">Selesai</span>
                            @else
                                <span class="badge bg-danger">Batal</span>
                            @endif
                        </td>
                        <td>
                            <div class="d-flex gap-2">
                                <a href="{{ route('petani.pesanan.detail', $pesanan->id) }}" class="btn btn-sm btn-info text-white" title="Lihat Detail">
                                    <i class="bi bi-eye"></i> Detail
                                </a>
                                {{-- Tombol Aksi Status --}}
                                @if($pesanan->status == 'menunggu_konfirmasi')
                                <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST" class="d-inline">
                                    @csrf @method('PATCH')
                                    <input type="hidden" name="status" value="diproses">
                                    <button class="btn btn-sm btn-success" title="Terima Pesanan"><i class="bi bi-check-lg"></i></button>
                                </form>
                                <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST" class="d-inline">
                                    @csrf @method('PATCH')
                                    <input type="hidden" name="status" value="batal">
                                    <button class="btn btn-sm btn-danger" title="Tolak Pesanan" onclick="return confirm('Tolak pesanan ini?')"><i class="bi bi-x-lg"></i></button>
                                </form>
                            @elseif($pesanan->status == 'diproses')
                                <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST">
                                    @csrf @method('PATCH')
                                    <input type="hidden" name="status" value="dikirim">
                                    <button class="btn btn-sm btn-primary" title="Kirim Barang"><i class="bi bi-truck"></i> Kirim</button>
                                </form>
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="text-center">Belum ada pesanan masuk.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection