<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\JenisTanahModel;

class JenisTanah extends BaseController
{
    protected $model;

    public function __construct()
    {
        $this->model = new JenisTanahModel();
    }

    public function index()
    {
        $data = [
            'title' => 'Jenis Tanah',
            'jenis_tanah' => $this->model->findAll()
        ];

        return view('admin/jenis_tanah', $data);
    }

    public function store()
    {
        $this->model->save([
            'jenis_tanah' => $this->request->getPost('jenis_tanah'),
            'skor'        => $this->request->getPost('skor'),
            'warna_peta'  => $this->request->getPost('warna_peta'),
        ]);

        return redirect()->to('/admin/jenis-tanah')
            ->with('success', 'Data berhasil ditambahkan');
    }

    public function update($id)
    {
        $this->model->update($id, [
            'jenis_tanah' => $this->request->getPost('jenis_tanah'),
            'skor'        => $this->request->getPost('skor'),
            'warna_peta'  => $this->request->getPost('warna_peta'),
        ]);

        return redirect()->to('/admin/jenis-tanah')
            ->with('success', 'Data berhasil diupdate');
    }

    public function delete($id)
    {
        $this->model->delete($id);

        return redirect()->to('/admin/jenis-tanah')
            ->with('success', 'Data berhasil dihapus');
    }
}
