<?php
include "koneksi.php";

// === TAMBAH DATA SUPPLIER ===
if (isset($_POST['tambah'])) {
    $stmt = $conn->prepare("INSERT INTO tabel_supplier (nama_supplier,kontak,telepon,email,alamat) VALUES (?,?,?,?,?)");
    $stmt->bind_param("sssss", $_POST['nama_supplier'], $_POST['kontak'], $_POST['telepon'], $_POST['email'], $_POST['alamat']);
    if ($stmt->execute()) {
        echo "<script>alert('Supplier berhasil ditambahkan!');location.href='index.php?page=supplier';</script>";
    } else {
        echo "Error: " . $stmt->error;
    }
}

// === UPDATE DATA SUPPLIER ===
if (isset($_POST['update'])) {
    $stmt = $conn->prepare("UPDATE tabel_supplier SET nama_supplier=?, kontak=?, telepon=?, email=?, alamat=? WHERE id=?");
    $stmt->bind_param("sssssi", $_POST['nama_supplier'], $_POST['kontak'], $_POST['telepon'], $_POST['email'], $_POST['alamat'], $_POST['id']);
    if ($stmt->execute()) {
        echo "<script>alert('Data supplier berhasil diperbarui!');location.href='index.php?page=supplier';</script>";
    } else {
        echo "Error: " . $stmt->error;
    }
}

// === HAPUS DATA SUPPLIER ===
if (isset($_GET['hapus'])) {
    $id = (int)$_GET['hapus'];
    $conn->query("DELETE FROM tabel_supplier WHERE id=$id");
    echo "<script>alert('Supplier berhasil dihapus!');location.href='index.php?page=supplier';</script>";
}

// === PAGINATION & SEARCH ===
$limit  = 10;
$page   = isset($_GET['page_num']) ? (int)$_GET['page_num'] : 1;
$offset = ($page - 1) * $limit;

$search = $_GET['search'] ?? '';
$where  = $search ? "WHERE nama_supplier LIKE '%$search%' OR kontak LIKE '%$search%'" : "";

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

// Ambil data supplier
$qSupplier = $conn->query("SELECT * FROM tabel_supplier $where ORDER BY id DESC LIMIT $limit OFFSET $offset");

// === Jika mode edit ===
$editData = null;
if (isset($_GET['edit'])) {
    $idEdit = (int)$_GET['edit'];
    $editData = $conn->query("SELECT * FROM tabel_supplier WHERE id=$idEdit")->fetch_assoc();
}
?>

<div class="container mt-4">
    <h3>Data Supplier</h3>

    <!-- Form Tambah / Edit Supplier -->
    <form method="post" class="row g-2 mb-3">
        <input type="hidden" name="id" value="<?= $editData['id'] ?? '' ?>">
        <div class="col-md-3">
            <input type="text" name="nama_supplier" value="<?= $editData['nama_supplier'] ?? '' ?>" class="form-control" placeholder="Nama Supplier" required>
        </div>
        <div class="col-md-2">
            <input type="text" name="kontak" value="<?= $editData['kontak'] ?? '' ?>" class="form-control" placeholder="Kontak">
        </div>
        <div class="col-md-2">
            <input type="text" name="telepon" value="<?= $editData['telepon'] ?? '' ?>" class="form-control" placeholder="Telepon">
        </div>
        <div class="col-md-2">
            <input type="email" name="email" value="<?= $editData['email'] ?? '' ?>" class="form-control" placeholder="Email">
        </div>
        <div class="col-md-3">
            <input type="text" name="alamat" value="<?= $editData['alamat'] ?? '' ?>" class="form-control" placeholder="Alamat">
        </div>
        <div class="col-md-12 mt-2">
            <?php if ($editData): ?>
                <button type="submit" name="update" class="btn btn-warning btn-sm">
                    <i class="bi bi-pencil"></i> Update
                </button>
                <a href="index.php?page=supplier" class="btn btn-secondary btn-sm">Batal</a>
            <?php else: ?>
                <button type="submit" name="tambah" class="btn btn-success btn-sm">
                    <i class="bi bi-plus"></i> Tambah
                </button>
            <?php endif; ?>
        </div>
    </form>

    <!-- Pencarian -->
    <form method="get" class="mb-3">
        <input type="hidden" name="page" value="supplier">
        <div class="input-group">
            <input type="text" name="search" value="<?= htmlspecialchars($search) ?>" class="form-control" placeholder="Cari supplier...">
            <button class="btn btn-primary" type="submit"><i class="bi bi-search"></i> Cari</button>
        </div>
    </form>

    <!-- Tabel Data Supplier -->
    <table class="table table-bordered table-sm">
        <thead class="table-light">
            <tr>
                <th>No</th>
                <th>Nama Supplier</th>
                <th>Kontak</th>
                <th>Telepon</th>
                <th>Email</th>
                <th>Alamat</th>
                <th width="100">Aksi</th>
            </tr>
        </thead>
        <tbody>
        <?php if($qSupplier->num_rows > 0): $no = $offset+1; ?>
            <?php while($row = $qSupplier->fetch_assoc()): ?>
            <tr>
                <td><?= $no++ ?></td>
                <td><?= htmlspecialchars($row['nama_supplier']) ?></td>
                <td><?= htmlspecialchars($row['kontak']) ?></td>
                <td><?= htmlspecialchars($row['telepon']) ?></td>
                <td><?= htmlspecialchars($row['email']) ?></td>
                <td><?= htmlspecialchars($row['alamat']) ?></td>
                <td class="text-center">
                    <a href="index.php?page=supplier&edit=<?= $row['id'] ?>" class="btn btn-warning btn-sm"><i class="fas fa-pen"></i></a>
                    <a href="index.php?page=supplier&hapus=<?= $row['id'] ?>" onclick="return confirm('Yakin hapus supplier ini?')" class="btn btn-danger btn-sm"><i class="fas fa-trash"></i></a>
                </td>
            </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr><td colspan="7" class="text-center">Belum ada data supplier</td></tr>
        <?php endif; ?>
        </tbody>
    </table>

    <!-- Pagination -->
    <nav>
        <ul class="pagination">
            <?php for($i=1; $i <= $totalPage; $i++): ?>
                <li class="page-item <?= ($i==$page)?'active':'' ?>">
                    <a class="page-link" href="index.php?page=supplier&search=<?= urlencode($search) ?>&page_num=<?= $i ?>"><?= $i ?></a>
                </li>
            <?php endfor; ?>
        </ul>
    </nav>
</div>

