<?php 
    $edit_value = null;
    $url = 'sampel/pendaftaran/set_sampel/'.$id_uji;
    $no_sampel = $this->m_pendaftaran->set_nomor_sampel();
    if(!empty($temporarySampel)){
        $edit_value = $temporarySampel;
    }
    if(!empty($editSampel)){
        $edit_value = $editSampel;
        $url .= '/'.$edit_value['id'];
    }
?>
<form class="form-horizontal" method="post" action="<?= base_url($url); ?>">
    <input type="hidden" name="<?= $this->security->get_csrf_token_name();?>" value="<?= $this->security->get_csrf_hash();?>">
    <?php if($edit_value) : ?>

    <?php endif; ?>
    <div class="row">
        <div class="col-lg-12">
            <!-- begin panel -->
            <div class="panel panel-grey">
                <div class="panel-heading">
                    <a href="#" onclick="goBack()" class="btn btn-primary m-b-5"><span class="fa fa-arrow-left"></span> Kembali</a>
                    <hr>
                </div>
                <div class="panel-body">
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Asal Sampel<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control asal_sampel" name="asal_sampel" <?= !empty($editSampel) ? "value='".$editSampel['asal_sampel']."'": "value='".$detail_uji['instansi_umum']."'"; ?> type="text" placeholder="Asal Sampel" required>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Regsitrasi Laboratorium/Nomor Uji<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select class="form-control select2 id_uji" name="id_uji" required>
                                <option></option>
                                <?php foreach ($data_nomor_uji as $r) { ?>
                                    <option <?= opt_selected($edit_value, 'id_uji', $r['id']); ?>>
                                        <?= $r['registrasi_laboratorium'].' ['.$r['nomor_uji'].']'; ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Tahun<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control tahun" onkeyup="cek_nomor_sampel();" name="tahun" value="<?= !empty($editSampel) ? $editSampel['tahun'] : date('Y') ?>" required>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Nomor Sampel<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control nomor_sampel" onkeyup="cek_nomor_sampel();" name="nomor_sampel" <?= !empty($editSampel) ? "value='".$editSampel['nomor_sampel']."'": "value='".$no_sampel."'"; ?> type="text" placeholder="Nomor Sampel" required>
                            <span class='text-danger' id='notif_cek_nomor_sampel'></span>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Klasifikasi<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select class="form-control select2 klasifikasi" onchange="pilihJenisSampel(this.value,1)" name="klasifikasi" required>
                                <option></option>
                                <?php foreach ($klasifikasi as $r) { ?>
                                    <option <?= opt_selected($edit_value, 'klasifikasi', $r['id']); ?>>
                                        <?= $r['nama']; ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12 golongan_sampel_div">
                        <label class="col-md-3 col-form-label control-label">Golongan Sampel<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input type="hidden" name="id_keterangan">
                            <select class="form-control select2 id_keterangan" onchange="pilihJenisSampel(this.value,2)" name="id_keterangan">
                                <option></option>
                                <?php foreach ($gol_sampel as $r) { ?>
                                    <!-- <option <?= opt_selected($edit_value, 'id_keterangan', $r['id']); ?>>
                                        <?= $r['nama']; ?>
                                    </option> -->
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12 jenis_sampel_div">
                        <label class="col-md-3 col-form-label control-label">Jenis Sampel<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                        <input type="hidden" name="id_jenis_sampel">
                            <select class="form-control select2 id_jenis_sampel" name="id_jenis_sampel">
                                <option></option>
                                <?php foreach ($jenis_sampel as $r) { ?>
                                    <option <?= opt_selected($edit_value, 'id_jenis_sampel', $r['id']); ?>>
                                        <?= $r['nama']. (isset($r['kode']) ? '['.$r['kode'].']' : ''); ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Merk Dagang</label>
                        <div class="col-md-6">
                            <input class="form-control merk_dagang" type="text" <?= input_edit($edit_value, 'merk_dagang', 'text',0); ?>>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Kualitas<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select class="form-control select2 kualitas" name="kualitas" required>
                                <option></option>
                                <option <?= opt_selected($edit_value, 'kualitas', 'Baik'); ?>>Baik</option>
                                <option <?= opt_selected($edit_value, 'kualitas', 'Tidak Baik'); ?>>Tidak Baik</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Bentuk/Jenis<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control bentuk_jenis" type="text" <?= input_edit($edit_value, 'bentuk_jenis'); ?>>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Jenis Pengujian<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select name="jenis_pengujian[]" id="jenis_pengujian" onchange="setproksimat()" class="form-control  select2 jenis_pengujian" multiple="" required>
                                <option></option>
                                <?php foreach ($jenis_pengujian as $r) { ?>
                                    <option <?= multi_selected($edit_value, 'jenis_pengujian', $r['id']); ?>>
                                        <?= (in_array($r['id'], [11, 12])) ? $r['nama'].' ['.$r['singkatan'].']' : $r['nama']; ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Label Pakan<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control label_pakan" type="text" <?= input_edit($edit_value, 'label_pakan'); ?>>
                        </div>
                    </div>
                    <?php if(in_array($detail_uji['jenis_layanan'], ['Aktif', 'Khusus', 'Pasif Sertifikasi', 'Pasif Pesanan Khusus'])) { ?>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Petugas Pengambil Sampel<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control petugas_pengambil_sampel" <?= input_edit($edit_value, 'petugas_pengambil_sampel'); ?>>
                        </div>
                    </div>
                    <?php }else{ ?>
                        <input type="hidden" name="petugas_pengambil_sampel">
                    <?php } ?>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Penanganan Sampel<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select class="form-control select2 id_penanganan_sampel" name="id_penanganan_sampel" required>
                                <option></option>
                                <?php foreach ($penanganan_sampel as $r) { ?>
                                    <option <?= opt_selected($edit_value, 'id_penanganan_sampel', $r['id']); ?>>
                                        <?= $r['nama']; ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Berat Sampel<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control berat_sampel" type="text" <?= input_edit($edit_value, 'berat_sampel'); ?>>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Satuan Berat<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select class="form-control satuan_sampel select2" name="satuan_sampel">
                                <option></option>
                                <option <?= opt_selected($edit_value, 'satuan_berat', 'gram'); ?>>gram</option>
                                <option <?= opt_selected($edit_value, 'satuan_berat', 'mili liter'); ?>>mili liter</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Kemasan<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <input class="form-control kemasan" <?= input_edit($edit_value, 'kemasan'); ?>>
                        </div>
                    </div>
                    <div class="form-group m-b-12 p_sampel_kerja">
                        <label class="col-md-3 col-form-label control-label">Penyimpanan Sampel Kerja<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select class="form-control select2 id_sampel_kerja" name="id_sampel_kerja">
                                <option></option>
                                <?php foreach ($sampel_kerja as $r) { ?>
                                    <option <?= opt_selected($edit_value, 'id_sampel_kerja', $r['id']); ?>>
                                        <?= $r['nama']; ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label">Menggunakan Phytase/Fitase<span class="text-danger">*</span></label>

                        <div class="col-md-6">
                            <select class="form-control select2 " name="phytase" required>
                                <option></option>
                                <option <?= opt_selected($edit_value, 'phytase', '1'); ?>>Ya</option>
                                <option <?= opt_selected($edit_value, 'phytase', '0'); ?>>Tidak</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group m-b-12 p_sampel_arsip">
                        <label class="col-md-3 col-form-label control-label">Penyimpanan Sampel Arsip<span class="text-danger">*</span></label>
                        <div class="col-md-6">
                            <select class="form-control select2 id_sampel_arsip" name="id_sampel_arsip">
                                <option></option>
                                <?php foreach ($sampel_arsip as $r) { ?>
                                    <option <?= opt_selected($edit_value, 'id_sampel_arsip', $r['id']); ?>>
                                        <?= $r['nama']; ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <div class="form-group m-b-12">
                        <label class="col-md-3 col-form-label control-label"></label>
                        <div class="col-md-6">
                            <button type='submit' class='btn btn-success m-b-5' style='margin-right: 5px;'>Simpan</button>
                            <a href="#" onclick="goBack()" class="btn btn-danger m-b-5">Batal</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<script>
    $('.p_sampel_kerja, .p_sampel_arsip').hide();
    <?php if(!empty($edit_value['klasifikasi']) && $edit_value['klasifikasi'] == 3) : ?>
        $('.id_jenis_sampel').prop('disabled', 'disabled');
    <?php endif; ?>
    function pilihJenisSampel(x, type) {
        var idKlasifikasi = $(".klasifikasi").val();
        var idGolSampel = $(".id_keterangan").val();

        if(idKlasifikasi != '') {
            $.post( "<?= site_url('sampel/pendaftaran/get_jenis_sampel') ?>", { id_klasifikasi: idKlasifikasi, id_gol_sampel: idGolSampel}, function( data ) {
                data = JSON.parse(data);
                $(".id_jenis_sampel").html(data['jenis_sampel']);
                $(".id_keterangan").html(data['gol_sampel']);
            });
        }
        if(idKlasifikasi == 3){
            $('.id_jenis_sampel').append('<option selected="selected"></option>');
            $('.id_jenis_sampel').prop('disabled', 'disabled');
        }else{
            $('.id_jenis_sampel').prop('disabled', false);
        }
    }

</script>
<script>
    function cek_nomor_sampel()
    {
        var nomor_sampel = $('.nomor_sampel').val();
        var tahun = $('.tahun').val();
        var nomor_sampel_edit = "<?= (!empty($editSampel) ? $editSampel['nomor_sampel'] : 'kosong');?>";

        $.post( "<?= base_url('sampel/pendaftaran/cek_nomor_sampel'); ?>", { nomor_sampel: nomor_sampel, tahun: tahun, nomor_sampel_edit: nomor_sampel_edit}, function( data ) {
            if(data == 1){
                $('#notif_cek_nomor_sampel').html('Nomor sampel yang Anda masukkan telah digunakan, Silahkan masukkan nomor lain');
            }else{
                $('#notif_cek_nomor_sampel').html('');
            }
        });
    }
</script>