@extends('layouts.app')

@section('title', 'Jatah RT / Kelompok - Sistem Distribusi Qurban')

@section('header_title', 'Jatah RT / Kelompok')
@section('header_subtitle', 'Kelola alokasi paket qurban kelompok, institusi, atau RT setempat')

@section('content')
<div class="panel">
    <div class="panel-header">
        <h2 class="panel-title"><span>👥</span> Distribusi Kelompok & RT</h2>
    </div>

    <!-- Filters -->
    <form action="{{ route('jatah.index') }}" method="GET" class="filter-bar">
        <input type="text" name="search" class="form-control" placeholder="Cari RT / Nama Kelompok (contoh: RT 03)..." value="{{ request('search') }}">

        <select name="status" class="form-control">
            <option value="">-- Semua Status --</option>
            <option value="belum_diambil" {{ request('status') == 'belum_diambil' ? 'selected' : '' }}>Belum Diambil</option>
            <option value="diambil" {{ request('status') == 'diambil' ? 'selected' : '' }}>Sudah Diambil</option>
        </select>

        <button type="submit" class="btn btn-primary btn-sm">Filter</button>
        @if(request()->anyFilled(['search', 'status']))
            <a href="{{ route('jatah.index') }}" class="btn btn-secondary btn-sm">Reset</a>
        @endif
    </form>

    <!-- Table of Jatah RT -->
    @if ($jatahs->isEmpty())
        <div style="text-align: center; padding: 3rem 0; color: var(--text-muted);">
            <span style="font-size: 3rem; display: block; margin-bottom: 1rem;">👥</span>
            Tidak ditemukan jatah kelompok yang sesuai.
        </div>
    @else
        <div class="table-wrapper">
            <table class="table-custom">
                <thead>
                    <tr>
                        <th>Tujuan / Kelompok</th>
                        <th>Tahun</th>
                        <th>Alokasi Sapi</th>
                        <th>Alokasi Kambing</th>
                        <th>Status</th>
                        <th>Terakhir Diperbarui</th>
                        <th style="text-align: center;">Ubah Status</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($jatahs as $j)
                        <tr>
                            <td>
                                <strong style="color: var(--primary-dark); font-size: 1.05rem;">{{ $j->tujuan }}</strong>
                            </td>
                            <td style="font-weight: 600; color: var(--text-muted);">{{ $j->tahun }}</td>
                            <td>
                                <div style="font-size: 0.85rem;">
                                    🐮 Daging: <strong>{{ $j->daging_sapi }}</strong> porsi<br>
                                    🦴 Tulang: <strong>{{ $j->tulang_sapi }}</strong> porsi
                                </div>
                            </td>
                            <td>
                                <div style="font-size: 0.85rem;">
                                    🐐 Daging: <strong>{{ $j->daging_kambing }}</strong> porsi<br>
                                    🦴 Tulang: <strong>{{ $j->tulang_kambing }}</strong> porsi
                                </div>
                            </td>
                            <td>
                                @if ($j->status === 'diambil')
                                    <span class="badge badge-success">Sudah Diambil</span>
                                @else
                                    <span class="badge badge-danger">Belum Diambil</span>
                                @endif
                            </td>
                            <td style="font-size: 0.8rem; color: var(--text-muted);">
                                {{ $j->updated_at->format('d M Y, H:i') }}
                            </td>
                            <td style="text-align: center;">
                                @if ($j->status === 'diambil')
                                    <form action="{{ route('jatah.update-status', $j->id) }}" method="POST" style="display: inline;">
                                        @csrf
                                        <input type="hidden" name="status" value="belum_diambil">
                                        <button type="submit" class="btn btn-secondary btn-sm" style="background-color: var(--danger-bg); color: #b91c1c; border-color: #fca5a5;">
                                            ↩ Set Belum Diambil
                                        </button>
                                    </form>
                                @else
                                    <form action="{{ route('jatah.update-status', $j->id) }}" method="POST" style="display: inline;">
                                        @csrf
                                        <input type="hidden" name="status" value="diambil">
                                        <button type="submit" class="btn btn-primary btn-sm" style="background-color: var(--success); border-color: var(--success);">
                                            ✔ Set Sudah Diambil
                                        </button>
                                    </form>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        <div class="pagination-wrapper">
            <div style="font-size: 0.85rem; color: var(--text-muted);">
                Menampilkan {{ $jatahs->firstItem() }} - {{ $jatahs->lastItem() }} dari {{ $jatahs->total() }} Jatah Kelompok
            </div>
            <div>
                {{ $jatahs->links() }}
            </div>
        </div>
    @endif
</div>
@endsection
