<?php
require_once "../includes/auth.php";
requireAdmin();

require_once "../includes/config.php";

$sql = "SELECT * FROM members ORDER BY id DESC";
$result = $conn->query($sql);

$total = $result->num_rows;

include '../includes/header.php';
?>

<section class="member-section">

<div class="container">

<div class="d-flex justify-content-between align-items-center mb-4">

    <h2>👥 จัดการสมาชิก</h2>

    <span class="badge bg-primary fs-6">
        สมาชิกทั้งหมด <?= $total ?> คน
    </span>

</div>

<div class="card shadow mb-4">

    <div class="card-body">

        <input
            type="text"
            id="searchInput"
            class="form-control"
            placeholder="🔍 ค้นหาชื่อ รหัสนักศึกษา หรือสาขาวิชา">

    </div>

</div>

<div class="card shadow">

<div class="table-responsive">

<table class="table table-hover align-middle">

<thead class="table-dark">

<tr>

<th class="text-center">รูป</th>
<th>ID</th>
<th>ชื่อ-นามสกุล</th>
<th>รหัสนักศึกษา</th>
<th>สาขา</th>
<th>ปีจบ</th>
<th>สิทธิ์</th>
<th width="250">จัดการ</th>

</tr>

</thead>

<tbody id="memberTable">

<?php while($row = $result->fetch_assoc()): ?>

<tr>

<td class="text-center">

<?php
$photoPath = "../uploads/profiles/" . $row['photo'];

if (!empty($row['photo']) && file_exists($photoPath)):
?>

<img
src="<?= $photoPath; ?>"
style="
width:60px;
height:60px;
border-radius:50%;
object-fit:cover;
border:2px solid #ddd;">

<?php else: ?>

<img
src="https://via.placeholder.com/60x60?text=👤"
style="
width:60px;
height:60px;
border-radius:50%;">

<?php endif; ?>

</td>

<td><?= $row['id']; ?></td>

<td>

<?= htmlspecialchars($row['prefix']); ?>
<?= htmlspecialchars($row['first_name']); ?>
<?= htmlspecialchars($row['last_name']); ?>

</td>

<td><?= htmlspecialchars($row['student_id']); ?></td>

<td><?= htmlspecialchars($row['major']); ?></td>

<td><?= htmlspecialchars($row['graduation_year']); ?></td>

<td>

<?php if($row['role'] == "admin"): ?>

<span class="badge bg-danger">
Admin
</span>

<?php else: ?>

<span class="badge bg-secondary">
Member
</span>

<?php endif; ?>

</td>

<td>

<a
href="../member_detail.php?id=<?= $row['id']; ?>"
class="btn btn-success btn-sm">

<i class="fa fa-eye"></i>
ดู

</a>

<a
href="edit_member.php?id=<?= $row['id']; ?>"
class="btn btn-warning btn-sm">

<i class="fa fa-pen"></i>
แก้ไข

</a>
<a
href="reset_password.php?id=<?= $row['id']; ?>"
class="btn btn-primary btn-sm">

<i class="fa fa-key"></i>
รีเซ็ตรหัสผ่าน

</a>

<form
method="post"
action="delete_member.php"
class="d-inline"
onsubmit="return confirm('ยืนยันการลบสมาชิก ?')">

<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrfToken()) ?>">
<input type="hidden" name="id" value="<?= (int)$row['id'] ?>">

<button type="submit" class="btn btn-danger btn-sm">
    <i class="fa fa-trash"></i>
    ลบ
</button>

</form>

</td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

</div>

</section>

<script>

const search = document.getElementById("searchInput");

search.addEventListener("keyup", function () {

    let keyword = this.value.toLowerCase();

    let rows = document.querySelectorAll("#memberTable tr");

    rows.forEach(function (row) {

        let text = row.innerText.toLowerCase();

        row.style.display = text.includes(keyword) ? "" : "none";

    });

});

</script>

<?php include '../includes/footer.php'; ?>
