<?php
include "asset/session_check.php";

$nama_pegawai = $_SESSION['nama_pegawai'];
$akses = $_SESSION['akses'];
?>
<!doctype html>
<html lang="en">

<head>
    <?php
    include "asset/head.php";
    ?>
</head>

<body>
    <!-- NAVIGASI -->
    <?php
    include "asset/navigasi.php";
    ?>
    <div class="container">
        <div class="row align-items-center mb-3">

            <!-- Judul -->
            <div class="col-12 col-md-4 mb-2 mb-md-0">
                <h1 class="h3 mb-0">Data Barang</h1>
            </div>

            <!-- Form -->
            <div class="col-12 col-md-8">
                <form method="GET" class="row g-2 justify-content-md-end">

                    <div class="col-12 col-sm-8 col-lg-6">
                        <input type="text"
                            name="search"
                            class="form-control"
                            placeholder="Cari nama barang..."
                            value="<?= htmlspecialchars($_GET['search'] ?? '') ?>">
                    </div>

                    <div class="col-12 col-sm-4 col-lg-3 d-grid d-md-flex gap-2">
                        <button type="submit" class="btn btn-primary w-100 w-md-auto">
                            Filter
                        </button>
                        <a href="?" class="btn btn-secondary w-100 w-md-auto">
                            Reset
                        </a>
                    </div>

                </form>
            </div>

        </div>
        <div class="row">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th scope="col">Nama Barang</th>
                            <th scope="col">Harga</th>
                            <th scope="col">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        include "koneksi.php";

                        $limit = 10;
                        $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
                        if ($page < 1) $page = 1;

                        $start = ($page - 1) * $limit;

                        // ambil keyword search
                        $search = isset($_GET['search']) ? mysqli_real_escape_string($konek, $_GET['search']) : "";

                        // kondisi where
                        $where = "";
                        if (!empty($search)) {
                            $where = "WHERE nama_barang LIKE '%$search%'";
                        }

                        // hitung total data
                        $total_result = mysqli_query($konek, "SELECT COUNT(*) as total FROM barang $where");
                        $total_row = mysqli_fetch_assoc($total_result);
                        $total_data = $total_row['total'];
                        $total_page = ceil($total_data / $limit);

                        // ambil data sesuai halaman
                        $sql = mysqli_query($konek, "SELECT * FROM barang $where ORDER BY nama_barang LIMIT $start, $limit");

                        $no = $start + 1;
                        while ($row = mysqli_fetch_array($sql)) {
                        ?>
                            <tr>
                                <td><?php echo $row['nama_barang'] ?></td>
                                <td>Rp <?= number_format($row['harga'], 0, ',', '.') ?></td>
                                <td>
                                    <!-- <a href="" class="btn btn-primary btn-sm">+</a>
                                <a href="" class="btn btn-danger btn-sm">-</a> -->
                                    <button
                                        class="btn btn-warning btn-sm"
                                        data-bs-toggle="modal"
                                        data-bs-target="#edit<?= $row['idbarang']; ?>">
                                        <i class="bi bi-pencil-fill"></i>
                                    </button>
                                </td>
                                <div class="modal fade" id="edit<?= $row['idbarang']; ?>" tabindex="-1">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <form action="function/function-barang.php" method="post">
                                                <div class="modal-header">
                                                    <h5 class="modal-title">Edit Data Barang</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>

                                                <div class="modal-body">
                                                    <input type="text" name="idbarang" value="<?= $row['idbarang']; ?>" hidden>

                                                    <div class="mb-3">
                                                        <label class="form-label">Barang</label>
                                                        <input type="text" class="form-control" autocomplete="off" name="nama_barang" id="nama_barang" value="<?= $row['nama_barang'] ?>" placeholder="Masukkan Nama Customer">
                                                        <div id="barangList"
                                                            class="list-group w-100"
                                                            style="z-index:1000; display:none;"></div>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label>Harga</label>
                                                        <input type="text" name="harga" id="rupiah" class="form-control rupiah" value="<?= $row['harga']; ?>">
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" name="edit" class="btn tmbdata">Update</button>
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </tr>
                        <?php
                            $no++;
                        }
                        ?>
                    </tbody>
                </table>
            </div>
            <nav>
                <ul class="pagination justify-content-center">

                    <!-- Previous -->
                    <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=1&search=<?= $search ?>">
                            <!-- <i class="bi bi-chevron-bar-left"></i> -->
                             <i class="bi bi-chevron-double-left"></i>
                            <!-- <i class="bi bi-chevron-left"></i> -->
                        </a>
                    </li>
                    <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $page - 1 ?>&search=<?= $search ?>">
                            <!-- <i class="bi bi-chevron-bar-left"></i> -->
                             <!-- <i class="bi bi-chevron-double-left"></i> -->
                            <i class="bi bi-chevron-left"></i>
                        </a>
                    </li>

                    <?php
                    $range = 2; // jumlah halaman di kiri & kanan aktif
                    $show_first = 1;
                    $show_last = $total_page;

                    // Tampilkan halaman pertama
                    if ($page > 3) {
                        echo '<li class="page-item"><a class="page-link" href="?page=1&search=' . $search . '">1</a></li>';
                        if ($page > 4) {
                            echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
                        }
                    }

                    // Halaman sekitar aktif
                    for ($i = max(1, $page - $range); $i <= min($total_page, $page + $range); $i++) {
                        echo '<li class="page-item ' . ($i == $page ? 'active' : '') . '">
                    <a class="page-link" href="?page=' . $i . '&search=' . $search . '">' . $i . '</a>
                  </li>';
                    }

                    // Tampilkan halaman terakhir
                    if ($page < $total_page - 2) {
                        if ($page < $total_page - 3) {
                            echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
                        }
                        echo '<li class="page-item">
                    <a class="page-link" href="?page=' . $total_page . '&search=' . $search . '">' . $total_page . '</a>
                  </li>';
                    }
                    ?>

                    <!-- Next -->
                    <li class="page-item <?= ($page >= $total_page) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $page + 1 ?>&search=<?= $search ?>">
                            <!-- <i class="bi bi-chevron-bar-right"></i> -->
                             <!-- <i class="bi bi-chevron-double-right"></i> -->
                            <i class="bi bi-chevron-right"></i>
                        </a>
                    </li>
                    <li class="page-item <?= ($page >= $total_page) ? 'disabled' : '' ?>">
                        <a class="page-link" href="?page=<?= $total_page ?>&search=<?= $search ?>">
                            <!-- <i class="bi bi-chevron-bar-right"></i> -->
                            <i class="bi bi-chevron-double-right"></i>
                            <!-- <i class="bi bi-chevron-right"></i> -->
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- customer -->
    <script src="asset/js/auto_cust.js"></script>
    <!-- wilayah -->
    <script src="asset/js/auto_wilayah.js"></script>
    <script>
        var rupiah = document.getElementById("rupiah");
        document.addEventListener("DOMContentLoaded", function() {
            document.querySelectorAll(".rupiah").forEach(function(input) {
                input.addEventListener("keyup", function() {
                    this.value = formatRupiah(this.value);
                });
            });
        });

        /* Fungsi formatRupiah */
        function formatRupiah(angka, prefix) {
            var number_string = angka.replace(/[^,\d]/g, "").toString(),
                split = number_string.split(","),
                sisa = split[0].length % 3,
                rupiah = split[0].substr(0, sisa),
                ribuan = split[0].substr(sisa).match(/\d{3}/gi);

            // tambahkan titik jika yang di input sudah menjadi angka ribuan
            if (ribuan) {
                separator = sisa ? "." : "";
                rupiah += separator + ribuan.join(".");
            }

            rupiah = split[1] != undefined ? rupiah + "," + split[1] : rupiah;
            return prefix == undefined ? rupiah : rupiah ? "Rp. " + rupiah : "";
        }
    </script>
    <!-- INI FOOTER -->
    <?php
    include "asset/footer.php";
    ?>
</body>

</html>