169 lines
11 KiB
PHP
169 lines
11 KiB
PHP
<?php
|
|
require_once 'includes/db.php';
|
|
require_once 'includes/auth.php';
|
|
requireAdmin(); // Admin only
|
|
|
|
// Handle actions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['action'])) {
|
|
$action = $_POST['action'];
|
|
|
|
if ($action === 'create') {
|
|
$username = trim($_POST['username']);
|
|
$password = $_POST['password'];
|
|
$name = trim($_POST['name']);
|
|
$role = $_POST['role'];
|
|
$status = $_POST['status'];
|
|
|
|
// Check if username exists
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
if ($stmt->fetch()) {
|
|
$_SESSION['flash'] = ['type' => 'error', 'title' => 'ผิดพลาด', 'message' => 'ชื่อผู้ใช้นี้มีอยู่ในระบบแล้ว'];
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, password, name, role, status) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$username, $hashed_password, $name, $role, $status]);
|
|
$_SESSION['flash'] = ['type' => 'success', 'title' => 'สำเร็จ', 'message' => 'เพิ่มผู้ใช้งานใหม่เรียบร้อยแล้ว'];
|
|
}
|
|
} elseif ($action === 'update_status') {
|
|
$id = $_POST['id'];
|
|
$status = $_POST['status'];
|
|
if ($id != $_SESSION['user_id']) { // prevent deactivating oneself
|
|
$stmt = $pdo->prepare("UPDATE users SET status = ? WHERE id = ?");
|
|
$stmt->execute([$status, $id]);
|
|
$_SESSION['flash'] = ['type' => 'success', 'title' => 'สำเร็จ', 'message' => 'อัปเดตสถานะเรียบร้อยแล้ว'];
|
|
} else {
|
|
$_SESSION['flash'] = ['type' => 'error', 'title' => 'ผิดพลาด', 'message' => 'ไม่สามารถระงับบัญชีของตัวเองได้'];
|
|
}
|
|
}
|
|
|
|
header("Location: users.php");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Fetch all users
|
|
$stmt = $pdo->query("SELECT id, username, name, role, status, created_at FROM users ORDER BY created_at DESC");
|
|
$users = $stmt->fetchAll();
|
|
|
|
require_once 'includes/header.php';
|
|
?>
|
|
|
|
<div class="space-y-6">
|
|
<div class="flex flex-col sm:flex-row sm:items-center justify-between border-b border-slate-100 pb-3 gap-4">
|
|
<h2 class="text-xl font-black text-slate-800 flex items-center gap-2">
|
|
<i data-lucide="users" class="text-blue-500 w-5 h-5"></i> จัดการผู้ใช้งานระบบ
|
|
</h2>
|
|
<button onclick="document.getElementById('createUserModal').classList.remove('hidden')" class="bg-blue-600 hover:bg-blue-700 text-white text-xs font-bold py-2 px-4 rounded-xl shadow-sm transition-all flex items-center gap-2">
|
|
<i data-lucide="plus" class="w-4 h-4"></i> เพิ่มผู้ใช้งาน
|
|
</button>
|
|
</div>
|
|
|
|
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">
|
|
<div class="overflow-x-auto">
|
|
<table class="w-full text-left border-collapse text-xs md:text-sm">
|
|
<thead>
|
|
<tr class="bg-slate-50 text-slate-500 font-bold border-b border-slate-200">
|
|
<th class="py-3 px-4 uppercase tracking-wider">ชื่อบัญชี</th>
|
|
<th class="py-3 px-4 uppercase tracking-wider">ชื่อ-นามสกุล</th>
|
|
<th class="py-3 px-4 uppercase tracking-wider">บทบาท</th>
|
|
<th class="py-3 px-4 uppercase tracking-wider">สถานะ</th>
|
|
<th class="py-3 px-4 uppercase tracking-wider text-right">จัดการ</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-slate-100">
|
|
<?php foreach ($users as $user): ?>
|
|
<tr class="hover:bg-slate-50 transition-colors">
|
|
<td class="py-3 px-4 font-bold text-slate-700"><?php echo htmlspecialchars($user['username']); ?></td>
|
|
<td class="py-3 px-4 text-slate-600"><?php echo htmlspecialchars($user['name']); ?></td>
|
|
<td class="py-3 px-4">
|
|
<?php if($user['role'] === 'admin'): ?>
|
|
<span class="bg-blue-100 text-blue-700 px-2 py-1 rounded-md text-[10px] font-bold">ผู้ดูแลระบบ</span>
|
|
<?php else: ?>
|
|
<span class="bg-slate-100 text-slate-600 px-2 py-1 rounded-md text-[10px] font-bold">ผู้ใช้งาน</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="py-3 px-4">
|
|
<?php if($user['status'] === 'active'): ?>
|
|
<span class="text-emerald-500 font-bold flex items-center gap-1.5"><i data-lucide="circle" class="w-2 h-2 fill-emerald-500"></i> ใช้งานได้</span>
|
|
<?php else: ?>
|
|
<span class="text-rose-500 font-bold flex items-center gap-1.5"><i data-lucide="circle" class="w-2 h-2 fill-rose-500"></i> ระงับการใช้งาน</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="py-3 px-4 text-right space-x-2">
|
|
<?php if ($user['id'] != $_SESSION['user_id']): ?>
|
|
<form method="POST" class="inline-block" onsubmit="event.preventDefault(); Swal.fire({title: 'ยืนยัน', text: 'ยืนยันการเปลี่ยนสถานะผู้ใช้นี้?', icon: 'warning', showCancelButton: true, confirmButtonColor: '#4f46e5', cancelButtonColor: '#ef4444', confirmButtonText: 'ใช่, เปลี่ยนสถานะ', cancelButtonText: 'ยกเลิก'}).then((result) => { if(result.isConfirmed) { this.submit(); } });">
|
|
<input type="hidden" name="action" value="update_status">
|
|
<input type="hidden" name="id" value="<?php echo $user['id']; ?>">
|
|
<input type="hidden" name="status" value="<?php echo $user['status'] === 'active' ? 'inactive' : 'active'; ?>">
|
|
<button type="submit" class="text-xs font-bold <?php echo $user['status'] === 'active' ? 'text-rose-500 hover:text-rose-700' : 'text-emerald-500 hover:text-emerald-700'; ?>">
|
|
<?php echo $user['status'] === 'active' ? 'ระงับ' : 'เปิดใช้งาน'; ?>
|
|
</button>
|
|
</form>
|
|
<?php else: ?>
|
|
<span class="text-slate-300 text-xs">-</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal: Create User -->
|
|
<div id="createUserModal" class="fixed inset-0 bg-slate-900/50 backdrop-blur-sm z-50 hidden flex items-center justify-center p-4">
|
|
<div class="bg-white rounded-2xl shadow-xl border border-slate-200 w-full max-w-md overflow-hidden animate-fadeIn">
|
|
<div class="p-5 border-b border-slate-100 flex justify-between items-center bg-slate-50">
|
|
<h3 class="font-black text-slate-800 text-lg">เพิ่มและจัดสร้างผู้ใช้ใหม่</h3>
|
|
<button type="button" onclick="document.getElementById('createUserModal').classList.add('hidden')" class="text-slate-400 hover:text-rose-500 transition-colors">
|
|
<i data-lucide="x" class="w-5 h-5"></i>
|
|
</button>
|
|
</div>
|
|
<form method="POST" action="users.php" class="p-5 space-y-4">
|
|
<input type="hidden" name="action" value="create">
|
|
|
|
<div>
|
|
<label class="block text-xs font-bold text-slate-500 mb-1">ชื่อ-นามสกุล / ชื่อเรียก</label>
|
|
<input type="text" name="name" required class="w-full py-2 px-3 border border-slate-200 rounded-lg text-sm outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500">
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-xs font-bold text-slate-500 mb-1">ชื่อบัญชีผู้ใช้งาน (Username)</label>
|
|
<input type="text" name="username" required class="w-full py-2 px-3 border border-slate-200 rounded-lg text-sm outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500">
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-xs font-bold text-slate-500 mb-1">รหัสผ่าน</label>
|
|
<input type="password" name="password" required class="w-full py-2 px-3 border border-slate-200 rounded-lg text-sm outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500">
|
|
</div>
|
|
|
|
<div class="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label class="block text-xs font-bold text-slate-500 mb-1">ระดับสิทธิ์</label>
|
|
<select name="role" class="w-full py-2 px-3 border border-slate-200 rounded-lg text-sm outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500">
|
|
<option value="user">ผู้ใช้งาน</option>
|
|
<option value="admin">ผู้ดูแลระบบ</option>
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<label class="block text-xs font-bold text-slate-500 mb-1">สถานะ</label>
|
|
<select name="status" class="w-full py-2 px-3 border border-slate-200 rounded-lg text-sm outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500">
|
|
<option value="active">ใช้งานได้</option>
|
|
<option value="inactive">ระงับการใช้งาน</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="pt-4 flex justify-end gap-3 border-t border-slate-100">
|
|
<button type="button" onclick="document.getElementById('createUserModal').classList.add('hidden')" class="px-4 py-2 text-sm font-bold text-slate-600 bg-slate-100 hover:bg-slate-200 rounded-lg transition-colors">ยกเลิก</button>
|
|
<button type="submit" class="px-4 py-2 text-sm font-bold text-white bg-blue-600 hover:bg-blue-700 rounded-lg shadow-sm transition-colors">บันทึกข้อมูล</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|