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

class Form_batubara_masuk extends CI_Controller
{
  function __construct()
  {
    parent::__construct();

    $this->load->model('Form_batubara_masuk_model');
    //$this->load->model('Unit_batubara_masuk_model');
    $this->load->model('Asal_batubara_masuk_model');
    $this->load->model('Stockpile_batubara_masuk_model');
    $this->load->model('Kode_batubara_masuk_model');
    $this->load->model('Checker_model');

    $this->data['module'] = 'Form Batubara Masuk';
    if ($this->session->userdata('status') != "login") {
      redirect(base_url("login"));
    }
    if ($this->session->userdata('level') != "0" and $this->session->userdata('level') != "1") {
      redirect(base_url('error'));
    }
    $data_menu = "";
    $data_akses = $this->db->get_where('kategori_admin', 'id_user=' . $this->session->userdata('id_user'))->result();
    if (empty($data_akses)) {
      $data_menu = "0";
    } else {
      foreach ($data_akses as $data_akses) {

        $data_menu .= "" . $data_akses->id_jenis_form . ",";
      }
    }
    $data_menu = explode(",", substr($data_menu, 0, -1));


    if ($this->session->userdata('level') == 0 or (in_array(2, $data_menu))) {
    } else {
      redirect(base_url('error'));
    }
  }

  public function index()
  {
    $this->data['title'] = 'Data ' . $this->data['module'];
    $this->data['active'] = 'form_batubara_masuk';
    $this->data['form_batubara_masuk'] =  $this->Form_batubara_masuk_model->get_all();
    //$this->data['unit_batubara_masuk'] =  $this->Unit_batubara_masuk_model->get_all();
    $this->data['asal_batubara_masuk'] =  $this->Asal_batubara_masuk_model->get_all();
    $this->data['stockpile_batubara_masuk'] =  $this->Stockpile_batubara_masuk_model->get_all();
    $this->data['kode_batubara_masuk'] =  $this->Kode_batubara_masuk_model->get_all();
    $this->data['checker'] =  $this->Checker_model->get_all();
    $this->load->view('form_batubara_masuk/form_batubara_masuk', $this->data);
  }

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

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


  public function add()
  {
    $tanggal_batubara_masuk = $this->input->post('tanggal_batubara_masuk');
    $tgl1 = substr($tanggal_batubara_masuk, 0, 2);
    $bln1 = substr($tanggal_batubara_masuk, 3, 2);
    $thn1 = substr($tanggal_batubara_masuk, 6, 4);
    $tanggal_batubara_masuk = "$thn1-$bln1-$tgl1";
    $data = array(
      'tanggal_batubara_masuk'              => $tanggal_batubara_masuk,
      'shift_batubara_masuk'                => $this->input->post('shift_batubara_masuk'),
      'jam_batubara_masuk'                  => $this->input->post('jam_batubara_masuk'),
      'nomor_unit_batubara_masuk'              => $this->input->post('nomor_unit_batubara_masuk'),
      'id_asal_batubara_masuk'              => $this->input->post('id_asal_batubara_masuk'),
      'id_stockpile_batubara_masuk'         => $this->input->post('id_stockpile_batubara_masuk'),
      'id_kode_batubara_masuk'               => $this->input->post('id_kode_batubara_masuk'),
      'bruto_batubara_masuk'                => $this->input->post('bruto_batubara_masuk'),
      'tarra_batubara_masuk'                => $this->input->post('tarra_batubara_masuk'),
      'seam_batubara_masuk'                 => $this->input->post('seam_batubara_masuk'),
      'blok_batubara_masuk'                 => $this->input->post('blok_batubara_masuk'),
      'id_checker'                          => $this->input->post('id_checker'),
      'keterangan_batubara_masuk'           => $this->input->post('keterangan_batubara_masuk'),
    );

    if (!(empty($_FILES['file_upload']['name']))) {
      $ds = DIRECTORY_SEPARATOR;

      $filename = date('YmdHis');

      $config['upload_path']   = FCPATH . '/assets/images/batubara_masuk/';
      $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
      $config['file_name'] = $filename;
      $this->load->library('upload', $config);

      if ($this->upload->do_upload('file_upload')) {
        $nama = $this->upload->data('file_name');
        $data['file_upload'] = $nama;
      }
    }

    // eksekusi query INSERT
    $insert = $this->Form_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_batubara_masuk'));
    $tanggal_batubara_masuk = $this->input->post('tanggal_batubara_masuk');
    $tgl1 = substr($tanggal_batubara_masuk, 0, 2);
    $bln1 = substr($tanggal_batubara_masuk, 3, 2);
    $thn1 = substr($tanggal_batubara_masuk, 6, 4);
    $tanggal_batubara_masuk = "$thn1-$bln1-$tgl1";
    $data = array(
      'tanggal_batubara_masuk'              => $tanggal_batubara_masuk,
      'shift_batubara_masuk'                => $this->input->post('shift_batubara_masuk'),
      'jam_batubara_masuk'                  => $this->input->post('jam_batubara_masuk'),
      'nomor_unit_batubara_masuk'              => $this->input->post('nomor_unit_batubara_masuk'),
      'id_asal_batubara_masuk'              => $this->input->post('id_asal_batubara_masuk'),
      'id_stockpile_batubara_masuk'         => $this->input->post('id_stockpile_batubara_masuk'),
      'id_kode_batubara_masuk'         => $this->input->post('id_kode_batubara_masuk'),
      'bruto_batubara_masuk'                => $this->input->post('bruto_batubara_masuk'),
      'tarra_batubara_masuk'                => $this->input->post('tarra_batubara_masuk'),
      'seam_batubara_masuk'                 => $this->input->post('seam_batubara_masuk'),
      'blok_batubara_masuk'                 => $this->input->post('blok_batubara_masuk'),
      'id_checker'                          => $this->input->post('id_checker'),
      'keterangan_batubara_masuk'           => $this->input->post('keterangan_batubara_masuk'),
    );


    if (!(empty($_FILES['file_upload']['name']))) {
      $ds = DIRECTORY_SEPARATOR;

      $filename = date('YmdHis');

      $config['upload_path']   = FCPATH . '/assets/images/batubara_masuk/';
      $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
      $config['file_name'] = $filename;
      $this->load->library('upload', $config);

      if ($this->upload->do_upload('file_upload')) {
        $foto_old = $this->Form_batubara_masuk_model->get_by_id($id)->file_upload;
        if (file_exists(FCPATH . "assets/images/batubara_masuk/$foto_old") and (!($foto_old == ""))) {
          unlink(FCPATH . "assets/images/batubara_masuk/$foto_old");
        }
        $nama = $this->upload->data('file_name');
        $data['file_upload'] = $nama;
      }
    }



    // eksekusi query
    $this->Form_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'));
    $foto_old = $this->Form_batubara_masuk_model->get_by_id($id)->file_upload;
    if (file_exists(FCPATH . "assets/images/batubara_masuk/$foto_old") and (!($foto_old == ""))) {
      unlink(FCPATH . "assets/images/batubara_masuk/$foto_old");
    }
    $this->Form_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['form_batubara_masuk'] = $this->Form_batubara_masuk_model->get_by_id($id);
    $this->data['title'] = 'Data ' . $this->data['module'];
    $this->load->view('form_batubara_masuk/' . $form_open, $this->data);

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

  public function report()
  {
    ob_start();
    $tanggal_report_awal = $this->input->post('tanggal_report_awal');
    $tgl1 = substr($tanggal_report_awal, 0, 2);
    $bln1 = substr($tanggal_report_awal, 3, 2);
    $thn1 = substr($tanggal_report_awal, 6, 4);
    $tanggal_report_awal = "$thn1-$bln1-$tgl1";

    $tanggal_report_akhir = $this->input->post('tanggal_report_akhir');
    $tgl1 = substr($tanggal_report_akhir, 0, 2);
    $bln1 = substr($tanggal_report_akhir, 3, 2);
    $thn1 = substr($tanggal_report_akhir, 6, 4);
    $tanggal_report_akhir = "$thn1-$bln1-$tgl1";

    $this->data['form_batubara_masuk'] = $this->Form_batubara_masuk_model->get_data_report($tanggal_report_awal, $tanggal_report_akhir);
    $this->data['tanggal_report_awal'] = $tanggal_report_awal;
    $this->data['tanggal_report_akhir'] = $tanggal_report_akhir;

    $this->load->view('form_batubara_masuk/report', $this->data);

    $html = ob_get_contents();
    //      $html = '<title style="font-family: freeserif">'.nl2br($html).'</title>';
    ob_end_clean();

    require_once('application/libraries/html2pdf/html2pdf.class.php');
    $pdf = new HTML2PDF('P', 'F4', 'en', true, 'UTF-8', array(20, 15, 20, 15));
    $pdf->setDefaultFont('Arial');
    $pdf->setTestTdInOnePage(false);
    $pdf->WriteHTML($html);
    $pdf->Output('Laporan Batubara Masuk.pdf');
  }
}
