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

class Kepaladinas extends CI_Controller
{

  public function __construct()
  {
    parent::__construct();
    $this->load->model('model');
    $this->load->model('m_tabel_ss');
    // $this->load->library('pdf');
    date_default_timezone_set("Asia/Kuala_Lumpur");
    ini_set('memory_limit', '-1');
    if ($this->session->userdata('level') != 'Kepala Dinas') {
      $this->session->unset_userdata(array('nik',  'level'));
      redirect('/login');
    } else {
      $cek_data = $this->model->tampil_data_where('tb_admin', array('nik' => $this->session->userdata('nik'), 'level' => $this->session->userdata('level'), 'id_dinas' => $this->session->userdata('id_dinas')))->result();
      if (count($cek_data) > 0) {
        # code...
        $this->main['level'] = $cek_data[0]->level;
        $this->main["nama"] = $cek_data[0]->nama;
        $cek_data_dinas = $this->model->tampil_data_where('tb_dinas', ['id_dinas' => $cek_data[0]->id_dinas])->result()[0];
        $this->main["id_dinas"] = $cek_data_dinas->id_dinas;
        $this->main["dinas"] = $cek_data_dinas->dinas;
        $this->main['lat'] = $cek_data_dinas->lat;
        $this->main['lng'] = $cek_data_dinas->lng;
        $this->main['radius'] = $cek_data_dinas->radius;
        $this->month = date("m");
        $this->year = date("Y");
      } else {
        $this->session->unset_userdata(array('nik',  'level'));
        redirect('/login');
      }
    }
  }

  function index()
  {
    $main = $this->main;
    $main["header"] = "Halaman Utama";
    $main['list_karyawan'] = $this->model->tampil_data_where('tb_karyawan', ['id_dinas' => $main['id_dinas']])->result();

    $array = $main['list_karyawan'];
    $list = json_decode(json_encode($array), true);

    $jabatan_order = ['Kepala Dinas', 'Sekretaris', 'Bendahara', 'Kepala Bidang'];
    $new_array_list = array();
    // loop jabatan
    foreach ($jabatan_order as $key => $value) {
      // loop karyawan
      foreach ($list as $key2 => $value2) {
        // check if $value2['jabatan'] contains $value
        if (strpos($value2['jabatan'], $value) !== false) {
          // if true, push to array
          array_push($new_array_list, $value2);
          unset($list[$key2]);
        }
      }
    }

    // print_r($new_array_list);
    // print_r($list);
    foreach ($list as $key => $value) {
      // if $value['jabatan'] is not 'Staf' push to array
      if ($value['jabatan'] != 'Staf') {
        array_push($new_array_list, $value);
        unset($list[$key]);
      }
    }

    // print_r($new_array_list);
    foreach ($list as $key => $value) {
      // if $value['jabatan'] is 'Staf' push to array
      if ($value['jabatan'] == 'Staf') {
        array_push($new_array_list, $value);
        unset($list[$key]);
      }
    }
    $new_array_list = json_decode(json_encode($new_array_list));
    $main['list_karyawan'] = $new_array_list;
    $today = date("Y-m-d");
    foreach ($new_array_list as $key => $value) {
      $main['list_karyawan'][$key]->status_kerja = '-';
      $check_libur = $this->model->tampil_data_where('tb_informasi_libur', ['nik' => $value->nik, 'tanggal' => $today])->result();

      if (count($check_libur) > 0) {
        $main['list_karyawan'][$key]->status_kerja = 'Libur';
      }

      $check_perjalanan_dinas = $this->model->tampil_data_where('tb_informasi_perjalanan_dinas', ['nik' => $value->nik, 'tanggal' => $today])->result();

      if (count($check_perjalanan_dinas) > 0) {
        $main['list_karyawan'][$key]->status_kerja = 'Perjalanan Dinas';
      }
    }

    // echo $this->level;
    $this->load->view('kepaladinas/menu/index', $main);
    // echo $this->session->userdata('level');
  }


  function pengaturan()
  {
    $main = $this->main;
    $main["header"] = "Halaman Lokasi Dinas";

    // echo $this->level;
    $this->load->view('kepaladinas/menu/pengaturan', $main);
    // $this->load->view('kepaladinas/index');
    // echo "sini index admin";
  }


  function karyawan($karyawan = null)
  {
    if ($this->input->post('proses') == "table_laporan") {
      $list = $this->m_tabel_ss->get_datatables(array('created_at', 'ket_laporan'), array(null, 'created_at', 'ket_laporan',  null), array('no_laporan' => 'desc'), "tb_laporan_karyawan", null, array("nik" => $this->input->post('nik')), "*");
      $data = array();
      $no = $_POST['start'];
      foreach ($list as $field) {



        $no++;
        $row = array();
        $row[] = $no;
        $row[] = $field->created_at;
        $row[] = $field->nama_laporan;
        $row[] = "<center><button type='button' onclick='cek_laporan(" . '"' . (string)$field->no_laporan . '"' . ")' title='Edit Detail Karyawan' class='btn btn-primary btn-circle btn-sm waves-effect waves-light'><i class='ico fa fa-edit'></i></button></center>";
        $data[] = $row;
      }

      $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->m_tabel_ss->count_all("tb_laporan_karyawan", null, array("nik" => $this->input->post('nik')), "*"),
        "recordsFiltered" => $this->m_tabel_ss->count_filtered(array('created_at', 'ket_laporan'), array(null, 'created_at', 'ket_laporan',  null), array('no_laporan' => 'desc'), "tb_laporan_karyawan", null, array("nik" => $this->input->post('nik')), "*"),
        "data" => $data,
      );
      //output dalam format JSON
      echo json_encode($output);
    } else if ($this->input->post('proses') == "table_karyawan") {
      $list = $this->m_tabel_ss->get_datatables(array('nik', 'nama', 'no_telpon', 'jabatan', 'pangkat'), array(null, 'nik', 'nama', 'no_telpon', 'jabatan', 'pangkat', null), array('status' => 'desc'), "tb_karyawan", null, array("id_dinas" => $this->main['id_dinas']), "*");
      $data = array();
      $no = $_POST['start'];

      // change stdClass $list to array
      $list = json_decode(json_encode($list), true);

      $jabatan_order = ['Kepala Dinas', 'Sekretaris', 'Bendahara', 'Kepala Bidang'];
      $new_array_list = array();
      // loop jabatan
      foreach ($jabatan_order as $key => $value) {
        // loop karyawan
        foreach ($list as $key2 => $value2) {
          // check if $value2['jabatan'] contains $value
          if (strpos($value2['jabatan'], $value) !== false) {
            // if true, push to array
            array_push($new_array_list, $value2);
            unset($list[$key2]);
          }
        }
      }

      // print_r($new_array_list);
      // print_r($list);
      foreach ($list as $key => $value) {
        // if $value['jabatan'] is not 'Staf' push to array
        if ($value['jabatan'] != 'Staf') {
          array_push($new_array_list, $value);
          unset($list[$key]);
        }
      }

      // print_r($new_array_list);
      foreach ($list as $key => $value) {
        // if $value['jabatan'] is 'Staf' push to array
        if ($value['jabatan'] == 'Staf') {
          array_push($new_array_list, $value);
          unset($list[$key]);
        }
      }



      foreach ($new_array_list as $field) {

        $stat = '';
        $title = 'Tukar ID Smartphone';
        if ($field['device_id'] == null || $field['device_id'] == '') {
          $stat = 'disabled';
          $title = 'Karyawan Belum Login Dari Smartphone';
        }

        $cek_laporan = $this->model->tampil_data_where('tb_laporan_karyawan', ['nik' => $field['nik']])->result();

        if (count($cek_laporan) > 0) {
          $ada_laporan  = 'ada';
        } else {
          $ada_laporan  = 'tidak';
        }

        $no++;
        $row = array();
        $row[] = $no;
        $row[] = $field['nik'];
        $row[] = $field['nama'];
        $row[] = $field['no_telpon'];
        $row[] = $field['jabatan'];
        $row[] = $field['pangkat'];
        $row[] = "<center></center><button type='button' onclick='cek_laporan(" . '"' . (string)$field['nik'] . '"' . "," . '"' . $ada_laporan . '"' . ")' title='Laporan Kegiatan Karyawan' class='btn btn-success btn-circle btn-sm waves-effect waves-light'><i class='ico fa fa-calendar'></i></button></center>";
        $data[] = $row;
      }

      $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->m_tabel_ss->count_all("tb_karyawan", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "recordsFiltered" => $this->m_tabel_ss->count_filtered(array('nik', 'nama', 'no_telpon', 'jabatan', 'pangkat'), array(null, 'nik', 'nama', 'no_telpon', 'jabatan', 'pangkat', null), array('status' => 'desc'), "tb_karyawan", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "data" => $data,
      );
      //output dalam format JSON
      echo json_encode($output);
    } else if ($karyawan != '' && $karyawan != null) {
      $main = $this->main;
      $main["header"] = "Halaman Detail Karyawan";
      $main['karyawan'] = $this->model->tampil_data_where('tb_karyawan', ['nik' => $karyawan, 'id_dinas' => $this->main['id_dinas']])->result();

      if (count($main['karyawan']) == 0) return redirect('kepaladinas/karyawan');

      $cek_laporan = $this->model->tampil_data_where('tb_laporan_karyawan', ['nik' => $karyawan])->result();

      if (count($cek_laporan) > 0) {
        // echo 'ada';
        $main['laporan'] = $cek_laporan;
        $main['nik'] = $karyawan;
        $nama = $main['karyawan'][0]->nama;
        $this->load->view('kepaladinas/menu/karyawan_detail', $main);
      } else {
        redirect('kepaladinas/karyawan');
      }
    } else {
      $main = $this->main;
      $main["header"] = "Halaman Pegawai";

      $this->load->view('kepaladinas/menu/karyawan', $main);
    }

    // echo "sini karyawan";
  }

  function jam_kerja()
  {
    $main = $this->main;
    $main["header"] = "Halaman Jam Kerja";

    // echo $this->level;
    $this->load->view('kepaladinas/menu/jam_kerja', $main);
  }


  function libur()
  {
    if ($this->input->post('proses') == "table_libur") {
      $list = $this->m_tabel_ss->get_datatables(array('range_tanggal', 'created_at'), array(null, 'range_tanggal', null, 'created_at',  null), array('id_libur' => 'desc'), "tb_libur", null, array("id_dinas" => $this->main['id_dinas']), "*");
      $data = array();
      $no = $_POST['start'];
      foreach ($list as $field) {

        $list_karyawan = json_decode($field->list_karyawan);

        $value_karyawan = '';
        foreach ($list_karyawan as $key => $value) {
          $check_karyawan = $this->model->tampil_data_where('tb_karyawan', ['nik' => $value])->result();
          $value_karyawan .= " " . $check_karyawan[0]->nama . ' ,';
        }

        $value_karyawan = rtrim($value_karyawan, ",");

        $no++;
        $row = array();
        $row[] = $no;
        $row[] = $field->range_tanggal;
        $row[] = $value_karyawan;
        $row[] = $field->created_at;
        $row[] = "<center><button type='button' onclick='check_info(" . '"' . (string)$field->id_libur . '"' . ")' title='Check Informasi Libur' class='btn btn-primary btn-circle btn-sm waves-effect waves-light'><i class='ico zmdi zmdi-info-outline'></i></button> <button type='button' onclick='hapus_libur(" . '"' . (string)$field->id_libur . '"' . ")' title='Hapus Informasi Libur' class='btn btn-danger btn-circle btn-sm waves-effect waves-light'><i class='ico zmdi zmdi-delete'></i></button></center>";
        $data[] = $row;
      }

      $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->m_tabel_ss->count_all("tb_libur", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "recordsFiltered" => $this->m_tabel_ss->count_filtered(array('range_tanggal', 'created_at'), array(null, 'range_tanggal', null,  'created_at',  null), array('id_libur' => 'desc'), "tb_libur", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "data" => $data,
      );
      //output dalam format JSON
      echo json_encode($output);
    } else {
      $main = $this->main;
      $main["header"] = "Halaman Libur";

      // echo $main["header"];

      $this->load->view('kepaladinas/menu/libur', $main);
    }
  }

  function perjalanan_dinas()
  {
    if ($this->input->post('proses') == "table_dinas") {
      $list = $this->m_tabel_ss->get_datatables(array('range_tanggal', 'created_at'), array(null, 'range_tanggal', null, 'created_at',  null), array('id_perjalanan_dinas' => 'desc'), "tb_perjalanan_dinas", null, array("id_dinas" => $this->main['id_dinas']), "*");
      $data = array();
      $no = $_POST['start'];
      foreach ($list as $field) {

        $list_karyawan = json_decode($field->list_karyawan);

        $value_karyawan = '';
        foreach ($list_karyawan as $key => $value) {
          $check_karyawan = $this->model->tampil_data_where('tb_karyawan', ['nik' => $value])->result();
          $value_karyawan .= " " . $check_karyawan[0]->nama . ' ,';
        }

        $value_karyawan = rtrim($value_karyawan, ",");

        $no++;
        $row = array();
        $row[] = $no;
        $row[] = $field->range_tanggal;
        $row[] = $value_karyawan;
        $row[] = $field->created_at;
        $row[] = "<center><button type='button' onclick='check_info(" . '"' . (string)$field->id_perjalanan_dinas . '"' . ")' title='Check Informasi Perjalanan Dinas' class='btn btn-primary btn-circle btn-sm waves-effect waves-light'><i class='ico zmdi zmdi-info-outline'></i></button> <button type='button' onclick='hapus_perjalanan_dinas(" . '"' . (string)$field->id_perjalanan_dinas . '"' . ")' title='Hapus Informasi Perjalanan Dinas' class='btn btn-danger btn-circle btn-sm waves-effect waves-light'><i class='ico zmdi zmdi-delete'></i></button></center>";
        $data[] = $row;
      }

      $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->m_tabel_ss->count_all("tb_perjalanan_dinas", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "recordsFiltered" => $this->m_tabel_ss->count_filtered(array('range_tanggal', 'created_at'), array(null, 'range_tanggal', null,  'created_at',  null), array('id_perjalanan_dinas' => 'desc'), "tb_perjalanan_dinas", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "data" => $data,
      );
      //output dalam format JSON
      echo json_encode($output);
    } else {
      $main = $this->main;
      $main["header"] = "Halaman Perjalanan Dinas";

      // echo $main["header"];

      $this->load->view('kepaladinas/menu/perjalanan_dinas', $main);
    }
  }


  function cetak_list_pegawai()
  {
    $this->load->library('Pdf');
    // A4 paper in portrait orientation
    $pdf = new Pdf('P', 'mm', array(210, 300), true, 'UTF-8', false);
    $pdf->SetMargins(10, 30);
    $pdf->AddPage();
    $pdf->SetFont('times', '', 10);

    $list_karyawan = $this->model->tampil_data_where(('tb_karyawan'), ['id_dinas' => $this->main['id_dinas']])->result_array();
    $jabatan_order = ['Kepala Dinas', 'Sekretaris', 'Bendahara', 'Kepala Bidang'];
    $array = array();
    // loop jabatan
    foreach ($jabatan_order as $key => $value) {
      // loop karyawan
      foreach ($list_karyawan as $key2 => $value2) {
        // check if $value2['jabatan'] contains $value
        if (strpos($value2['jabatan'], $value) !== false) {
          // if true, push to array
          array_push($array, $value2);
          unset($list_karyawan[$key2]);
        }
      }
    }

    // print_r($array);
    // print_r($list_karyawan);
    foreach ($list_karyawan as $key => $value) {
      // if $value['jabatan'] is not 'Staf' push to array
      if ($value['jabatan'] != 'Staf') {
        array_push($array, $value);
        unset($list_karyawan[$key]);
      }
    }

    // print_r($array);
    foreach ($list_karyawan as $key => $value) {
      // if $value['jabatan'] is 'Staf' push to array
      if ($value['jabatan'] == 'Staf') {
        array_push($array, $value);
        unset($list_karyawan[$key]);
      }
    }

    $list_karyawan = json_decode(json_encode($array));

    $html = '
    <table width="100%">
        <tr>
          <td align="center" style="font-weight:bold">
            <u>DAFTAR PEGAWAI</u><br>
          </td>
        </tr>
      </table>

      <style>
        
        .ini {
          border-bottom: 1px dotted black;
        }


      </style>
    ';

    $html .= '
    <table width="100%" cellpadding="5" cellspacing="0" border="1">
      <tr>
        <th width="5%" align="center"><b>No</b></th>
        <th width="20%" align="center"><b>Nama</b></th>
        <th width="20%" align="center"><b>NIK</b></th>
        <th width="15%" align="center"><b>Jabatan</b></th>
        <th width="10%" align="center"><b>Status</b></th>
        <th width="15%" align="center"><b>Pangkat</b></th>
        <th width="15%" align="center"><b>Keanggotaan</b></th>
      </tr>
    ';

    foreach ($list_karyawan as $key => $value) {
      $html .= '
      <tr>
        <td align="center">' . ($key + 1) . '</td>
        <td align="center">' . $value->nik . '</td>
        <td align="center">' . $value->nama . '</td>
        <td align="center">' . $value->jabatan . '</td>
        <td align="center">' . $value->status . '</td>
        <td align="center">' . $value->pangkat . '</td>
        <td align="center">' . $value->keanggotaan . '</td>
      </tr>
      ';
    }

    $html .= '
    </table><br><br>
    ';

    $html .= '
    <table style="width: 100%;">
      <tr>
        <td width="5%"></td>
        <td width="25%"></td>
        <td width="35%"></td>
        <td width="30%">
          Topoyo, / ' . date('d / ') . $this->model->bulan(date('m')) . '/ 2022
        </td>
        <td width="5%"></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>
          <b>Kepala Dinas Pariwisata</b>
        </td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>
          <b>Kepemudaaan Dan Olahraga</b>
        </td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>
          <b>HJ. NANDA . S. Kep ., MM</b>
        </td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>
          Pangkat: Pembina Tk. IIVb
        </td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>
          Nip . 12312321
        </td>
        <td></td>
      </tr>


    </table>
    ';

    $pdf->writeHTML($html, true, false, true, false, '');
    $pdf->Output("laporan data pasien.pdf", 'I');
  }

  function surat_masuk()
  {

    if ($this->input->post('proses') == "table_surat_masuk") {
      $list = $this->m_tabel_ss->get_datatables(array('no_surat', 'asal_surat', 'tanggal_pengiriman', 'tanggal_diterima', 'created_at', 'updated_at'), array('no_surat', 'asal_surat', 'tanggal_pengiriman', 'tanggal_diterima', 'created_at', 'updated_at',  null), array('id_surat_masuk' => 'desc'), "tb_surat_masuk", null, array("id_dinas" => $this->main['id_dinas']), "*");
      $data = array();
      $no = $_POST['start'];
      foreach ($list as $field) {

        $no++;
        $row = array();
        $row[] = $field->no_surat;
        $row[] = $field->tanggal_pengiriman;
        $row[] = $field->tanggal_diterima;
        $row[] = $field->no_agenda;
        $row[] = $field->created_at;
        $row[] = $field->updated_at;
        $row[] = "<center><button type='button' onclick='check_info(" . '"' . (string)$field->id_surat_masuk . '"' . ")' title='Check Informasi Surat Masuk' class='btn btn-primary btn-circle btn-sm waves-effect waves-light'><i class='ico zmdi zmdi-info-outline'></i></button> <button type='button' onclick='lihat_surat(" . '"' . (string)$field->id_surat_masuk . '"' . ")' title='Lihat Surat Masuk' class='btn btn-success btn-circle btn-sm waves-effect waves-light'><i class='ico mdi mdi-email'></i></button></center>";
        $data[] = $row;
      }

      $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->m_tabel_ss->count_all("tb_surat_masuk", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "recordsFiltered" => $this->m_tabel_ss->count_filtered(array('no_surat', 'asal_surat', 'tanggal_pengiriman', 'tanggal_diterima', 'created_at', 'updated_at'), array('no_surat', 'asal_surat', 'tanggal_pengiriman', 'tanggal_diterima',  null), array('id_surat_masuk' => 'desc'), "tb_surat_masuk", null, array("id_dinas" => $this->main['id_dinas']), "*"),
        "data" => $data,
      );
      //output dalam format JSON
      echo json_encode($output);
    } else {
      $main = $this->main;
      $main["header"] = "Halaman Surat Masuk";



      $this->load->view('kepaladinas/menu/surat_masuk', $main);
    }
  }


  function laporan($month = null, $year = null)
  {
    $main = $this->main;
    $main["header"] = "Halaman Laporan";

    // $main["month"] = $month != null ? $month : date("m");
    $main["month"] = $month != null ? $month : date("m");
    $main["year"] = $year != null ? $year : date("Y");

    $main["first_date"] = ($main["month"] == "09" && $main["year"]  == 2022) ? 21 : 1;

    $main["last_date"] =  date("d");

    // echo $main["month"].;
    // echo $main["year"] . "ini year";
    // $i = 21;
    // $tanggal = strlen($i) == 1 ? "0" . $i : $i;
    // echo $tanggal;

    $list_karyawan = $this->model->tampil_data_where(('tb_karyawan'), ['id_dinas' => $this->main['id_dinas']])->result_array();

    $jabatan_order = ['Kepala Dinas', 'Sekretaris', 'Bendahara', 'Kepala Bidang'];
    $array = array();
    // loop jabatan
    foreach ($jabatan_order as $key => $value) {
      // loop karyawan
      foreach ($list_karyawan as $key2 => $value2) {
        // check if $value2['jabatan'] contains $value
        if (strpos($value2['jabatan'], $value) !== false) {
          // if true, push to array
          array_push($array, $value2);
          unset($list_karyawan[$key2]);
        }
      }
    }

    // print_r($array);
    // print_r($list_karyawan);
    foreach ($list_karyawan as $key => $value) {
      // if $value['jabatan'] is not 'Staf' push to array
      if ($value['jabatan'] != 'Staf') {
        array_push($array, $value);
        unset($list_karyawan[$key]);
      }
    }

    // print_r($array);
    foreach ($list_karyawan as $key => $value) {
      // if $value['jabatan'] is 'Staf' push to array
      if ($value['jabatan'] == 'Staf') {
        array_push($array, $value);
        unset($list_karyawan[$key]);
      }
    }

    // change array to stdClass
    $main["list_karyawan"] = json_decode(json_encode($array));

    // print_r($main['list_karyawan']);

    $this->load->view('kepaladinas/menu/laporan', $main);
  }

  function cetakPDF($stat = null, $month = null, $year = null)
  {
    $main = $this->main;

    if ($stat == null) {
      redirect(base_url('kepaladinas'));
    }
    if ($stat != 'pns' && $stat != 'kontrak') {
      redirect(base_url('kepaladinas'));
    }

    $main['stat'] = $stat == 'pns' ? 'PNS' : 'Kontrak';

    $month = $month != null ? $month : date("m");
    $year = $year != null ? $year : date("Y");

    $main['month'] = $month;
    $main['year'] = $year;

    $list_karyawan = $this->model->tampil_data_where(('tb_karyawan'), ['id_dinas' => $this->main['id_dinas']])->result_array();

    $jabatan_order = ['Kepala Dinas', 'Sekretaris', 'Bendahara', 'Kepala Bidang'];
    $array = array();
    // loop jabatan
    foreach ($jabatan_order as $key => $value) {
      // loop karyawan
      foreach ($list_karyawan as $key2 => $value2) {
        // check if $value2['jabatan'] contains $value
        if (strpos($value2['jabatan'], $value) !== false) {
          // if true, push to array
          array_push($array, $value2);
          unset($list_karyawan[$key2]);
        }
      }
    }

    // print_r($array);
    // print_r($list_karyawan);
    foreach ($list_karyawan as $key => $value) {
      // if $value['jabatan'] is not 'Staf' push to array
      if ($value['jabatan'] != 'Staf') {
        array_push($array, $value);
        unset($list_karyawan[$key]);
      }
    }

    // print_r($array);
    foreach ($list_karyawan as $key => $value) {
      // if $value['jabatan'] is 'Staf' push to array
      if ($value['jabatan'] == 'Staf') {
        array_push($array, $value);
        unset($list_karyawan[$key]);
      }
    }

    $main['list_karyawan'] = json_decode(json_encode($array));
    $main['title'] = "Laporan Data Karyawan " . $main['stat'] . " Bulan " . $this->model->bulan($month) . " Tahun " . $year;

    $main['title2'] = 'Daftar Hadir Harian ';
    $main['title2'] .=  $stat == 'pns' ? "Pegawai Negeri Sipil" : "Tenaga Kontrak";
    $main['title3'] = $this->model->bulan($month) . " " . $year;

    $this->load->view('kepaladinas/menu/cetak_pdf', $main);
  }

  function logout()
  {
    $this->session->sess_destroy();
    redirect(base_url('login'));
  }
}
