
<script>
function submit(x) {
    if (x == 'add') {
        $('[name="idbarang"]');
        $('[name="nama_barang"]').val("");
        $('[name="merek_id"]').val("").trigger('change');
        $('[name="tipe"]').val("");
        $('[name="sn"]').val("");
        $('[name="satuan"]').val("");
        $('#barangModal .modal-title').html('Tambah Barang');
        $('[name="ubah"]').hide();
        $('[name="tambah"]').show();
    } else {
        $('#barangModal .modal-title').html('Ubah Barang');
        $('[name="tambah"]').hide();
        $('[name="ubah"]').show();

        $.ajax({
            type: "POST",
            data: {
                id: x
            },
            url: '<?=base_url();?>process/view_barang.php',
            dataType: 'json',
            success: function(data) {
                $('[name="idbarang"]').val(data.idbarang);
                $('[name="merek_id"]').val(data.merek_id).trigger('change');
                $('[name="nama_barang"]').val(data.nama_barang);
                $('[name="tipe"]').val(data.tipe);
                $('[name="sn"]').val(data.sn);
                $('[name="satuan"]').val(data.satuan);
            }
        });
    }
}
</script>
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Barang</h1>
    </div>
    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <a href="#" class="btn btn-primary btn-icon-split btn-sm" data-toggle="modal" data-target="#barangModal"
                onclick="submit('add')">
                <span class="icon text-white-50">
                    <i class="fas fa-plus"></i>
                </span>
                <span class="text">Tambah</span>
            </a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th width="20">NO</th>
                            <th>Kode Barang</th>
                            <th>Nama Barang</th>
                            <th>Nama Merek</th>
                            <th>Tipe</th>
                            <th>Sn</th>
                            <th min="0">Stok</th>
                            <th>Satuan</th>
                            <th width="50">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $n=1;
                        $query = mysqli_query($con,"SELECT x.*,x1.nama_merek,x2.nama_satuan
                        FROM barang x 
                        LEFT JOIN merek x1 ON x1.idmerek=x.merek_id
                        LEFT JOIN satuan x2 ON x2.id_satuan=x.satuan_id
                        ORDER BY x.idbarang DESC")or die(mysqli_error($con));
                        while($row = mysqli_fetch_array($query)):
                        ?>
                        <tr>
                            <td><?= $n++; ?></td>
                            <td><?= $row['idbarang']; ?></td>
                            <td><?= $row['nama_barang']; ?></td>
                            <td><?= $row['nama_merek']; ?></td>
                            <td><?= $row['tipe']; ?></td>
                            <td><?= $row['sn']; ?></td>
                            <td><?= $row['stok']; ?></td>
                            <td><?= $row['nama_satuan']; ?></td>
                            <td>
                                <a href="<?=base_url();?>/process/barang.php?act=<?=encrypt('delete');?>&id=<?=encrypt($row['idbarang']);?>"
                                    class="btn btn-sm btn-circle btn-danger btn-hapus"><i class="fas fa-trash"></i></a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>
<!-- /.container-fluid -->

<!-- Modal Tambah barang -->
<div class="modal fade" id="barangModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
    aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <form action="<?=base_url();?>process/barang.php" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel"></h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="idbarang">Kode Barang <span class="text-danger">*</span></label>
                                <?php
                                    $nilai = 10;
                                    function RandomText($nilai){
                                      $karakter = '0123456789';
                                      $randomString = "";
                                      for($i=0;$i<$nilai;$i++){
                                        $index=rand(0, strlen($karakter)-1);
                                        $randomString .= $karakter[$index];
                                      }
                                      return $randomString;
                                    }
                                    //echo RandomText($nilai);
                                ?>
                                 
                                 <input type="text" class="form-control" name="idbarang" id="idbarang" value="BRG-<?php echo RandomText($nilai); ?>" class="form-control" readOnly>
                                    
                                 <label for="nama_barang">Nama Barang <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="nama_barang" name="nama_barang" required 
                                oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)" maxlength="50">
                            </div>
                        </div>
                        <div class="col-md-12">
                                 <div class="form-group">
                                       <label for="merek_id">Merek Barang <span class="text-danger">*</span></label>
                                       <select name="merek_id" id="cbmerek" class="form-control select2" style="width:100%;"
                                          required oninvalid="InvalidMsgCB(this)" oninput="InvalidMsgCB(this)">
                                         <option value="">-- Pilih Merek --</option>
                                         <?= list_merek(); ?>
                                     </select>
                                     
                                </div>
                                <div class="form-group">
                                    <label for="tipe">Tipe Barang <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="tipe" name="tipe" required
                                    oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)" maxlength="40">
                                </div>
                                <div class="form-group">
                                    <label for="sn">SN <span class="text-danger">*tidak perlu diisi jika barang tidak ada no SN</span></label>
                                    <input type="text" class="form-control" id="sn" name="sn" maxlength="25">
                                    <input type="hidden" name="satuan" value="" id="satuan" class="form-control">
                            </div>
                            </div>
                        </div>
                        <script>
                                function InvalidMsg(textbox) {
                                    if (textbox.value === '') {
                                        textbox.setCustomValidity
                                        ('Tidak Boleh Kosong!');
                                    } else {
                                        textbox.setCustomValidity('');
                                    }
                                        return true;
                                    }
                         </script>
                         <script>
                                function InvalidMsgCB(combobox) {
                                    if (combobox.value === '') {
                                        combobox.setCustomValidity
                                        ('Silahkan Pilih Merek Barang!');
                                    } else {
                                        combobox.setCustomValidity('');
                                    }
                                        return true;
                                    }
                         </script>
                    <hr class="sidebar-divider">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal"><i class="fas fa-times"></i>
                        Batal</button>
                    <button class="btn btn-primary float-right" type="submit" name="tambah"><i class="fas fa-save"></i>
                        Tambah</button>
                    <button class="btn btn-primary float-right" type="submit" name="ubah"><i class="fas fa-save" ></i>
                        Ubah</button>
                </div>
            </form>
        </div>
    </div>
</div>