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

class Asal_batubara_masuk extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('Asal_batubara_masuk_model');

    $this->data['module'] = 'Asal Batubara Masuk';
    if ($this->session->userdata('status') != "login") {
      redirect(base_url("login"));
    }
    if ($this->session->userdata('level') != "0" and $this->session->userdata('level') != "2") {
      redirect(base_url('error'));
    }
  }

  public function index()
  {
    $this->data['title'] = 'Data ' . $this->data['module'];
    $this->data['active'] = 'asal_batubara_masuk';
    $this->data['asal_batubara_masuk'] =  $this->Asal_batubara_masuk_model->get_all();
    $this->load->view('asal_batubara_masuk/asal_batubara_masuk', $this->data);
  }

  public function ajax_list()
  {
    //get_datatables terletak di model
    $list = $this->Asal_batubara_masuk_model->get_datatables();
    $data = array();
    $no = $_POST['start'];

    // Membuat loop/ perulangan
    foreach ($list as $data_asal_batubara_masuk) {
      $no++;
      $row = array();
      $row[] = '<p style="text-align: center">' . $no . '</p>';
      $row[] = '<p style="text-align: left">' . $data_asal_batubara_masuk->nama_asal_batubara_masuk . '</p>';

      // Penambahan tombol edit dan hapus
      if ($this->session->userdata('id_user_group') == "2") {
        $row[] = '
      <p style="text-align: center">
      	<a class="btn btn-sm btn-warning" onclick="open_modal(\'Edit Data asal_batubara_masuk\',\'form_edit\',\'' . encrypt($data_asal_batubara_masuk->id_asal_batubara_masuk) . '\')" title="Edit"><i class="fa fa-pencil"></i></a>
        <a class="btn btn-sm btn-danger" href="' . base_url('asal_batubara_masuk/delete/') . encrypt($data_asal_batubara_masuk->id_asal_batubara_masuk) . '" title="Hapus" onclick="javasciprt: return confirm(\'Apakah Anda yakin ingin menghapus ?\')"><i class="fa fa-remove"></i></a>
			</p>';
      } else {
        $row[] = '';
      }

      $data[] = $row;
    }

    $output = array(
      "draw" => $_POST['draw'],
      "recordsTotal" => $this->Asal_batubara_masuk_model->count_all(),
      "recordsFiltered" => $this->Asal_batubara_masuk_model->count_filtered(),
      "data" => $data
    );
    //output to json format
    echo json_encode($output);
  }


  public function add()
  {
    $data = array(
      'nama_asal_batubara_masuk'          => $this->input->post('nama_asal_batubara_masuk'),
    );

    // eksekusi query INSERT
    $insert = $this->Asal_batubara_masuk_model->insert($data);
    // set pesan data berhasil dibuat
    $this->session->set_flashdata('message', '
        <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
          <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil disimpan
        </div>');
    echo "1";
  }

  public function update()
  {
    $id = decrypt($this->input->post('id_asal_batubara_masuk'));
    $data = array(
      'nama_asal_batubara_masuk'          => $this->input->post('nama_asal_batubara_masuk'),
    );



    // eksekusi query
    $this->Asal_batubara_masuk_model->update($id, $data);
    // set pesan data berhasil dibuat
    $this->session->set_flashdata('message', '
          <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
            <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil diperbaharui
          </div>');
    echo "1";
  }

  public function delete()
  {
    $id = decrypt($this->input->post('id'));
    $this->Asal_batubara_masuk_model->delete($id);

    $this->session->set_flashdata('message', '
      <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
        <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil dihapus
      </div>');
    echo "1";
  }

  public function form_open()
  {
    $form_open = $this->input->post('form_open');
    $id = decrypt($this->input->post('id'));
    $this->data['asal_batubara_masuk'] = $this->Asal_batubara_masuk_model->get_by_id($id);
    $this->data['title'] = 'Data ' . $this->data['module'];
    $this->load->view('asal_batubara_masuk/' . $form_open, $this->data);

    //    $this->load->view('asal_batubara_masuk/'.$form_open, $id);
  }
  function get_data()
  {
    $id = decrypt($this->input->post('id'));
    $data = $this->Asal_batubara_masuk_model->get_by_id($id);
    echo json_encode($data);
  }
}
