<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Gapoktan;

class GapoktanController extends Controller
{
    public function index()
    {
        $gapoktan = Gapoktan::with('petanis')->get();
        return view('admin.gapoktan.index', compact('gapoktan')); 
    }

    public function store(Request $request)
    {
        $request->validate([
            'nik' => 'required|unique:gapoktans,nik|max:16',
            'nama' => 'required|string|max:255',
        ], [
            'nik.unique' => 'NIK ini sudah terdaftar di sistem.',
        ]);

        Gapoktan::create([
            'nik' => $request->nik,
            'nama' => $request->nama,
        ]);

        return redirect()->back()->with('success', 'Data Gapoktan berhasil ditambahkan!');
    }

    // Menghapus data
    public function destroy($id)
    {
        Gapoktan::findOrFail($id)->delete();
        return redirect()->back()->with('success', 'Data berhasil dihapus!');
    }
}