<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('toi_date_input')){
    function toi_date_input($name='', $value='', $style='', $size=200){
        $tanggal = date('d');
        $bulan   = date('m');
        $tahun   = date('Y');

        $id = str_replace(['[',']'], '', $name);

        if($value != ''){
            if(strpos($value, '-') != false){
                $value = strtotime($value);
            }
            $tanggal = date('d', $value);
            $bulan   = date('m', $value);
            $tahun   = date('Y', $value);
        }

        $time_css = "onfocus='this.size=3;' onblur='this.size=1;' onchange=this.size=1;this.blur();on_tanggal('".$id."');";
        $html = "
        <div style='width:".$size."px; border: 1px solid #fff; padding: 1px;' class='input-group'>
        <select id='id_tanggal".$id."' style='width:25%;' class='".$style."' ".$time_css." >";
        for($x=1; $x<32; $x++) {  
            $x = (strlen($x) < 2) ? '0'.$x : $x;
            $html .= "<option ".(($tanggal == $x) ? 'selected':'')." value='".$x."'>".$x."</option>";
        }
        $html .= "</select> <span style='width:5%; text-align:center;'>/</span> 
        <select id='id_bulan".$id."' style='width:25%;' class='".$style."' ".$time_css.">";
        for($x=1; $x<13; $x++) {  
            $x = strlen($x) < 2 ? '0'.$x : $x;
            $html .= "<option ".(($bulan == $x) ? 'selected':'')."  value='".$x."'>".$x."</option>";
        }
        $html .= "</select> <span style='width:5%; text-align:center;'>/</span>
        <select id='id_tahun".$id."' style='width:40%;' class='".$style."' ".$time_css.">";
        for($x=date('Y')-5; $x<(date('Y')+5); $x++) {  
            $html .="<option ".(($tahun == $x) ? 'selected':'')."  value='".$x."'>".$x."</option>";
        }
        if($value != '' && $value != '1970-01-01' && $value != strtotime('1970-01-01')){
            $vdate = date('Y-m-d', $value);
        }else{
            $vdate = date('Y-m-d');
        }
        $html .="</select>
        <input type='hidden' name='".$name."' value='".$vdate."' id='tanggal".$id."'>
        </div>";
        ?>
        <script>
            $("#tanggal<?= $id; ?>").val("<?= $value; ?>");
            function on_tanggal(xx)
            {
            $("#tanggal"+xx).val($("#id_tahun"+xx).val()+'-'+$("#id_bulan"+xx).val()+'-'+$("#id_tanggal"+xx).val());
            }
        </script>
        <?php
        return $html;
    }
}
if ( ! function_exists('toi_time_input')){
    function toi_time_input($name='', $value='', $style='', $size=200){
        $jam    = date('H');
        $menit  = date('i');

        $id = str_replace(['[',']'], '', $name);

        if($value != ''){
            if(strpos($value, '-') != false){
                $value = strtotime($value);
            }
            $jam    = date('H', $value);
            $menit  = date('i', $value);
        }

        $time_css = "onfocus='this.size=3;' onblur='this.size=1;' onchange=this.size=1;this.blur();on_waktu('".$id."');";
        $html = "
        <div style='width:".$size."px; border: 1.5px solid #fff; padding: 1px;' class='input-group'>
        <select id='id_jam".$id."' style='width:48%;' class='".$style."' ".$time_css." >";
        for($x=0; $x<24; $x++) {  
            $x = (strlen($x) < 2) ? '0'.$x : $x;
            $html .="<option ".(($jam == $x) ? 'selected':'')." value='".$x."'>".$x."</option>";
        }
        $html .= "</select> <span style='width:4%; text-align:center;'>:</span> 
        <select id='id_menit".$id."' style='width:48%;' class='".$style."' ".$time_css.">";
        for($x=0; $x<60; $x++) {  
            $x = strlen($x) < 2 ? '0'.$x : $x;
            $html .="<option ".(($menit == $x) ? 'selected':'')." value='".$x."'>".$x."</option>";
        }

        if($value != ''){
            $vtime = date('H:i', $value);
        }else{
            $vtime = date('H:i');
        }

        $html .="</select>
        <input type='hidden' name='".$name."' value='".$vtime."' id='waktu".$id."'>
        </div>";

        ?>
        <script>

            $("#waktu<?= $id; ?>").val($("#id_jam<?= $id; ?>").val()+':'+$("#id_menit<?= $id; ?>").val());
            function on_waktu(xx)
            {
                $("#waktu"+xx).val($("#id_jam"+xx).val()+':'+$("#id_menit"+xx).val());
                //alert($("#waktu"+xx).val());
            }
        </script>
        <?php
        return $html;
    }
}

if ( ! function_exists('toi_notif_modal')){
    function toi_notif_modal($id='', $url='#', $title='', $type='default')
    {
        $html = "";
        $html .="
            <!-- Button trigger modal -->
            <button type='button' class='btn btn-".$type." btn-xs' data-toggle='modal' data-target='#i".$id."'>
                Hapus
            </button>
            
            <!-- Modal -->
            <div class='modal fade' id='i".$id."' tabindex='-1' role='dialog' aria-labelledby='i".$id."ModalLabel' aria-hidden='true'>
                <div class='modal-dialog model-sm' role='document'>
                    <div class='modal-content bg-".$type."'>
                        <div class='modal-header'>
                            <button type='button' class='close' data-dismiss='modal' aria-label='Close'>
                                <span aria-hidden='true'>&times;</span>
                            </button>
                        </div>
                        <div class='modal-body'>
                                <h4>Apakah Anda yakin akan menghapus data ini?</h4>
                                <button type='button' class='btn btn-secondary' data-dismiss='modal'>Tidak</button>
                                <a href='".$url."' class='btn btn-secondary'>Ya</a>
                        </div>
                        <div class='modal-footer'>
                            
                        </div>
                    </div>
                </div>
            </div>
        ";
        return $html;
    }
}

if ( ! function_exists('toi_image_modal')){
    function toi_image_modal($id='', $url='#', $title='', $type='default')
    {
        $html = "";
        $html .="
            <!-- Button trigger modal -->
            <button type='button' class='btn btn-success btn-xs' data-toggle='modal' data-target='#i".$id."'>
                Foto
            </button>
            
            <!-- Modal -->
            <div class='modal fade' id='i".$id."' tabindex='-1' role='dialog' aria-labelledby='i".$id."ModalLabel' aria-hidden='true'>
                <div class='modal-dialog model-sm' role='document'>
                    <div class='modal-content bg-".$type."'>
                        <div class='modal-header'>
                            <button type='button' class='close' data-dismiss='modal' aria-label='Close'>
                                <span aria-hidden='true'>&times;</span>
                            </button>
                        </div>
                        <div class='modal-body'>
                                <img width='500' src='".base_url($url)."'>
                                <hr>
                                <button type='button' class='btn btn-secondary' data-dismiss='modal'>keluar</button>
                        </div>
                        <div class='modal-footer'>
                            
                        </div>
                    </div>
                </div>
            </div>
        ";
        return $html;
    }
}
if ( ! function_exists('toi_verifikasi_data_uji')){
    function toi_verifikasi_data_uji($label='', $content='')
    {
        $html = "
            <div class='col-md-3'>
                <label>".$label."</label>
            </div>
            <div class='col-md-3'>
                : ".$content."
            </div>
        ";

        return $html;
    }
}

if ( ! function_exists('toi_modal_note_verifikasi')){
    function toi_modal_note_verifikasi($id='', $url='#', $title='', $type='default')
    {
        $html = "";
        $html .="
            <!-- Modal -->
            <div class='modal fade' id='verifikasi".$id."' >
                <div class='modal-dialog model-sm' role='document'>
                    <div class='modal-content bg-".$type."'>
                        <div class='modal-header'>
                            <button type='button' class='close' data-dismiss='modal' aria-label='Close'>
                                <span aria-hidden='true'>&times;</span>
                            </button>
                        </div>
                        <div class='modal-body'>
                                <form method='post' action='".site_url('permintaan_uji/verifikasi/' . $id . '/tidak')."'>
                                <textarea name='note_verifikasi' class='form-control mb-2'></textarea>
                                <button type='submit' class='btn btn-primary'>Tolak dan Simpan Catatan</button>
                                <button type='button' class='btn btn-secondary' data-dismiss='modal'>keluar</button>
                                </form>
                        </div>
                        <div class='modal-footer'>
                            
                        </div>
                    </div>
                </div>
            </div>

            <script>
                function toi_modal_note_verifikasi(x)
                {
                    $('#verifikasi'+x).modal('show');
                }
            </script>
        ";
        return $html;
    }
}

if ( ! function_exists('parameter_uji_lhu_lhp')){
    function parameter_uji_lhu_lhp($data_sampel = '')
    {
        $parameter = [];
        if($data_sampel != ''){
            foreach($data_sampel as $dkey => $ds){
                if($ds['jenis_pengujian'] != '' && !is_null($ds['jenis_pengujian']) && isset($ds['jenis_pengujian'])){
                    $jenis_uji = json_decode($ds['jenis_pengujian']);
                    foreach($jenis_uji as $jkey => $ju){
                        $parameter[] = $ju;
                    }
                }
            }

            $parameter = array_unique($parameter);
            sort($parameter);
        }

        $ppuji = [];
        $puji = parameter_uji();
        foreach($parameter as $pkey => $p){
            foreach($puji as $ukey => $u){
                if($u['id'] == $p){
                    $ppuji[] = $u;
                }
            }
        }

        return $ppuji;
    }
}

if ( ! function_exists('parameter_uji')){
    function parameter_uji()
    {
        $parameter = [
            [
                'id'        => 1,
                'nama_uji'  => 'air',
                'label'     => 'Air',
            ],
            [
                'id'        => 2,
                'nama_uji'  => 'abu',
                'label'     => 'Abu',
            ],
            [
                'id'        => 3,
                'nama_uji'  => 'protein_kasar',
                'label'     => 'Protein Kasar',
            ],
            [
                'id'        => 4,
                'nama_uji'  => 'lemak_kasar',
                'label'     => 'Lemak Kasar',
            ],

            [
                'id'        => 5,
                'nama_uji'  => 'serat_kasar',
                'label'     => 'Serrat Kasar',
            ],
            [
                'id'        => 6,
                'nama_uji'  => 'kalsium',
                'label'     => 'Kalsium',
            ],
            [
                'id'        => 7,
                'nama_uji'  => 'fosfor',
                'label'     => 'Fosfor',
            ],
            [
                'id'        => 8,
                'nama_uji'  => 'tdn',
                'label'     => 'TDN',
            ],
            [
                'id'        => 9,
                'nama_uji'  => 'ur',
                'label'     => 'Urea',
            ],
            [
                'id'        => 10,
                'nama_uji'  => 'af',
                'label'     => 'Aflatoksin Total',
            ],
            [
                'id'        => 11,
                'nama_uji'  => 'andf',
                'label'     => 'aNDF',
            ],
            [
                'id'        => 12,
                'nama_uji'  => 'mbm',
                'label'     => 'MBM',
            ],
            [
                'id'        => 13,
                'nama_uji'  => 'ca',
                'label'     => 'Kalsium',
            ],
            [
                'id'        => 14,
                'nama_uji'  => 'pkd',
                'label'     => 'PK Dumas',
            ],
            [
                'id'        => 15,
                'nama_uji'  => '',
                'label'     => '',
            ],
        ];
        return $parameter;
    }
}
