<!-- 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">Peminjaman</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="#peminjaman" 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_peminjaman.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>
            </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>Id Peminjaman</th>
                            <th>Nama Barang</th>
                            <th>Jumlah</th>
                            <th>Tanggal Pinjam</th>
                            <th>Tanggal Kembali</th>
                            <th>Nama Konsumen</th>
                            <th>Nama Divisi</th>
                            <th>Status</th>
                            <th>Keterangan</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $n = 1;
                        $query = mysqli_query($con, "SELECT x.*,x1.nama_barang,x3.nama_konsumen,x4.nama_divisi
                        FROM peminjaman x 
                        JOIN barang x1 ON x1.idbarang=x.barang_id 
                        JOIN konsumen x3 ON x3.id_konsumen = x.konsumen_id
                        JOIN divisi x4 ON x4.id_divisi = x.divisi_id
                        ORDER BY x.id_peminjaman DESC") or die(mysqli_error($con));
                        // $tgl_sekarang = strtotime(date("Y-m-d"));
                        $date = new DateTime();
                        $day_late = $date->format('d');

                        while ($row = mysqli_fetch_array($query)) :
                            $tgl_kembali = $row['tgl_kembali'];
                            $stamp = strtotime($tgl_kembali);
                            $day = date('d', $stamp);
                            $selisih = $day - $day_late;
                        ?>
                            <tr>
                                <td><?= $n++; ?></td>
                                <td><?= $row['id_peminjaman']; ?></td>
                                <td><?= $row['nama_barang']; ?></td>
                                <td><?= $row['jumlah']; ?></td>
                                <td><?= $row['tgl_pinjam']; ?></td>
                                <td><?= $row['tgl_kembali']; ?></td>
                                <td><?= $row['nama_konsumen']; ?></td>
                                <td><?= $row['nama_divisi']; ?></td>
                                <td>
                                    <?php
                                    if ($row['status'] == null || $row['status'] == 0) {
                                    ?>
                                        <span class="label label-warning">Belum dikembalikan</span>
                                    <?php } else {
                                    ?>
                                        <span class="label label-primary">Sudah dikembalikan</span>
                                    <?php
                                    }
                                    ?>
                                </td>
                                <td>
                                    <?php
                                    if ($selisih < 0) {
                                    ?>
                                        Telat <b style="color: red;"><?= $selisih ?></b> Hari <br>
                                    <?php
                                    } elseif ($selisih <= 0) {
                                    ?>
                                        Hari ini wajib mengembalikan barang
                                    <?php
                                    } else {
                                    ?>
                                        Masih ada waktu <?= $selisih ?> Hari
                                    <?php
                                    }
                                    ?>
                                </td>
                                <td>
                                    <?php
                                    if ($row['status'] == null || $row['status'] == 0) {
                                    ?>
                                        <form action="<?= base_url(); ?>process/pengembalian.php" method="post">
                                            <input type="hidden" name="id" value="<?= $row['id_peminjaman'] ?>">
                                            <input type="hidden" name="stok" value="<?= $row['jumlah'] ?>">
                                            <input type="hidden" name="barang" value="<?= $row['barang_id'] ?>">
                                            <button class="btn btn-primary btn-xs btn-kembali" name="tambah"><i>Kembalikan</i></button>
                                        </form>
                                    <?php } else {
                                    ?>
                                        <span class="label label-primary">Barang kembali</span>
                                    <?php
                                    }
                                    ?>

                                </td>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

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

<!-- Modal Tambah barang -->
<div class="modal fade" id="peminjaman" 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/peminjaman.php" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Tambah Peminjaman</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="id_peminjaman">Id Peminjaman <span class="text-danger">*</span></label>
                                <?php
                                $nilai = 5;
                                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="id_peminjaman" id="id_peminjaman" value="PM-<?php echo RandomText($nilai); ?>" class="form-control" readOnly>

                            </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="tgl_pinjam">Tanggal Pinjam<span class="text-danger">*</span></label>
                                <input type="date" class="form-control" id="tgl_pinjam" name="tgl_pinjam" value="<?= date('Y-m-d'); ?>" >
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="tgl_kembali">Tanggal Kembali<span class="text-danger">*</span></label>
                                <input type="date" class="form-control" id="tgl_kembali" name="tgl_kembali" value="<?= date('Y-m-d'); ?>" >
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="divisi_id">Nama Divisi <span class="text-danger">*</span></label>
                                <select name="divisi_id" id="divisi_id" class="form-control select2" style="width:100%;" 
                                required oninvalid="InvalidMsgCBDvs(this)" oninput="InvalidMsgCBDvs(this)" >
                                    <option value="">-- Pilih Divisi --</option>
                                    <?= list_divisi(); ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="konsumen_id">Nama Konsumen <span class="text-danger">*</span></label>
                                <select name="konsumen_id" id="konsumen_id" class="form-control select2" style="width:100%;" 
                                required oninvalid="InvalidMsgCBKsm(this)" oninput="InvalidMsgCBKsm(this)" >
                                    <option value="">-- Pilih Konsumen --</option>
                                    <?= list_konsumen(); ?>
                                </select>
                            </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 InvalidMsgCBDvs(combobox) {
                                    if (combobox.value === '') {
                                        combobox.setCustomValidity
                                        ('Silahkan Pilih Divisi!');
                                    } else {
                                        combobox.setCustomValidity('');
                                    }
                                        return true;
                                    }
                         </script>
                         <script>
                                function InvalidMsgCBKsm(combobox) {
                                    if (combobox.value === '') {
                                        combobox.setCustomValidity
                                        ('Silahkan Pilih Konsumen!');
                                    } 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>