@extends('layouts.admin')

@section('title', 'Dashboard Admin')
@section('page-title', 'Overview Sistem')

@section('content')
    <section class="row">
        <div class="col-12">
            <div class="row">
                {{-- Statistik Petani Aktif --}}
                <div class="col-6 col-lg-3 col-md-6">
                    <div class="card">
                        <div class="card-body px-3 py-4-5">
                            <div class="row">
                                <div class="col-md-4 d-flex justify-content-center align-items-center">
                                    <div class="stats-icon purple"><i class="bi bi-people-fill"></i></div>
                                </div>
                                <div class="col-md-8">
                                    <h6 class="text-muted font-semibold">Petani Aktif</h6>
                                    <h6 class="font-extrabold mb-0">{{ $totalPetani }}</h6>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Statistik Menunggu Verifikasi --}}
                <div class="col-6 col-lg-3 col-md-6">
                    <div class="card">
                        <div class="card-body px-3 py-4-5">
                            <div class="row">
                                <div class="col-md-4 d-flex justify-content-center align-items-center">
                                    <div class="stats-icon red"><i class="bi bi-person-plus-fill"></i></div>
                                </div>
                                <div class="col-md-8">
                                    <h6 class="text-muted font-semibold">Verifikasi Masuk</h6>
                                    <h6 class="font-extrabold mb-0">{{ $petaniPending }}</h6>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Statistik Total Produk --}}
                <div class="col-6 col-lg-3 col-md-6">
                    <div class="card">
                        <div class="card-body px-3 py-4-5">
                            <div class="row">
                                <div class="col-md-4 d-flex justify-content-center align-items-center">
                                    <div class="stats-icon green"><i class="bi bi-basket-fill"></i></div>
                                </div>
                                <div class="col-md-8">
                                    <h6 class="text-muted font-semibold">Total Produk</h6>
                                    <h6 class="font-extrabold mb-0">{{ $totalProduk }}</h6>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Statistik Total Transaksi --}}
                <div class="col-6 col-lg-3 col-md-6">
                    <div class="card">
                        <div class="card-body px-3 py-4-5">
                            <div class="row">
                                <div class="col-md-4 d-flex justify-content-center align-items-center">
                                    <div class="stats-icon blue"><i class="bi bi-receipt"></i></div>
                                </div>
                                <div class="col-md-8">
                                    <h6 class="text-muted font-semibold">Total Transaksi</h6>
                                    <h6 class="font-extrabold mb-0">{{ $totalTransaksi }}</h6>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            {{-- Tabel Ringkasan Transaksi --}}
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h4>Transaksi Terbaru di Platform</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>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @forelse($transaksiTerbaru 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>
                                            </tr>
                                        @empty
                                            <tr>
                                                <td colspan="6" 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 class="text-center mt-3">
                                <a href="{{ route('admin.monitoring') }}" class="btn btn-sm btn-primary">
                                    Lihat Semua Data
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection