<?php 
include 'header.php'; 
include 'connect.php'; 

// เช็คสิทธิ์ Admin
if(!isset($_SESSION['username']) || $_SESSION['username'] !== 'admin'){
    echo "<script>alert('เฉพาะ Admin เท่านั้น'); window.location='index.php';</script>";
    exit();
}

$sql = "SELECT id, username FROM users";
$result = $conn->query($sql);
?>

<div class="container">
    <h2 style="color: #4a148c; border-left: 5px solid #ffd54f; padding-left: 15px; margin: 30px 0;">รายชื่อสมาชิกในระบบ</h2>

    <table style="width: 100%; border-collapse: collapse; background: white; border-radius: 10px; overflow: hidden; box-shadow: 0 5px 15px rgba(0,0,0,0.05);">
        <thead>
            <tr style="background: #8e24aa; color: white; text-align: left;">
                <th style="padding: 15px;">ID</th>
                <th style="padding: 15px;">ชื่อผู้ใช้งาน (Username)</th>
                <th style="padding: 15px; text-align: center;">จัดการข้อมูล</th>
            </tr>
        </thead>
        <tbody>
            <?php if ($result->num_rows > 0): ?>
                <?php while($row = $result->fetch_assoc()): ?>
                    <tr style="border-bottom: 1px solid #eee;">
                        <td style="padding: 15px;"><?php echo $row['id']; ?></td>
                        <td style="padding: 15px; font-weight: bold; color: #333;"><?php echo $row['username']; ?></td>
                        <td style="padding: 15px; text-align: center;">
                            <a href="edit_user.php?id=<?php echo $row['id']; ?>" 
                               style="text-decoration: none; background: #f3e5f5; color: #8e24aa; padding: 5px 15px; border-radius: 20px; font-size: 0.9rem; margin-right: 5px;">
                               แก้ไข
                            </a>
                            <a href="delete_user.php?id=<?php echo $row['id']; ?>" 
                               onclick="return confirm('ยืนยันการลบสมาชิกรายนี้หรือไม่?')"
                               style="text-decoration: none; background: #ffebee; color: #d32f2f; padding: 5px 15px; border-radius: 20px; font-size: 0.9rem;">
                               ลบ
                            </a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="3" style="padding: 20px; text-align: center;">ไม่พบข้อมูลสมาชิก</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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