<?php
$input = array('nama bahan kimia', 'rumus kimia', 'bentuk', 'spesifikasi', 'merek', 'volume kemasan',
    'satuan kemasan', 'tanggal kadaluarsa', 'jenis kemasan', 'jumlah dipakai', 'jumlah stok', 'jumlah total',
    'lokasi', 'nomor katalog', 'lot', 'keterangan',
);
?>
<div class="row">
    <!-- begin col-8 -->
    <div class="col-lg-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>
                    <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-grey" data-click="panel-remove"><i class="fa fa-times"></i></a>
                </div>
                <a onclick="goBack()" class="btn btn-primary m-b-5"><span class="fa fa-arrow-left"></span> Kembali</a>
                <hr>
            </div>
            <form class="form-horizontal" method="post" action="">
            <div class="panel-body">
                <div class="col-sm-10">
                        <input type="hidden" name="id_bahan" value="<?php echo (!empty($id_bahan) ? $id_bahan : null); ?>">
                        <?php foreach ($input as $i) { ?>
                            <?php $name_input = str_replace(' ', '_', $i); ?>
                            <?php if(empty($edit) OR !empty($edit) && !in_array($name_input, array('lot', 'tanggal_kadaluarsa'))) { ?>
                            <div class="form-group m-b-11">
                                <?php if(!in_array($i, array('spesifikasi'))){
                                  $label = ucwords($i);
                                }else{
                                  $label = '';
                                } ?>
                                  <label class="col-md-3 col-form-label control-label"><?php echo $label; ?></label>
                                <div class="col-md-6">
                                  <?php if(in_array($i, array('spesifikasi'))){ // untuk radio button ?>
                                    <label class="col-form-label control-label">
                                      PA<input <?php echo (!empty($edit) && $edit[$name_input] == 'PA') ? 'checked' : ''; ?> type="radio" name="<?php echo $name_input; ?>" value="PA">
                                    </label>
                                    <label class="col-form-label control-label">
                                      Teknis<input <?php echo (!empty($edit) && $edit[$name_input] == 'Teknis') ? 'checked' : ''; ?> type="radio" name="<?php echo $name_input; ?>" value="Teknis">
                                    </label>
                                  <?php } else if(in_array($i, array('bentuk', 'jenis kemasan', 'satuan kemasan'))) { //untuk select box?>
                                    <?php
                                     if($i == 'bentuk')
                                        $array_param = array('Cair', 'Padat', 'Granule', 'Pipih', 'Tablet', 'Gas');
                                     if($i == 'jenis kemasan')
                                        $array_param = array('Botol Kaca', 'Botol Plastik', 'Jirigen', 'Plastik');
                                     if($i == 'satuan kemasan')
                                         $array_param = array('liter', 'gram');
                                    ?>
                                    <select class="form-control default-select2" name="<?php echo $name_input; ?>">
                                      <option></option>
                                      <?Php foreach($array_param as $pa){ ?>
                                        <option <?php echo (!empty($edit) && $edit[$name_input] == $pa) ? 'selected' : ''; ?> value="<?php echo $pa; ?>"><?php echo $pa; ?></option>
                                      <?php } ?>
                                    </select>
                                  <?php } else if(in_array($i, array('rumus kimia'))) { //format khusus?>
                                    <input class="form-control" name="<?php echo $name_input; ?>" type="text" placeholder="Masukkan <?php echo ucwords($i) ?>" value="<?php echo !empty($edit) ? str_replace(['<sub>', '</sub>', '</sup>'], '', str_replace('<sup>', '^', $edit[$name_input])) : $this->input->post($i) ?>">
                                  <?php } else if(in_array($i, array('tanggal kadaluarsa'))) { //untuk type date?>
                                    <input class="form-control" name="<?php echo $name_input; ?>" type="date" placeholder="Masukkan <?php echo ucwords($i) ?>" value="<?php echo !empty($edit) ? $edit[$name_input] : $this->input->post($i) ?>">
                                  <?php } else if(in_array($i, array('volume kemasan', 'jumlah stok', 'jumlah total', 'jumlah dipakai'))) { //untuk type date?>
                                    <input class="form-control <?php echo $name_input;?>" <?php echo ($name_input == 'jumlah_dipakai' ? "onkeyup='sisaBahan();'" : ''); ?> name="<?php echo $name_input; ?>" type="number" placeholder="Masukkan <?php echo ucwords($i) ?>" value="<?php echo !empty($edit) ? $edit[$name_input] : $this->input->post($i) ?>">
                                  <?php }else { ?>
                                    <input class="form-control" name="<?php echo $name_input; ?>" type="text" placeholder="Masukkan <?php echo ucwords($i) ?>" value="<?php echo !empty($edit) ? $edit[$name_input] : $this->input->post($i) ?>">
                                  <?php } ?>

                                </div>
                            </div>
                        <?php } } ?>
                </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-7">
                        <button type="submit" class="btn btn-md btn-success">Simpan</button>
                        <button type="reset" class="btn btn-md btn-warning">Reset</button>
                        <a onclick="goBack();" class="btn btn-md btn-danger">Batal</a>
                      </div>
                  </div>
                </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    function sisaBahan(){
        var g = $('.jumlah_stok').val();
        var h = $('.jumlah_dipakai').val();
				var total_ambil = 0;

				<?php if(!empty($total_pengambilan)){ ?>
					total_ambil = <?php echo $total_pengambilan; ?>
				<?php } ?>

        if(g === '') f = 0;
        if(h === '') f = 0;

        var j = parseFloat(g)-(parseFloat(h)+parseFloat(total_ambil));

        if(isNaN(parseFloat(j))){
            $('.jumlah_total').val(0);
        }else{
            $('.jumlah_total').val(j);
        }
    }
</script>
