<?php

namespace App\Controllers;

use App\Models\PetaniModel;

class Petani extends BaseController
{
    protected $petaniM;
    public function __construct()
    {
        $this->petaniM = new PetaniModel();
    }

    public function index()
    {
        $data = ['title' => 'Data Petani', 'petani' => $this->petaniM->findAll()];
        return view('petani/index', $data);
    }

    public function simpan()
    {
        $this->petaniM->save($this->request->getPost());
        return redirect()->to('/petani')->with('success', 'Data petani <b>berhasil ditambahkan</b> ke sistem.');
    }

    public function update($id)
    {
        $this->petaniM->update($id, $this->request->getPost());
        return redirect()->to('/petani')->with('success', 'Perubahan data petani <b>berhasil disimpan</b>.');
    }

    public function hapus($id)
    {
        $this->petaniM->delete($id);
        return redirect()->to('/petani')->with('success', 'Data petani <b>telah dihapus</b> dari database.');
    }
}
