<?php
session_start();

// การเชื่อมต่อฐานข้อมูล (ปรับแต่งตามของคุณ)
include 'config.php'; // หรือ include การเชื่อมต่อฐานข้อมูลของคุณ

// SQL สำหรับดึงข้อมูลสมาชิก
$sql = "SELECT uid, username, fullname, email, phone FROM users ORDER BY uid DESC";
$result = $conn->query($sql);

// ตรวจสอบ error (เฉพาะขณะแก้ปัญหา)
if (!$result) {
    echo "Database Error: " . $conn->error;
}
?>

<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>รายชื่อสมาชิก</title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Kanit', sans-serif;
        }
        
        .card {
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
            border: 1px solid rgba(0, 0, 0, 0.125);
        }
        
        .card-header {
            border-bottom: 1px solid rgba(0, 0, 0, 0.125);
            background-color: #fff !important;
        }
        
        .btn-add {
            background: linear-gradient(45deg, #28a745, #20c997);
            color: white;
            border: none;
            padding: 0.5rem 1rem;
            border-radius: 0.375rem;
            text-decoration: none;
            margin-bottom: 1rem;
            display: inline-block;
        }
        
        .btn-add:hover {
            background: linear-gradient(45deg, #218838, #1ba188);
            color: white;
            transform: translateY(-2px);
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: linear-gradient(45deg, #007bff, #6f42c1);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 1.2rem;
        }
        
        .btn-action {
            padding: 0.25rem 0.75rem;
            margin: 0.125rem;
            border-radius: 0.25rem;
            text-decoration: none;
            font-size: 0.875rem;
            border: none;
            cursor: pointer;
        }
        
        .btn-edit {
            background: linear-gradient(45deg, #ffc107, #fd7e14);
            color: #212529;
        }
        
        .btn-edit:hover {
            background: linear-gradient(45deg, #e0a800, #e8681c);
            color: #212529;
            transform: translateY(-1px);
        }
        
        .btn-delete {
            background: linear-gradient(45deg, #dc3545, #e83e8c);
            color: white;
        }
        
        .btn-delete:hover {
            background: linear-gradient(45deg, #c82333, #d91a72);
            color: white;
            transform: translateY(-1px);
        }
        
        .no-data {
            text-align: center;
            padding: 3rem 1rem;
            color: #6c757d;
        }
        
        .no-data i {
            font-size: 4rem;
            margin-bottom: 1rem;
        }
        
        .table > tbody > tr > td {
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="container mt-4">
        
        <!-- ส่วนแสดงตารางสมาชิก -->
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="fas fa-users me-2"></i>แสดงรายชื่อสมาชิก</h5>
            </div>
            <div class="card-body">
                
                <?php if (isset($_SESSION["login"]) && $_SESSION["login"] == "pass" && isset($_SESSION["uid_iogin"])): ?>
                <a href="?page=regis" class="btn btn-add">
                    <i class="fas fa-plus-circle me-2"></i>เพิ่มสมาชิก
                </a>
                <?php endif; ?>
                
                <?php if ($result && $result->num_rows > 0): ?>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead class="table-light">
                            <tr>
                                <th width="80">ลำดับ</th>
                                <th>ชื่อ - นามสกุล</th>
                                <th width="150">เบอร์โทร</th>
                                <th width="200">อีเมล์</th>
                                <?php if (isset($_SESSION["login"]) && $_SESSION["login"] == "pass" && isset($_SESSION["uid_iogin"])): ?>
                                <th width="180">การจัดการ</th>
                                <?php endif; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $no = 1;
                            while($row = $result->fetch_assoc()):
                            ?>
                            <tr>
                                <td class="fw-bold"><?= $no ?></td>
                                <td>
                                    <div class="user-info">
                                        <div class="user-avatar">
                                            <?= mb_substr($row['fullname'] ?: 'N', 0, 1, 'UTF-8') ?>
                                        </div>
                                        <div>
                                            <div class="fw-medium"><?= htmlspecialchars($row['fullname'] ?: 'ไม่มีชื่อ') ?></div>
                                            <small class="text-muted">@<?= htmlspecialchars($row['username'] ?: 'unknown') ?></small>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <?php if (!empty($row['phone'])): ?>
                                        <?= htmlspecialchars($row['phone']) ?>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                                <td><?= htmlspecialchars($row['email'] ?: '') ?></td>
                                <?php if (isset($_SESSION["login"]) && $_SESSION["login"] == "pass" && isset($_SESSION["uid_iogin"])): ?>
                                <td>
                                    <div class="d-flex flex-wrap gap-1">
                                        <a href="?page=edit_user&uid=<?= $row['uid'] ?>" class="btn btn-action btn-edit">
                                            <i class="fas fa-edit me-1"></i>แก้ไข
                                        </a>
                                        <a href="?page=delete_user&uid=<?= $row['uid'] ?>" 
                                           class="btn btn-action btn-delete" 
                                           onclick="return confirm('คุณแน่ใจว่าต้องการลบสมาชิก <?= htmlspecialchars($row['fullname']) ?> ?')">
                                            <i class="fas fa-trash-alt me-1"></i>ลบ
                                        </a>
                                    </div>
                                </td>
                                <?php endif; ?>
                            </tr>
                            <?php
                            $no++;
                            endwhile;
                            ?>
                        </tbody>
                    </table>
                </div>
                
                <?php else: ?>
                <div class="no-data">
                    <i class="fas fa-users-slash text-muted"></i>
                    <h5 class="text-muted">ไม่มีข้อมูลสมาชิก</h5>
                    <p class="mb-0 text-muted">ยังไม่มีสมาชิกในระบบ</p>
                </div>
                <?php endif; ?>
                
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

<?php
// ปิดการเชื่อมต่อฐานข้อมูล
if (isset($conn)) {
    $conn->close();
}
?>