@extends('layouts.admin')

@section('title', 'Monitoring Sistem')
@section('page-title', 'Monitoring Data')

@section('content')
    <div class="row">
        {{-- TABEL SEMUA TRANSAKSI --}}
        <div class="col-12 mb-4">
            <div class="card">
                <div class="card-header">
                    <h4>Semua Transaksi</h4>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle">
                            <thead>
                                <tr>
                                    <th>Invoice</th>
                                    <th>Petani / Usaha</th>
                                    <th>Pembeli</th>
                                    <th>Total</th>
                                    <th>Status</th>
                                    <th>Tanggal</th>
                                    <th>Aksi</th> 
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($transaksis as $trx)
                                    <tr>
                                        <td class="fw-bold text-primary">#{{ $trx->kode_invoice }}</td>
                                        <td>
                                            <div class="d-flex flex-column">
                                                <span class="fw-bold text-dark">
                                                    {{ $trx->petani->nama_lengkap ?? 'Petani Tidak Ditemukan' }}
                                                </span>
                                                <small class="text-muted" style="font-size: 0.85em;">
                                                    <i class="bi bi-shop me-1"></i>
                                                    {{ $trx->petani->nama_usaha ?? '-' }}
                                                </small>
                                            </div>
                                        </td>

                                        <td>{{ $trx->pembeli->nama_lengkap ?? 'Guest' }}</td>

                                        <td class="fw-bold">Rp {{ number_format($trx->total_harga, 0, ',', '.') }}</td>

                                        <td>
                                            @php
                                                $badgeClass = match ($trx->status) {
                                                    'selesai' => 'bg-success',
                                                    'batal' => 'bg-danger',
                                                    'dikirim' => 'bg-primary',
                                                    'diproses' => 'bg-info',
                                                    'menunggu konfirmasi' => 'bg-warning text-dark',
                                                    default => 'bg-secondary',
                                                };
                                            @endphp
                                            <span class="badge {{ $badgeClass }} rounded-pill px-3">
                                                {{ ucwords($trx->status) }}
                                            </span>
                                        </td>

                                        <td class="text-muted small">{{ $trx->created_at->format('d M Y') }}</td>

                                        <td>
                                            <a href="{{ route('admin.transaksi.detail', $trx->id) }}"
                                                class="btn btn-sm btn-info text-white" data-bs-toggle="tooltip"
                                                title="Lihat Detail">
                                                <i class="bi bi-eye"></i> Detail
                                            </a>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="7" class="text-center py-4 text-muted">
                                            <div class="d-flex flex-column align-items-center">
                                                <i class="bi bi-inbox fs-1 mb-2"></i>
                                                Belum ada transaksi baru.
                                            </div>
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        {{-- TABEL SEMUA PRODUK --}}
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4>Semua Produk Terdaftar</h4>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Nama Produk</th>
                                    <th>Pemilik (Petani)</th>
                                    <th>Harga</th>
                                    <th>Stok</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($produks as $p)
                                    <tr>
                                        <td>{{ $p->nama_produk }}</td>
                                        <td>{{ $p->petani->nama_lengkap }}</td>
                                        <td>Rp {{ number_format($p->harga, 0, ',', '.') }}</td>
                                        <td>{{ $p->stok }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    {{ $produks->links() }}
                </div>
            </div>
        </div>
    </div>
@endsection