@extends('layouts.admin')

@section('title', 'Kelola Produk')
@section('page-title', 'Daftar Produk Saya')

@section('content')
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4 class="card-title">List Produk Pertanian</h4>
            <a href="{{ route('petani.produk.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-lg"></i> Tambah Produk
            </a>
        </div>
        <div class="card-body">
            @if(session('success'))
                <div class="alert alert-light-success alert-dismissible fade show" role="alert">
                    <i class="bi bi-check-circle"></i> {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            <div class="table-responsive">
                <table class="table table-hover table-lg">
                    <thead>
                        <tr>
                            <th>Foto</th>
                            <th>Nama Produk</th>
                            <th>Harga (Rp)</th>
                            <th>Stok</th>
                            <th class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($produks as $produk)
                            <tr>
                                <td class="col-1">
                                    @if($produk->foto_produk)
                                        <img src="{{ asset('storage/' . $produk->foto_produk) }}" alt="{{ $produk->nama_produk }}"
                                            class="rounded" style="width: 80px; height: 80px; object-fit: cover;">
                                    @else
                                        <span class="badge bg-secondary">No Image</span>
                                    @endif
                                </td>
                                <td>
                                    <p class="font-bold mb-0">{{ $produk->nama_produk }}</p>
                                    <small class="text-muted">{{ Str::limit($produk->deskripsi, 50) }}</small>
                                </td>
                                <td>Rp {{ number_format($produk->harga, 0, ',', '.') }}</td>
                                <td>
                                    @if($produk->stok > 0)
                                        <span class="badge bg-success">{{ $produk->stok }} kg</span>
                                    @else
                                        <span class="badge bg-danger">Habis</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    {{-- Tombol Edit --}}
                                    <a href="{{ route('petani.produk.edit', $produk->id) }}"
                                        class="btn btn-sm btn-warning me-1">
                                        <i class="bi bi-pencil-square"></i>
                                    </a>

                                    {{-- Form Hapus --}}
                                    <form action="{{ route('petani.produk.destroy', $produk->id) }}" method="POST"
                                        class="d-inline" onsubmit="return confirm('Yakin ingin menghapus produk ini?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-danger">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="text-center py-5">
                                    <img src="{{ asset('template/admin/static/images/samples/error-404.svg') }}" width="150"
                                        class="mb-3">
                                    <p class="text-muted">Belum ada produk yang dijual.</p>
                                    <a href="{{ route('petani.produk.create') }}" class="btn btn-primary btn-sm">Tambah Produk
                                        Sekarang</a>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection