<script>
function submit(x) {
    if (x == 'add') {
        $('[name="barang_id"]').val("").trigger('change');
        $('[name="merek_id"]').val("").trigger('change');
        $('[name="jumlah"]').val("");
        $('[name="satuan"]').val("");
        $('#barang_masuk .modal-title').html('Tambah Barang');
        $('[name="ubah"]').hide();
        $('[name="tambah"]').show();
    } 
}
</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 Masuk</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="#barang_masuk"
            onclick="submit('add')">
                <span class="icon text-white-50">
                    <i class="fas fa-plus"></i>
                </span>
                <span class="text">Tambah</span>
            </a>
            <a href="<?=base_url();?>process/cetak_barang_masuk.php" target="_blank"
                class="btn btn-info btn-icon-split btn-sm float-right">
                <span class="icon text-white-50">
                    <i class="fas fa-print"></i>
                </span>
                <span class="text">Cetak</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>Tanggal</th>
                            <th>Tanggal PO</th>
                            <th>No PO</th>
                            <th>Project Name</th>
                            <th>Nama Barang</th>
                            <th>Merek</th>
                            <th>Tipe</th>
                            <th>Jumlah Masuk</th>
                            <th>Satuan</th>
                            <th>Keterangan</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $n=1;
                        $query = mysqli_query($con,"SELECT x.*,x1.nama_barang,x1.tipe,x2.nama_merek,x3.nama_satuan
                        FROM barang_masuk x 
                        JOIN barang x1 ON x1.idbarang=x.barang_id 
                        JOIN merek x2 ON x2.idmerek=x1.merek_id 
                        JOIN satuan x3 ON x3.id_satuan=x.satuan_id
                        ORDER BY x.idbarang_masuk DESC")or die(mysqli_error($con));
                        while($row = mysqli_fetch_array($query)):
                        ?>
                        <tr>
                            <td><?= $n++; ?></td>
                            <td><?= date('d-m-Y',strtotime($row['tanggal'])); ?></td>
                            <td><?= date('d-m-Y',strtotime($row['tanggal_po'])); ?></td>
                            <td><?= $row['no_po']; ?></td>
                            <td><?= $row['project_name']; ?></td>
                            <td><?= $row['nama_barang']; ?></td>
                            <td><?= $row['nama_merek']; ?></td>
                            <td><?= $row['tipe']; ?></td>
                            <td><?= $row['jumlah']; ?></td>
                            <td><?= $row['nama_satuan']; ?></td>
                            <td><?= $row['keterangan']; ?></td>
                            <td>
                                <a href="<?=base_url();?>/process/barang_masuk.php?act=<?=encrypt('delete');?>&id=<?=encrypt($row['idbarang_masuk']);?>"
                                    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="barang_masuk" 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_masuk.php" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Tambah Barang Masuk</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-6">
                            <div class="form-group">
                                <label for="tanggal">Tanggal<span class="text-danger">*</span></label>
                                <input type="date" class="form-control" id="tanggal" name="tanggal"
                                    value="<?=date('Y-m-d');?>" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="barang_id">Nama Barang <span class="text-danger">*</span></label>
                                <select name="barang_id" id="barang_id" class="form-control select2" style="width:100%;"
                                    required oninvalid="InvalidMsgCBBrg(this)" oninput="InvalidMsgCBBrg(this)">
                                    <option value="">-- Pilih Barang --</option>
                                    <?= list_barang(); ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="jumlah">Jumlah<span class="text-danger">*</span></label>
                                <input type="text" class="form-control uang" id="jumlah" name="jumlah" required
                                oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                    <label for="satuan_id">Satuan <span class="text-danger">*</span></label>
                                       <select name="satuan_id" id="satuan_id" class="form-control select2" style="width:100%;"
                                          required oninvalid="InvalidMsgCBStn(this)" oninput="InvalidMsgCBStn(this)">
                                         <option value="">-- Pilih Satuan --</option>
                                         <?= list_satuan(); ?>
                                     </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="no_po">No PO <span class="text-danger">*tidak perlu diisi jika barang tidak ada No PO</span></label>
                                    <input type="text" class="form-control" id="no_po" name="no_po" maxlength="25">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="tanggal_po">Tanggal PO<span class="text-danger">*</span></label>
                                <input type="date" class="form-control" id="tanggal_po" name="tanggal_po"
                                    value="<?=date('Y-m-d');?>" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                            <label for="project_name">Project Name <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="project_name" name="project_name" required
                                    oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)" maxlength="40">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="keterangan">Keterangan <span class="text-danger">*</span></label>
                                <textarea name="keterangan" id="keterangan" cols="30" rows="5" class="form-control"
                                    required oninvalid="InvalidMsg(this)" oninput="InvalidMsg(this)" maxlength="205"></textarea>
                            </div>
                        </div>
                    </div>
                    <script>
                                function InvalidMsg(textbox) {
                                    if (textbox.value === '') {
                                        textbox.setCustomValidity
                                        ('Tidak Boleh Kosong!');
                                    } else {
                                        textbox.setCustomValidity('');
                                    }
                                        return true;
                                    }
                         </script>
                         <script>
                                function InvalidMsgCBBrg(combobox) {
                                    if (combobox.value === '') {
                                        combobox.setCustomValidity
                                        ('Silahkan Pilih Barang!');
                                    } else {
                                        combobox.setCustomValidity('');
                                    }
                                        return true;
                                    }
                         </script>
                         <script>
                                function InvalidMsgCBStn(combobox) {
                                    if (combobox.value === '') {
                                        combobox.setCustomValidity
                                        ('Silahkan Pilih Satuan 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>
                </div>
            </form>
        </div>
    </div>
</div>