<?php
include "koneksi.php";

// ==== DELETE ====
if (isset($_GET['hapus'])) {
    $id = intval($_GET['hapus']);
    $conn->query("DELETE FROM tabel_pelanggan WHERE id=$id");
    echo "<script>alert('Pelanggan berhasil dihapus!'); location.href='index.php?page=pelanggan';</script>";
}

// ==== SEARCH + PAGINATION ====
$limit  = 5; // jumlah data per halaman
$page   = isset($_GET['page_num']) ? (int)$_GET['page_num'] : 1;
$start  = ($page - 1) * $limit;

$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$where  = $search ? "WHERE nama LIKE '%$search%' OR telepon LIKE '%$search%'" : "";

// Hitung total data
$totalResult = $conn->query("SELECT COUNT(*) AS total FROM tabel_pelanggan $where");
$totalData   = $totalResult->fetch_assoc()['total'];
$totalPages  = ceil($totalData / $limit);

// Ambil data pelanggan
$pelanggan = $conn->query("SELECT * FROM tabel_pelanggan $where ORDER BY id DESC LIMIT $start, $limit");
?>

<h2 class="mb-4">Data Pelanggan</h2>

<div class="d-flex justify-content-between mb-3">
    <a href="index.php?page=pelanggan_tambah" class="btn btn-primary">
        <i class="bi bi-plus-circle"></i> Tambah Pelanggan
    </a>
    <form method="get" class="d-flex">
        <input type="hidden" name="page" value="pelanggan">
        <input type="text" name="search" class="form-control me-2" 
               placeholder="Cari nama / telepon..." value="<?= htmlspecialchars($search) ?>">
        <button type="submit" class="btn btn-secondary">
            <i class="bi bi-search"></i>
        </button>
    </form>
</div>

<table class="table table-bordered table-striped">
    <thead class="table-dark">
        <tr>
            <th>No</th>
            <th>Nama</th>
            <th>Alamat</th>
            <th>Telepon</th>
            <th>Email</th>
            <th width="100">Aksi</th>
        </tr>
    </thead>
    <tbody>
    <?php if ($pelanggan->num_rows > 0): ?>
        <?php $no=$start+1; while($row = $pelanggan->fetch_assoc()): ?>
        <tr>
            <td><?= $no++ ?></td>
            <td><?= $row['nama'] ?></td>
            <td><?= $row['alamat'] ?></td>
            <td><?= $row['telepon'] ?></td>
            <td><?= $row['email'] ?></td>
            <td class="text-center">
                <a href="index.php?page=pelanggan_edit&id=<?= $row['id'] ?>" 
                   class="btn btn-sm btn-warning" title="Edit">
                   <i class="fas fa-pen-square"></i>
                </a>
                <a href="index.php?page=pelanggan&hapus=<?= $row['id'] ?>" 
                   class="btn btn-sm btn-danger" 
                   onclick="return confirm('Yakin hapus pelanggan ini?')" title="Hapus">
                   <i class="fas fa-trash"></i>
                </a>
            </td>
        </tr>
        <?php endwhile; ?>
    <?php else: ?>
        <tr><td colspan="6" class="text-center">Data tidak ditemukan</td></tr>
    <?php endif; ?>
    </tbody>
</table>

<!-- Pagination -->
<nav>
    <ul class="pagination justify-content-center">
        <?php if ($page > 1): ?>
            <li class="page-item">
                <a class="page-link" href="index.php?page=pelanggan&page_num=<?= $page-1 ?>&search=<?= urlencode($search) ?>">Sebelumnya</a>
            </li>
        <?php endif; ?>
        <?php for ($i=1; $i <= $totalPages; $i++): ?>
            <li class="page-item <?= ($i==$page) ? 'active' : '' ?>">
                <a class="page-link" href="index.php?page=pelanggan&page_num=<?= $i ?>&search=<?= urlencode($search) ?>"><?= $i ?></a>
            </li>
        <?php endfor; ?>
        <?php if ($page < $totalPages): ?>
            <li class="page-item">
                <a class="page-link" href="index.php?page=pelanggan&page_num=<?= $page+1 ?>&search=<?= urlencode($search) ?>">Berikutnya</a>
            </li>
        <?php endif; ?>
    </ul>
</nav>
