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

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

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

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

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


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

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



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

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