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

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

        $this->data['module'] = 'Master EDP Stockpile 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'] = 'checker_batubara_kirim_jetty';
        $this->data['checker_batubara_kirim_jetty'] =  $this->Checker_batubara_kirim_jetty_model->get_all();
        $this->load->view('checker_batubara_kirim_jetty/checker_batubara_kirim_jetty', $this->data);
    }

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

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


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

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



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

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