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

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

    $this->data['module'] = 'kode Batubara kirim';
    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'] = 'kode_batubara_kirim';
    $this->data['kode_batubara_kirim'] =  $this->Kode_batubara_kirim_model->get_all();
    $this->load->view('kode_batubara_kirim/kode_batubara_kirim', $this->data);
  }

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

    // Membuat loop/ perulangan
    foreach ($list as $data_kode_batubara_kirim) {
      $no++;
      $row = array();
      $row[] = '<p style="text-align: center">' . $no . '</p>';
      $row[] = '<p style="text-align: left">' . $data_kode_batubara_kirim->nama_kode_batubara_kirim . '</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 kode_batubara_kirim\',\'form_edit\',\'' . encrypt($data_kode_batubara_kirim->id_kode_batubara_kirim) . '\')" title="Edit"><i class="fa fa-pencil"></i></a>
        <a class="btn btn-sm btn-danger" href="' . base_url('kode_batubara_kirim/delete/') . encrypt($data_kode_batubara_kirim->id_kode_batubara_kirim) . '" 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->Kode_batubara_kirim_model->count_all(),
      "recordsFiltered" => $this->Kode_batubara_kirim_model->count_filtered(),
      "data" => $data
    );
    //output to json format
    echo json_encode($output);
  }


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

    // eksekusi query INSERT
    $insert = $this->Kode_batubara_kirim_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_kode_batubara_kirim'));
    $data = array(
      'nama_kode_batubara_kirim'          => $this->input->post('nama_kode_batubara_kirim'),
    );



    // eksekusi query
    $this->Kode_batubara_kirim_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->Kode_batubara_kirim_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['kode_batubara_kirim'] = $this->Kode_batubara_kirim_model->get_by_id($id);
    $this->data['title'] = 'Data ' . $this->data['module'];
    $this->load->view('kode_batubara_kirim/' . $form_open, $this->data);

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