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

class Form_batubara_kirim extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('Tujuan_batubara_kirim_model');
    $this->load->model('Kode_batubara_kirim_model');
    $this->load->model('Checker_model');
    $this->load->model('Checker_batubara_kirim_model');
    $this->load->model('Form_batubara_kirim_model');
    $this->load->model('Stockpile_batubara_masuk_model');

    $this->data['module'] = 'Form Batubara Kirim';
    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(4, $data_menu))) {
    } else {
      redirect(base_url('error'));
    }
  }

  public function index()
  {
    $this->data['title'] = 'Data ' . $this->data['module'];
    $this->data['active'] = 'form_batubara_kirim';
    $this->data['stockpile_batubara_masuk'] =  $this->Stockpile_batubara_masuk_model->get_all();
    $this->data['tujuan_batubara_kirim'] =  $this->Tujuan_batubara_kirim_model->get_all();
    $this->data['kode_batubara_kirim'] =  $this->Kode_batubara_kirim_model->get_all();
    $this->data['checker'] =  $this->Checker_model->get_all();
    $this->data['checker_batubara_kirim'] =  $this->Checker_batubara_kirim_model->get_all();
    $this->data['form_batubara_kirim'] =  $this->Form_batubara_kirim_model->get_all();
    $this->load->view('form_batubara_kirim/form_batubara_kirim', $this->data);
  }

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

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


  public function add()
  {
    $this->db->order_by('id_batubara_kirim', 'asc');
    $up_data = $this->db->get_where('form_batubara_kirim', ['tanggal_batubara_kirim' => '2020-01-21'])->result_array();
    $i = 1;
    foreach ($up_data as $row) {
      $this->db->set('nomor_batubara_baru', $i);
      $this->db->where('id_batubara_kirim', $row['id_batubara_kirim']);
      $this->db->update('form_batubara_kirim');
      $i++;
    }



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

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

    $nomor_urut = ($this->Form_batubara_kirim_model->get_nomor_max($tanggal_batubara_kirim)->nomor_urut_batubara_kirim) + 1;
    $date_now = date('Y-m-d');
    $this->db->order_by('id_batubara_kirim', 'desc');
    $this->db->limit(1);
    $query1 = $this->db->get_where('form_batubara_kirim', ['tanggal_batubara_kirim' => $date_now])->row_array();
    if ($tanggal_batubara_kirim == $date_now) {
      $no_baru = $query1['nomor_batubara_baru'] + 1;
    } else {
      $this->db->order_by('id_batubara_kirim', 'desc');
      $this->db->limit(1);
      $query2 = $this->db->get_where('form_batubara_kirim', ['tanggal_batubara_kirim' => $tgl_kirim])->row_array();
      $no_baru = $query2['nomor_batubara_baru'] + 1;
    }
    $data = array(
      'unit_batubara_kirim'          => $this->input->post('unit_batubara_kirim'),
      'nomor_urut_batubara_kirim'          => $nomor_urut,
      'nomor_batubara_baru'          => $no_baru,
      'nomor_polisi_batubara_kirim'          => $this->input->post('nomor_polisi_batubara_kirim'),
      'id_asal_batubara_kirim'          => $this->input->post('id_asal_batubara_kirim'),
      'id_tujuan_batubara_kirim'          => $this->input->post('id_tujuan_batubara_kirim'),
      'shift_batubara_kirim'          => $this->input->post('shift_batubara_kirim'),
      'bruto_batubara_kirim'          => $this->input->post('bruto_batubara_kirim'),
      'tarra_batubara_kirim'          => $this->input->post('tarra_batubara_kirim'),
      'tanggal_batubara_kirim'          => $tanggal_batubara_kirim,
      'jam_batubara_kirim'          => $this->input->post('jam_batubara_kirim'),
      // 'tanggal_terima'          => $tanggal_terima,
      // 'jam_terima'          => $this->input->post('jam_terima'),
      'nomor_sj_batubara_kirim'          => $this->input->post('nomor_sj_batubara_kirim'),
      'id_kode_batubara_kirim'          => $this->input->post('id_kode_batubara_kirim'),
      'id_checker'       => $this->input->post('id_checker'),
      'id_checker_batubara_kirim'       => $this->input->post('id_checker_batubara_kirim'),
      'keterangan_batubara_kirim'          => $this->input->post('keterangan_batubara_kirim'),
    );

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

      $filename = date('YmdHis');

      $config['upload_path']   = FCPATH . '/assets/images/batubara_kirim/';
      $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_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_batubara_kirim'));
    $tanggal_batubara_kirim = $this->input->post('tanggal_batubara_kirim');
    $tgl1 = substr($tanggal_batubara_kirim, 0, 2);
    $bln1 = substr($tanggal_batubara_kirim, 3, 2);
    $thn1 = substr($tanggal_batubara_kirim, 6, 4);
    $tanggal_batubara_kirim = "$thn1-$bln1-$tgl1";

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

    $data = array(
      'unit_batubara_kirim'          => $this->input->post('unit_batubara_kirim'),
      'nomor_urut_batubara_kirim'       => $nomor_urut,
      'nomor_batubara_baru'             => $no_baru,
      'nomor_polisi_batubara_kirim'     => $this->input->post('nomor_polisi_batubara_kirim'),
      'id_asal_batubara_kirim'          => $this->input->post('id_asal_batubara_kirim'),
      'id_tujuan_batubara_kirim'        => $this->input->post('id_tujuan_batubara_kirim'),
      'shift_batubara_kirim'            => $this->input->post('shift_batubara_kirim'),
      'bruto_batubara_kirim'            => $this->input->post('bruto_batubara_kirim'),
      'tarra_batubara_kirim'            => $this->input->post('tarra_batubara_kirim'),
      'tanggal_batubara_kirim'          => $tanggal_batubara_kirim,
      'jam_batubara_kirim'              => $this->input->post('jam_batubara_kirim'),
      'tanggal_terima'                  => $tanggal_terima,
      'jam_terima'                      => $this->input->post('jam_terima'),
      'nomor_sj_batubara_kirim'         => $this->input->post('nomor_sj_batubara_kirim'),
      'id_kode_batubara_kirim'          => $this->input->post('id_kode_batubara_kirim'),
      'id_checker'       => $this->input->post('id_checker'),
      'id_checker_batubara_kirim'       => $this->input->post('id_checker_batubara_kirim'),
      'keterangan_batubara_kirim'       => $this->input->post('keterangan_batubara_kirim'),
    );


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

      $filename = date('YmdHis');

      $config['upload_path']   = FCPATH . '/assets/images/batubara_kirim/';
      $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_kirim_model->get_by_id($id)->file_upload;
        if (file_exists(FCPATH . "assets/images/batubara_kirim/$foto_old") and (!($foto_old == ""))) {
          unlink(FCPATH . "assets/images/batubara_kirim/$foto_old");
        }
        $nama = $this->upload->data('file_name');
        $data['file_upload'] = $nama;
      }
    }

    $tanggal_old = $this->Form_batubara_kirim_model->get_by_id($id)->tanggal_batubara_kirim;
    if (!($tanggal_old == $tanggal_batubara_kirim)) {
      $nomor_urut = ($this->Form_batubara_kirim_model->get_nomor_max($tanggal_batubara_kirim)->nomor_urut_batubara_kirim) + 1;
      $data['nomor_urut_batubara_kirim'] = $nomor_urut;
    }




    // eksekusi query
    $this->Form_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'));

    $foto_old = $this->Form_batubara_kirim_model->get_by_id($id)->file_upload;
    if (file_exists(FCPATH . "assets/images/batubara_kirim/$foto_old") and (!($foto_old == ""))) {
      unlink(FCPATH . "assets/images/batubara_kirim/$foto_old");
    }

    $this->Form_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['form_batubara_kirim'] = $this->Form_batubara_kirim_model->get_by_id($id);
    $this->data['title'] = 'Data ' . $this->data['module'];
    $this->load->view('form_batubara_kirim/' . $form_open, $this->data);

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



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

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

    $this->data['form_batubara_kirim'] = $this->Form_batubara_kirim_model->get_data_report($tanggal_batubara_awal, $tanggal_batubara_akhir);
    $this->data['tanggal_awal'] = $tanggal_batubara_awal;
    $this->data['tanggal_akhir'] = $tanggal_batubara_akhir;

    $this->load->view('form_batubara_kirim/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(5, 5, 5, 15));
    $pdf->setDefaultFont('Arial');
    $pdf->setTestTdInOnePage(false);
    $pdf->WriteHTML($html);
    $pdf->Output('Lap Batubara Kirim.pdf');
  }
  
  public function report_kode()
  {
    ob_start();
    $tanggal_batubara_awal = $this->input->post('tanggal_report_awal');
    $tgl1 = substr($tanggal_batubara_awal, 0, 2);
    $bln1 = substr($tanggal_batubara_awal, 3, 2);
    $thn1 = substr($tanggal_batubara_awal, 6, 4);
    $tanggal_batubara_awal = "$thn1-$bln1-$tgl1";

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

    $id_kode_batubara_kirim = $this->input->post('id_kode_batubara_kirim');

    $this->data['form_batubara_kirim'] = $this->Form_batubara_kirim_model->get_data_report_kode($tanggal_batubara_awal, $tanggal_batubara_akhir, $id_kode_batubara_kirim);
    $this->data['nama_kode'] = $this->Kode_batubara_kirim_model->get_by_id($id_kode_batubara_kirim)->nama_kode_batubara_kirim;
    $this->data['tanggal_awal'] = $tanggal_batubara_awal;
    $this->data['tanggal_akhir'] = $tanggal_batubara_akhir;

    $this->load->view('form_batubara_kirim/report_kode', $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 Kode Kirim.pdf');
  }



  public function cetak($id)
  {
    ob_start();
    $id = decrypt($id);
    $this->data['form_batubara_kirim'] = $this->Form_batubara_kirim_model->get_by_id($id);

    $this->load->view('form_batubara_kirim/cetak', $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(15, 15, 20, 15));
    $pdf->setDefaultFont('Arial');
    $pdf->setTestTdInOnePage(false);
    $pdf->WriteHTML($html);
    $pdf->Output('Surat Kirim Batubara.pdf');
  }

  public function batubara_diterima()
  {
    $id = decrypt($this->input->post('id'));

    // time sekarang
    date_default_timezone_set('Asia/Makassar');
    $tanggal = date('Y-m-d');
    $waktu = date('H:i:s');


    $data = array(
      'approve'          => 1,
      'tanggal_terima'   => $tanggal,
      'jam_terima'       => $waktu,
    );

    $this->Form_batubara_kirim_model->update($id, $data);

    $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>Batubara Diterima
        </div>');
    echo "1";
  }
}
