<?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="mb-0">Data Customer</h1>
            </div>

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

                    <div class="col-12 col-sm-6 col-lg-4">
                        <input type="text" class="form-control" name="nama_customer" placeholder="Nama Customer"
                            value="<?= htmlspecialchars($_GET['nama_customer'] ?? '') ?>">
                    </div>

                    <div class="col-12 col-sm-6 col-lg-4">
                        <input type="text" class="form-control" name="pic" placeholder="Nama PIC"
                            value="<?= htmlspecialchars($_GET['pic'] ?? '') ?>">
                    </div>

                    <div class="col-12 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="customer.php" class="btn btn-secondary w-100 w-md-auto">
                            Reset
                        </a>
                    </div>
                </form>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <!-- <a href="customer-tambah.php" type="button" class="btn btn-danger">Tambah Data</a> -->
                <button class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#tambacustomer">
                    Tambah Customer
                </button>
            </div>
        </div>
        <div class="row">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Nama Customer</th>
                            <th scope="col">Alamat</th>
                            <th scope="col">Kota</th>
                            <th scope="col">Telp</th>
                            <th scope="col">Email</th>
                            <th scope="col">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        include "koneksi.php";
                        $where = [];

                        if (!empty($_GET['nama_customer'])) {
                            $customer = mysqli_real_escape_string($konek, $_GET['nama_customer']);
                            $where[] = "c.nama_customer LIKE '%$customer%'";
                        }
                        
                        $where_sql = "";
                        if (!empty($where)) {
                            $where_sql = "WHERE " . implode(" AND ", $where);
                        }

                        $limit = 10; // jumlah data per halaman
                        $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;

                        if ($page < 1) {
                            $page = 1;
                        }

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

                        $count_query = mysqli_query($konek, "
                        SELECT COUNT(DISTINCT c.idcustomer) as total
                        FROM customer c
                        $where_sql
                    ");

                        $count_data = mysqli_fetch_assoc($count_query);
                        $total_data = $count_data['total'];
                        $total_pages = ceil($total_data / $limit);

                        $no = $start + 1;
                        $sql = mysqli_query($konek, "SELECT c.* FROM customer c $where_sql GROUP BY c.idcustomer LIMIT $start, $limit");
                        while ($row = mysqli_fetch_array($sql)) {
                            ?>
                            <tr>
                                <td><?php echo $no ?></td>
                                <td><?php 
                                    if (!empty($row['nama_customer']) && strpos($row['nama_customer'], 'NULL') === 0) {
                                        echo $row['nama_customer'] ;
                                    }else{
                                        echo $row['bu'] . " " . $row['nama_customer'];
                                    }
                                    ?>
                                </td>
                                <td><?php echo $row['alamat'] ?></td>
                                <td><?php echo $row['kota'] ?></td>
                                <td><?php echo $row['telp'] ?></td>
                                <td><?php echo $row['email'] ?></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['idcustomer']; ?>">
                                        <i class="bi bi-pencil-fill"></i>
                                    </button>
                                </td>
                                <div class="modal fade" id="edit<?= $row['idcustomer']; ?>" tabindex="-1">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <form action="function/function-customer.php" method="post">
                                                <div class="modal-header">
                                                    <h5 class="modal-title">Edit Data Customer</h5>
                                                    <button type="button" class="btn-close"
                                                        data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <input type="text" name="idcustomer" value="<?= $row['idcustomer']; ?>" hidden
                                                        >
                                                    <div class="mb-3">
                                                        <label class="form-label">Customers</label>
                                                        <div class="input-group">
                                                            <select class="form-select" name="bu" id="bu"
                                                                style="max-width: 95px;">
                                                                <option value="PT ">PT</option>
                                                                <option value="CV ">CV</option>
                                                                <option value="UD ">UD</option>
                                                                <option value="Toko ">Toko</option>
                                                                <option value="Bapak ">Bapak</option>
                                                                <option value="Ibu ">Ibu</option>
                                                            </select>
                                                            <input type="text" class="form-control" autocomplete="off"
                                                                name="nama_customer" id="nama_customer"
                                                                value="<?= $row['nama_customer'] ?>"
                                                                placeholder="Masukkan Nama Customer">
                                                        </div>
                                                        <div id="customerList" class="list-group w-100"
                                                            style="z-index:1000; display:none;"></div>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label>Alamat</label>
                                                        <input type="text" name="alamat" class="form-control"
                                                            value="<?= $row['alamat']; ?>">
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label">KOTA</label>
                                                        <input type="text" id="kota" name="kota" class="form-control"
                                                            value="<?= $row['kota'] ?>">
                                                        <div id="suggestion" class="list-group"></div>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label>Telp</label>
                                                        <input type="text" name="telp" class="form-control"
                                                            value="<?= $row['telp'] ?>">
                                                    </div>
                                                    <div class="mb-3">
                                                        <label>Email</label>
                                                        <input type="text" name="email" class="form-control"
                                                            value="<?= $row['email'] ?>">
                                                    </div>
                                                    <div class="mb-3">
                                                        <label>Tgl Input</label>
                                                        <input type="date" name="tglinput" class="form-control"
                                                            value="<?= $row['tglinput'] ?>">
                                                    </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=<?= $page - 1 ?>&nama_customer=<?= $_GET['nama_customer'] ?? '' ?>&pic=<?= $_GET['pic'] ?? '' ?>">
                            Previous
                        </a>
                    </li>

                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                            <a class="page-link"
                                href="?page=<?= $i ?>&nama_customer=<?= $_GET['nama_customer'] ?? '' ?>&pic=<?= $_GET['pic'] ?? '' ?>">
                                <?= $i ?>
                            </a>
                        </li>
                    <?php endfor; ?>

                    <!-- Next -->
                    <li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
                        <a class="page-link"
                            href="?page=<?= $page + 1 ?>&nama_customer=<?= $_GET['nama_customer'] ?? '' ?>&pic=<?= $_GET['pic'] ?? '' ?>">
                            Next
                        </a>
                    </li>

                </ul>
            </nav>
        </div>
    </div>
    <div class="modal fade" id="tambacustomer" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form action="function/function-customer.php" method="post">
                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Data Customer</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">Customers</label>
                            <div class="input-group">
                                <select class="form-select" name="bu" id="bu" style="max-width: 95px;">
                                    <option value="PT ">PT</option>
                                    <option value="CV ">CV</option>
                                    <option value="UD ">UD</option>
                                    <option value="Toko ">Toko</option>
                                    <option value="Bapak ">Bapak</option>
                                    <option value="Ibu ">Ibu</option>
                                </select>
                                <input type="text" class="form-control" name="nama_customer">
                            </div>
                        </div>
                        <div class="mb-3">
                            <label>Alamat</label>
                            <input type="text" name="alamat" class="form-control">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">KOTA</label>
                            <input type="text" id="kota" name="kota" class="form-control kota">
                            <div id="suggestion" class="list-group suggestion"></div>
                        </div>
                        <div class="mb-3">
                            <label>Telp</label>
                            <input type="text" name="telp" class="form-control">
                        </div>
                        <div class="mb-3">
                            <label>Email</label>
                            <input type="text" name="email" class="form-control">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" name="tambahcustomer" class="btn tmbdata">Simpan</button>
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    </div>
                </form>
            </div>
        </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>
    <!-- INI FOOTER -->
    <?php
    include "asset/footer.php";
    ?>
</body>

</html>