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

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

    $this->data['module'] = 'Master Unit 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'] = 'unit_batubara_kirim';
    $this->data['unit_batubara_kirim'] =  $this->Unit_batubara_kirim_model->get_all();
    $this->load->view('unit_batubara_kirim/unit_batubara_kirim', $this->data);
  }

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

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


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

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



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

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