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

class DataDesa extends CI_Controller
{

  public function __construct()
  {
    parent::__construct();
    // $this->load->library('notif');
    $this->load->model("ModelDataDesa");
  }

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

  function edit()
  {
    $id = $this->uri->segment(3);
    $datadesa = $this->ModelDataDesa->get_data_edit($id);
    $data = array(
      'body' => 'DataDesa/edit',
      'form' => 'DataDesa/form',
      'datadesa' => $datadesa,
    );
    $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_datadesa", $id);
    if ($this->db->update("data_desa", $data)) {
      // code...
      $this->session->set_flashdata("notif", $this->Notif->berhasil("Berhasil Input Data"));
      redirect(base_url() . "DataDesa");
    } else {
      $this->session->set_flashdata("notif", $this->Notif->gagal("gagal Input Data"));
      redirect(base_url() . "DataDesa");
    }
  }

  function delete()
  {
    $id = $this->input->post("id_datadesa");
    $this->db->where_in("id_datadesa", $id);
    if ($this->db->delete("data_desa")) {
      $this->session->set_flashdata("notif", $this->Notif->berhasil("Berhasil Input Data"));
      redirect(base_url() . "DataDesa");
      // code...
    } else {
      $this->session->set_flashdata("notif", $this->Notif->gagal("gagal Input Data"));
      redirect(base_url() . "DataDesa");
    }
  }
}