@extends('layouts.admin')
@section('content')
    <div class="container-fluid">
        <h3>Data Master Gapoktan</h3>

        {{-- Pesan Sukses/Error --}}
        @if (session('success'))
            <div class="alert alert-success">{{ session('success') }}</div>
        @endif
        @if ($errors->any())
            <div class="alert alert-danger">{{ $errors->first() }}</div>
        @endif

        <div class="row">
            {{-- Form Tambah Data --}}
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">Tambah Data</div>
                    <div class="card-body">
                        <form action="{{ route('admin.gapoktan.store') }}" method="POST">
                            @csrf
                            <div class="form-group mb-3">
                                <label>NIK</label>
                                <input type="number" name="nik" class="form-control" required
                                    placeholder="Masukkan 16 digit NIK">
                            </div>
                            <div class="form-group mb-3">
                                <label>Nama Petani / Kelompok</label>
                                <input type="text" name="nama" class="form-control" required
                                    placeholder="Masukkan Nama">
                            </div>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </form>
                    </div>
                </div>
            </div>

            {{-- Tabel Data --}}
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Daftar NIK Terdaftar</div>
                    <div class="card-body">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Tanggal Dibuat</th>
                                    <th>NIK</th>
                                    <th>Nama Petani/Kelompok</th>
                                    <th>Status Akun</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($gapoktan as $key => $item)
                                    <tr>
                                        <td>{{ $key + 1 }}</td>
                                        <td>{{ \Carbon\Carbon::parse($item->created_at)->translatedFormat('d F Y') }}</td>
                                        <td>{{ $item->nik }}</td>
                                        <td>{{ $item->nama }}</td>
                                        <td>
                                            @if ($item->petanis)
                                                <span class="badge bg-primary-green">Digunakan oleh
                                                    {{ $item->petanis->nama_lengkap }}</span>
                                            @else
                                                <span class="badge bg-warning text-dark">Belum Digunakan</span>
                                            @endif
                                        </td>
                                        <td>
                                            <form action="{{ route('admin.gapoktan.destroy', $item->id) }}" method="POST"
                                                onsubmit="return confirm('Yakin hapus data ini?')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-danger btn-sm">Hapus</button>
                                            </form>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
