<?php
include 'koneksi.php';  // Menghubungkan ke database

// Menangani input pencarian
$search_query = "";
if (isset($_POST['search'])) {
    $search_query = mysqli_real_escape_string($conn, $_POST['search']);
}

// Proses update anggota
if (isset($_POST['update'])) {
    $id = $_POST['edit_id'];
    $nama = $_POST['edit_nama'];
    $jk = $_POST['edit_jk'];

    if (empty($nama) || empty($jk)) {
        echo "<script>alert('Semua kolom harus diisi.');</script>";
    } else {
        $query_update = "UPDATE anggota SET nama_anggota = '$nama', jenis_kelamin = '$jk' WHERE id_anggota = '$id'";
        if (mysqli_query($conn, $query_update)) {
            echo "<script>alert('Data berhasil diperbarui!'); window.location='?pages=anggota';</script>";
            exit;
        } else {
            echo "<script>alert('Gagal memperbarui data.');</script>";
        }
    }
}

// Query untuk mengambil data anggota dengan filter pencarian
$query = "SELECT id_anggota, nama_anggota, jenis_kelamin FROM anggota 
          WHERE nama_anggota LIKE '%$search_query%' OR id_anggota LIKE '%$search_query%' 
          ORDER BY id_anggota ASC";

$result = mysqli_query($conn, $query);

// Menangani penghapusan anggota
if (isset($_GET['hapus_id'])) {
    $id_anggota = $_GET['hapus_id'];
    if (is_numeric($id_anggota)) {
        $query_delete = "DELETE FROM anggota WHERE id_anggota = '$id_anggota'";
        if (mysqli_query($conn, $query_delete)) {
            echo "<script>alert('Anggota berhasil dihapus!'); window.location='?pages=anggota';</script>";
            exit;
        } else {
            echo "<script>alert('Gagal menghapus anggota.'); window.location='?pages=anggota';</script>";
            exit;
        }
    } else {
        echo "<script>alert('ID anggota tidak valid.'); window.location='?pages=anggota';</script>";
        exit;
    }
}

// Menambahkan anggota
if (isset($_POST['submit'])) {
    $nama_anggota = $_POST['nama_anggota'];
    $jenis_kelamin = $_POST['jenis_kelamin'];
    if (empty($nama_anggota) || empty($jenis_kelamin)) {
        echo "<script>alert('Semua kolom harus diisi.');</script>";
    } else {
        $query_insert = "INSERT INTO anggota (nama_anggota, jenis_kelamin) 
                            VALUES ('$nama_anggota', '$jenis_kelamin')";
        if (mysqli_query($conn, $query_insert)) {
            echo "<script>alert('Anggota berhasil ditambahkan!'); window.location='?pages=anggota';</script>";
            exit;
        } else {
            echo "<script>alert('Gagal menambahkan anggota.');</script>";
        }
    }
}
?>

<main class="main-content position-relative border-radius-lg ">
    <div class="container-fluid py-4">
        <div class="header-section">
            <h5 class="page-title">
                <span class="text-white">Anggota</span>
            </h5>
            <div> <button class="btn btn-success-custom" data-bs-toggle="modal" data-bs-target="#addAnggotaModal">
                    <i class="mdi mdi-plus-circle"></i> Tambah Anggota
                </button>
            </div>
        </div>
        <div class="card shadow-sm rounded-4">
            <div class="card-body">
                <div class="table-responsive">
                    <table id="DataTable" class="table table-hover table-bordered">
                        <thead class="text-center">
                            <tr>
                                <th style="width: 5%;">No</th>
                                <th>Nama Anggota</th>
                                <th>Jenis Kelamin</th>
                                <th class="aksi" style="width: 20%;">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $no = 1;
                            while ($row = mysqli_fetch_assoc($result)) : ?>
                                <tr>
                                    <td class="text-center"><?= $no++ ?></td>
                                    <td><?= htmlspecialchars($row['nama_anggota']) ?></td>
                                    <td>
                                        <span class="badge bg-<?= $row['jenis_kelamin'] == 'L' ? 'primary' : 'pink' ?>">
                                            <?= $row['jenis_kelamin'] == 'L' ? 'Laki-laki' : 'Perempuan' ?>
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <button
                                            class="btn btn-warning btn-sm"
                                            data-bs-toggle="modal"
                                            data-bs-target="#editAnggotaModal"
                                            data-id="<?= $row['id_anggota'] ?>"
                                            data-nama="<?= htmlspecialchars($row['nama_anggota']) ?>"
                                            data-jk="<?= $row['jenis_kelamin'] ?>">
                                            <i class="mdi mdi-pencil"></i> Edit
                                        </button>
                                        <a href="?pages=anggota&hapus_id=<?= $row['id_anggota'] ?>" onclick="return confirm('Yakin hapus?')" class="btn btn-danger btn-sm">
                                            <i class="mdi mdi-delete"></i> Hapus
                                        </a>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="modal fade" id="addAnggotaModal" tabindex="-1">
            <div class="modal-dialog">
                <form class="modal-content" method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Anggota</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label>Nama Anggota</label>
                            <input type="text" name="nama_anggota" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label>Jenis Kelamin</label>
                            <select name="jenis_kelamin" class="form-select" required>
                                <option value="L">Laki-laki</option>
                                <option value="P">Perempuan</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" name="submit" class="btn btn-primary">Simpan</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="modal fade" id="editAnggotaModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <form class="modal-content" method="POST">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit Anggota</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="edit_id" id="edit-id">
                        <div class="mb-3">
                            <label>Nama Anggota</label>
                            <input type="text" name="edit_nama" id="edit-nama" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label>Jenis Kelamin</label>
                            <select name="edit_jk" id="edit-jk" class="form-select" required>
                                <option value="L">Laki-laki</option>
                                <option value="P">Perempuan</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" name="update" class="btn btn-primary">Update</button>
                    </div>
                </form>
            </div>
        </div>

        <script>
            var editModal = document.getElementById('editAnggotaModal');

            editModal.addEventListener('show.bs.modal', function(event) {
                var button = event.relatedTarget;
                var id = button.getAttribute('data-id');
                var nama = button.getAttribute('data-nama');
                var jk = button.getAttribute('data-jk');

                document.getElementById('edit-id').value = id;
                document.getElementById('edit-nama').value = nama;
                document.getElementById('edit-jk').value = jk;
            });
        </script>

    </div>
</main>