<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Desa extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("ModelDesa");
    }

    function index()
    {
        $desa = $this->ModelDesa->get_data();
        $data = array(
            'body' => 'Desa/list',
            'desa' => $desa
        );
        $this->load->view('index', $data);
    }
    public function input()
    {
        $desa = $this->ModelDesa->get_data();
        $data = array(
            'body' => 'Desa/input',
            'form' => 'Desa/form',
            'desa' => $desa,
        );
        $this->load->view('index', $data);
    }
    public function insert()
    {
        $data = array(
            'nama_desa' => $this->input->post('nama_desa'),
        );
        if ($this->db->insert("desa", $data)) {
            $this->session->set_flashdata("notif", $this->MNotif->berhasil("Berhasil Input Data"));
            redirect(base_url() . "Desa");
        } else {
            $this->session->set_flashdata("notif", $this->MNotif->gagal("gagal Input Data"));
            redirect(base_url() . "Desa");
        }
        // echo json_encode($data);
    }

    function edit()
    {
        $id = $this->uri->segment(3);
        $desa = $this->ModelDesa->get_data_edit($id);
        $data = array(
            'body' => 'Desa/edit',
            'form' => 'Desa/form',
            'desa' => $desa,
        );
        $this->load->view('index', $data);
    }
    function update()
    {
        $id = $this->uri->segment(3);
        $data = array(
            'nama_desa' => $this->input->post('nama_desa'),
        );
        $this->db->where("id_desa", $id);
        if ($this->db->update("desa", $data)) {
            // code...
            $this->session->set_flashdata("notif", $this->MNotif->berhasil("Berhasil Input Data"));
            redirect(base_url() . "Desa");
        } else {
            $this->session->set_flashdata("notif", $this->MNotif->gagal("gagal Input Data"));
            redirect(base_url() . "Desa");
        }
    }

    function delete()
    {
        $id = $this->input->post("id_desa");
        $this->db->where_in("id_desa", $id);
        if ($this->db->delete("desa")) {
            $this->session->set_flashdata("notif", $this->MNotif->berhasil("Berhasil Hapus Data Desa"));
            redirect(base_url() . "Desa");
            // code...
        } else {
            $this->session->set_flashdata("notif", $this->MNotif->gagal("gagal Hapus Data Data"));
            redirect(base_url() . "Desa");
        }
    }
}