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

if ( ! function_exists('aksi_button')){
    function aksi_button($method){
        if($method === 'start') return '<div class="btn-group m-b-5 m-r-5">
        <a href="javascript:;" class="btn btn-inverse">Aksi</a>
        <a href="javascript:;" data-toggle="dropdown" class="btn btn-grey dropdown-toggle" aria-expanded="false">
            <span class="caret"></span>
        </a>
        <ul class="dropdown-menu pull-right" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(70px, 32px, 0px);">';
        if($method === 'end') return '</ul>
        </div>';
    }
}

if ( ! function_exists('nomor_id')){
    function nomor_id($id) {
        $pk = strlen($id);
        $idsm = null;
        if($pk == 2) $idsm = '0'.$id;
        else if($pk == 1) $idsm = '00'.$id;
        return $idsm;
    }
}

if ( ! function_exists('div_form')){
    function div_form($method, $nama_l = null, $lebar_l = null, $lebar_f = null, $requir = null) {
        $required = '';
        if($requir !== null) $required .= "<span class='text-danger'>*</span>";

        if($method === 'start'){
            $html ="<div class='form-group m-b-11'>
                    <label class='col-md-".$lebar_l." col-form-label control-label'>".$nama_l. $required."</label>
                    <div class='col-md-".$lebar_f."'>";
        }
        if($method === 'end') {
            $html = "</div></div>";
        }

        return $html;
    }
}

if ( ! function_exists('toi_input')){
    function toi_input($name, $value = null, $type = null, $css = null, $place_holder = null, $att = null){
        $place_holder = 'Masukkan '.($place_holder === null ? $name : $place_holder);
        $html ="<input class='form-control ".$css." ".$name."' name='".$name."' value='".$value."' type='".$type."' placeholder=' ".$place_holder."' ".$att.">";

        return $html;
    }
}

if ( ! function_exists('toi_datetime01')){
    function toi_datetime01($datetime){
        if(strpos($datetime, 'AM') || strpos($datetime, 'PM')){
            if(strpos($datetime, 'PM')){
                $datetime = str_replace('PM','',$datetime);
                $datetime = date('Y-m-d H:i:s',strtotime('+12 hour',strtotime($datetime)));
            }else{
                $datetime = str_replace('AM','',$datetime);
                $datetime = date('Y-m-d H:i:s',strtotime($datetime));
            }
        }
        return $datetime;
    }
}

if ( ! function_exists('toi_time01')){
    function toi_time01($time) {
        if(strpos($time, 'PM')){
            $time = str_replace('PM','',$time);
            $time = date('H:i:s',strtotime('+12 hour',strtotime($time)));
        }else{
            $time = str_replace('AM','',$time);
            $time = date('H:i:s',strtotime($time));
        }

        return $time;
    }
}

if ( ! function_exists('toi_datetime02')){
    function toi_datetime02($datetime) {
        $datetime = date('m/d/Y h:i A', strtotime($datetime));
        return $datetime;
    }
}

if ( ! function_exists('toi_date01')){
    function toi_date01($datetime) {
        $datetime = date('Y-m-d', strtotime($datetime));
        return $datetime;
    }
}

if ( ! function_exists('toi_date03')){
    function toi_date03($date=null) {
      if(!is_null($date) && $date != '' && strpos('|'.$date, "0000-00-00") == false){
        $date = date('d/m/Y', strtotime($date));
        return $date;
      }
    }
}

if ( ! function_exists('menu_parameter')){
    function menu_parameter($url, $back=false, $batch=null, $u_analis = null) {
        $html = "<div class='btn-group btn-group-justified'>";

        if($back){
          if(!is_null($batch)){
            $html .= "<a href='".site_url('sampel/analis/batch/').$url.$batch."' class='btn btn-primary ".($url === 'disposisi' ? 'active' : '')."'>Kembali</a>";
          } else {
            $html .= "<a href='".site_url('sampel/analis/form_uji/').$url."' class='btn btn-primary ".($url === 'disposisi' ? 'active' : '')."'>Kembali</a>";
          }
        } else{
          $html .= "<a href='".site_url('sampel/pengajuan/')."' class='btn btn-primary ".($url === 'disposisi' ? 'active' : '')."'>Disposisi</a>";
          if(!is_null($u_analis) && strpos($u_analis, '1|') != false) {
            $html .= "<a href='".site_url('sampel/analis/form_uji/air')."' class='btn btn-default ".($url === 'air' ? 'active' : '')."'>Air</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '2|') != false) {
            $html .= "<a href='".site_url('sampel/analis/form_uji/abu')."' class='btn btn-default ".($url === 'abu' ? 'active' : '')."'>Abu</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '3|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/protein_kasar')."' class='btn btn-default ".($url === 'protein_kasar' ? 'active' : '')."'>Protein Kasar</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '4|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/serat_kasar')."' class='btn btn-default ".($url === 'serat_kasar' ? 'active' : '')."'>Serat Kasar</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '5|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/lemak_kasar')."' class='btn btn-default ".($url === 'lemak_kasar' ? 'active' : '')."'>Lemak Kasar</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '7|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/fosfor')."' class='btn btn-default ".($url === 'fosfor' ? 'active' : '')."'>Fosfor</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '6|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/kalsium')."' class='btn btn-default ".($url === 'kalsium' ? 'active' : '')."'>Ca Titrimetri</a>";
          }

          if(!is_null($u_analis) && strpos($u_analis, '9|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/ur')."' class='btn btn-default ".($url === 'ur' ? 'active' : '')."'>Urea</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '10|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/af')."' class='btn btn-default ".($url === 'af' ? 'active' : '')."'>Aflatoksin Total</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '11|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/andf')."' class='btn btn-default ".($url === 'andf' ? 'active' : '')."'>aNDF</a>";
          }
          // if(!is_null($u_analis) && strpos($u_analis, '12|') != false) {
          //   $html .="<a href='".site_url('sampel/analis/form_uji/mbm')."' class='btn btn-default ".($url === 'mbm' ? 'active' : '')."'>MBM</a>";
          // }
          if(!is_null($u_analis) && strpos($u_analis, '13|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/ca')."' class='btn btn-default ".($url === 'ca' ? 'active' : '')."'>Ca AAS</a>";
          }
          if(!is_null($u_analis) && strpos($u_analis, '14|') != false) {
            $html .="<a href='".site_url('sampel/analis/form_uji/pkd')."' class='btn btn-default ".($url === 'pkd' ? 'active' : '')."'>PK Dumas</a>";
          }
        }

        $html .= "</div>";

        return $html;
    }
}

if ( ! function_exists('set_html_entities'))
{
    function set_html_entities($str)
    {
        return htmlentities($str, ENT_QUOTES | ENT_DISALLOWED | ENT_XML1, "UTF-8");
    }
}

if ( ! function_exists('set_indo_time')) {
    function set_indo_time($tanggal = null)
    {
        // Parameter Input : YYYY-MM-DD hh:mm:ss
        // Return Output : 01 Januari 2018 12:00 or NULL
        if ($tanggal != '0000-00-00 00:00:00' && !is_null($tanggal)) {
            $bulan = array(1 => 'Januari',
                'Februari',
                'Maret',
                'April',
                'Mei',
                'Juni',
                'Juli',
                'Agustus',
                'September',
                'Oktober',
                'November',
                'Desember'
            );
            $split = explode('-', $tanggal);
            $tanggal = substr($split[2], 0, 2);
            $jam = substr($split[2], 3, 8);
            return $tanggal . ' ' . $bulan[(int)$split[1]] . ' ' . $split[0] . ' ' . $jam;
        } else
            return NULL;
    }

    if ( ! function_exists('get_time_from_timestamp')) {
        function get_time_from_timestamp($tanggal = null)
        {
            // Parameter Input : YYYY-MM-DD hh:mm:ss
            // Return Output : hh:mm:ss
            if ($tanggal == null || empty($tanggal)) {
                //return "00:00";
                return null;
            } else {
                $split = explode(' ', $tanggal);
                $split[1] = explode(':', $split[1]);
                $split[1] = $split[1][0].":".$split[1][1];
                return $split[1];
            }
        }
    }

    if ( ! function_exists('set_indo_date_from_timestemp')) {
        function set_indo_date_from_timestemp($tanggal = null, $type=null)
        {
            // Parameter Input : YYYY-MM-DD hh:mm:ss
            // Return Output : 01 Januari 2018 12:00 or NULL
            $nbsp = '&nbsp;';
            if($type == 'cetak') $nbsp=' ';

            if (strpos('|'.$tanggal, '0000-00-00') == false && $tanggal != NULL) {
                $bulan = array(1 => 'Januari',
                    'Februari',
                    'Maret',
                    'April',
                    'Mei',
                    'Juni',
                    'Juli',
                    'Agustus',
                    'September',
                    'Oktober',
                    'November',
                    'Desember'
                );
                $split = explode('-', $tanggal);
                $tanggal = substr($split[2], 0, 2);
                return $tanggal . $nbsp . $bulan[(int)$split[1]] . $nbsp . $split[0];
            } else
                return NULL;
        }
    }

    if ( ! function_exists('set_indo_time_from_timestemp')) {
        function set_indo_time_from_timestemp($tanggal = null)
        {
            // Parameter Input : YYYY-MM-DD hh:mm:ss
            // Return Output : 12:00 or NULL
            if ($tanggal != '0000-00-00 00:00:00' && $tanggal != NULL) {

                $split = explode('-', $tanggal);
                $jam = substr($split[2], 3, 5);
                return $jam;
            } else
                return NULL;
        }
    }

    if ( ! function_exists('heading_panel_analis')) {
        function heading_panel_analis($data = null){
          $data2 = $data;
          $data = $data2['data'];
          $jenis_uji =  $data2['jenis_uji'];

          $html = "<style type='text/css'>
          	.width-td{width: 100px;}
          </style>

          <div class='row'>
              <!-- begin col-8 -->
              <div class='col-sm-12'>

                <!-- begin panel -->
                <div class='panel panel-grey'>
                    <div class='panel-heading'>
                      <div class='panel-heading-btn'>
                          <a href='javascript:;' class='btn btn-xs btn-icon btn-circle btn-grey' data-click='panel-expand'><i class='fa fa-expand'></i></a>
                      </div>"
                      .menu_parameter($jenis_uji).

                      "<button class='btn btn-primary' id='bt_cari1' onclick='cari1();'><span class='fa fa-search'></span> Pencarian</button>
                      <button class='btn btn-primary' id='bt_cari2' onclick='cari2();'><span class='fa fa-search'></span> Pencarian</button>
                      <hr id='hr1'>"

                      .filter_analis($data2).

                      "<form id='form_edit_analis' action='' method='post'>

                      <button id='edit_analis' type='button' class='btn btn-primary btn-sm' onclick='onEdit()'>Buat Batch</button>
                      <button style='margin-top: 3px;' id='cetak_analis' type='button' class='btn btn-warning btn-sm' onclick='onEdit()'>Cetak</button>
                      <input type='hidden' value='edit' name='method'>

          						<table class='table bg-inverse'>
          							<tr>
          								<th>Suhu (ᵒC)</th>
          								<td>:</td>
          								<th> ".(empty($data[0]) ? '' : $data[0]['suhu'])."</th>

          								<th>Terbitan/Revisi</th>
          								<td>:</td>
          								<th> ".(empty($data[0]) ? '' : $data[0]['terbitan_revisi_ke'])."</th>

          								<th style='width:300px;'></th>
          							</tr>
          							<tr>
          								<th>RH</th>
          								<td>:</td>
          								<th> ".(empty($data[0]) ? '' : $data[0]['rh'])."</th>

          								<th>Tanggal Terbitan</th>
          								<td>:</td>
          								<th>".(empty($data[0]) ? '' : $data[0]['tanggal_terbitan'])."</th>

          								<th style='width:300px;'></th>
          							</tr>
          							<tr>
          								<th>Alat Uji</th>
          								<td>:</td>
          								<th> ".(empty($data[0]) ? '' : $data[0]['alat_uji'])."</th>

          								<th>Tanggal Revisi</th>
          								<td>:</td>
          								<th>".(empty($data[0]) ? '' : $data[0]['tanggal_revisi'])."</th>

          								<th style='width:300px;'></th>
          							</tr>
          						</table>

                    </div>
                    <div class='panel panel-body table-responsive'>

                    <!-- CONTENT table-->
                <!-- </div> -->
                ";

                return $html;
        }
    }

    if ( ! function_exists('filter_analis')) {
        function filter_analis($data = null){
          $golongan_sampel = $data['golongan_sampel'];
          $jenis_uji = $data['jenis_uji'];

          $html = "<form method='post' action='' id='form_filter_cari'>
            <table id='tb_cari' class='table bg-inverse'>
              <tr>
                <th><h4 style='color:#fff;'><i>Search</i></h4></th>
              </tr>
              <tr>
                <td><label>Golongan Sampel</label></td>
                <td>:</td>
                <td>
                  <select class='form-control default-select2' name='gol_jenis_sampel' style='width:100%;'>
                    <option></option>";

                foreach ($golongan_sampel as $r) {
                    $html .="<option value='".$r['id_jenis']."'>".$r['nama_jenis']."</option>";
                }

                $html .= "</select>
                </td>

                <td width='50px'></td>

                <td><label>Tanggal Mulai Uji</label></td>
                <td>:</td>
                <td><input class='form-control' type='date' value='".(!empty($filter) ? $filter['mulai_uji'] : '')."' name='mulai_uji' placeholder='Masukkan tanggal'></td>
              </tr>
              <tr>
                <td><label>Nomor Sampel</label></td>
                <td>:</td>
                <td>
                  <input class='form-control' value='".(!empty($filter) ? $filter['id_sampel'] : '')."' name='id_sampel' placeholder='Masukkan Nomor Sampel'>
                </td>

                <td width='50px'></td>

                <td><label>Persetujuan</label></td>
                <td>:</td>
                <td><input class='form-control' type='date' value='".(!empty($filter) ? $filter['persetujuan'] : '')."' name='persetujuan' placeholder='Masukkan tanggal'></td>
              </tr>
              <tr>
                <td></td>
                <td></td>
                <td>
                  <button class='btn btn-primary btn-sm' type='button' onclick='onfilter_cari();'>Cari</button>
                  <a href='".site_url('sampel/analis/form_uji/').$jenis_uji."' class='btn btn-danger btn-sm'>Reset</a>
                </td>
              </tr>
              <tr>
                <td colspan='7'></td>
              </tr>
            </table>

            <input class='form-control' type='hidden' name='filter_cari' value='1'>
          </form>
          ";

          return $html;
        }
    }

    if ( ! function_exists('footer_analis')) {
        function footer_analis($data = null){
          $filter = $data['filter'];

          $html = "
                </form>
                </div>
                <div class='panel panel-footer'>
                <!-- code -->
                </div>
              </div>
              <!-- end panel -->
            </div>
          </div>

          <script> //JS UNTUK ANALIS DAN DISPOSISI FILTER
          		$('#edit_analis, #cetak_analis, #tb_cari, #bt_cari2').hide();";

              if(!empty($filter) && $filter['filter_cari']) {
                $html .="$('#tb_cari').show());";
              }

          		$html .="function onEdit(){
          			$('#form_edit_analis').submit();
          		}

          		function onCheck(x) {
          			if($('#id_check'+x).is(':checked')){
          					$('#edit_analis, #cetak_analis').show();
          			}else{
          				$('#edit_analis, #cetak_analis').hide();
          			}
          		}

          		function cari1(){
          			$('#bt_cari2').show();
          			$('#bt_cari1').hide();

          			$('#tb_cari').show();
          		}

          		function cari2(){
          			$('#bt_cari2').hide();
          			$('#bt_cari1').show();

          			$('#tb_cari').hide();
          		}

              function onfilter_cari() {
                $('#form_filter_cari').submit();
              }
          </script>
          ";

          return $html;
        }
    }
}

if ( ! function_exists('angka_string'))
{
    function angka_string($angka)
    {
        $satuan = array('Nol','Satu','Dua','Tiga','Empat','Lima','Enam','Tujuh','Delapan','Sembilan');
        $satuan_key = array(0,1,2,3,4,5,6,7,8,9);

        $str_angka = strlen($angka);
        if($str_angka = 1){
          for ($i=0; $i < 10; $i++) {
            if($angka == $satuan_key[$i]){
              $hasil = $satuan[$i];
            }
          }
        }
        if($str_angka == 2){
          for ($i=0; $i < 10; $i++) {
            if($angka == $satuan_key[$i]){
              $hasil = $satuan[$i]." Belas";
              if(strpos($angka, '0') != false){
                $hasil = "Sepuluh";
              }
            }
          }
        }
        return $hasil;
    }
}

if ( ! function_exists('get_file_extension')) {
    function get_file_extension($filepath)
    {
        // Parameter Input : files/namafile.txt
        // Return Output : txt
        return pathinfo($filepath, PATHINFO_EXTENSION);
    }
}

if ( ! function_exists('angka_romawi')) {
    function angka_romawi($number)
    {
      $number = (int) $number;
      $num_array = array(1,2,3,4,5,6,7,8,9,10,11,12);
      $rom_array = array('I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII');
      $no = null;

      for ($i=0; $i <count($num_array) ; $i++) {
        if($number == $num_array[$i]){
          $no = $rom_array[$i];
        }
      }

      return $no;
    }
}
