Files
2026-09-16 23:20:08 +07:00

213 lines
13 KiB
PHP

<?php
session_start();
require_once '../includes/db.php';
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_role'] !== 'superadmin') {
header("Location: index.php");
exit;
}
$msg = '';
$error = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_admin'])) {
$cid = trim($_POST['cid']);
$password = $_POST['password'];
// Check if user already exists
$stmt_check = $pdo->prepare("SELECT id FROM admin_users WHERE username = ?");
$stmt_check->execute([$cid]);
if ($stmt_check->fetch()) {
$error = "เจ้าของหมายเลข CID นี้เป็น Admin อยู่แล้ว";
} else {
$hash = password_hash($password, PASSWORD_DEFAULT);
$insert = $pdo->prepare("INSERT INTO admin_users (username, cid, password_hash, role) VALUES (?, ?, ?, 'admin')");
if ($insert->execute([$cid, $cid, $hash])) {
$msg = "เพิ่มสิทธิ์ผู้ดูแลระบบสำเร็จ";
} else {
$error = "เกิดข้อผิดพลาดในการบันทึกข้อมูล";
}
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['delete_admin'])) {
$admin_id = $_POST['admin_id'];
$del = $pdo->prepare("DELETE FROM admin_users WHERE id = ? AND role != 'superadmin'");
if ($del->execute([$admin_id])) {
$msg = "ลบสิทธิ์ผู้ดูแลระบบสำเร็จ";
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['reset_2fa'])) {
$admin_id = $_POST['admin_id'];
$reset = $pdo->prepare("UPDATE admin_users SET google2fa_secret = NULL WHERE id = ?");
if ($reset->execute([$admin_id])) {
$msg = "รีเซ็ต 2FA สำเร็จ (เจ้าหน้าที่จะต้องตั้งค่า 2FA ใหม่เมื่อเข้าสู่ระบบครั้งถัดไป)";
}
}
$stmt = $pdo->query("SELECT * FROM admin_users ORDER BY role DESC, created_at ASC");
$admins = $stmt->fetchAll();
// Add $current_admin logic
$stmt_admin = $pdo->prepare("SELECT username, role FROM admin_users WHERE id = ?");
$stmt_admin->execute([$_SESSION['admin_id'] ?? 0]);
$current_admin = $stmt_admin->fetch();
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Admins - KSH-STAFF</title>
<link rel="icon" type="image/png" href="../assets/img/logo.png">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
sans: ['Sarabun', 'sans-serif'],
},
animation: {
shine: 'shine 3s ease-in-out infinite'
},
keyframes: {
shine: {
'0%': { left: '-150%' },
'20%': { left: '150%' },
'100%': { left: '150%' }
}
}
}
}
}
</script>
</head>
<body class="bg-gray-100 font-sans antialiased text-gray-800 flex min-h-screen">
<!-- Sidebar -->
<aside class="w-64 bg-gray-800 text-gray-300 flex flex-col fixed top-0 left-0 h-screen shadow-lg z-20">
<div class="px-6 py-5 bg-gray-900 border-b border-gray-700 text-center">
<div class="relative inline-block overflow-hidden rounded-full w-20 h-20 mx-auto mb-3 shadow-md bg-white p-1">
<img src="../assets/img/logo.png" alt="Logo" class="w-full h-full object-cover rounded-full relative z-10">
<div class="absolute inset-0 z-20 w-1/2 h-full bg-gradient-to-r from-transparent via-white/60 to-transparent -skew-x-12 animate-shine mix-blend-overlay"></div>
</div>
<h3 class="text-white text-lg font-bold tracking-wide mt-1">KSH-STAFF Admin</h3>
</div>
<nav class="flex-1 pt-6 pb-4 overflow-y-auto flex flex-col gap-1">
<a href="index.php" class="block px-6 py-3 hover:bg-gray-700 hover:text-white transition duration-200">
<i class="fas fa-home w-6 text-center mr-2"></i> แดชบอร์ด
</a>
<a href="staff_list.php" class="block px-6 py-3 hover:bg-gray-700 hover:text-white transition duration-200">
<i class="fas fa-users w-6 text-center mr-2"></i> รายชื่อเจ้าหน้าที่
</a>
<?php if ($_SESSION['admin_role'] === 'superadmin'): ?>
<a href="manage_admins.php" class="block px-6 py-3 bg-blue-600 text-white font-medium transition duration-200 shadow-sm">
<i class="fas fa-crown w-6 text-center mr-2"></i> จัดการแอดมิน
</a>
<?php endif; ?>
<div class="mt-auto px-6 pt-4">
<a href="logout.php" class="flex items-center text-red-400 hover:text-red-300 hover:bg-red-400/10 py-2 px-3 rounded transition duration-200">
<i class="fas fa-sign-out-alt w-6 text-center mr-1"></i> ออกจากระบบ
</a>
</div>
</nav>
</aside>
<!-- Main Content -->
<main class="flex-1 ml-64 flex flex-col min-h-screen">
<header class="bg-white px-8 py-5 border-b border-gray-200 flex justify-between items-center shadow-sm z-10 sticky top-0">
<h4 class="text-xl font-bold text-gray-700"><i class="fas fa-crown text-yellow-500 mr-2"></i> จัดการผู้ดูแลระบบ (Super Admin)</h4>
<div class="text-sm text-gray-500 font-medium px-4 py-1.5 bg-gray-100 rounded-full border border-gray-200 flex items-center gap-2">
ผู้ใช้งาน: <span class="text-blue-600 font-bold"><?= htmlspecialchars($current_admin['username'] ?? $_SESSION['admin_username'] ?? 'Admin') ?></span>
<span class="bg-gray-200 text-gray-600 px-2 py-0.5 rounded text-xs"><?= htmlspecialchars($current_admin['role'] ?? '') ?></span>
</div>
</header>
<div class="p-8 flex-1">
<?php if (!empty($msg)): ?>
<script>Swal.fire({icon: 'success', title: 'สำเร็จ', text: '<?= $msg ?>', timer: 2000, confirmButtonColor: '#2563eb'});</script>
<?php endif; ?>
<?php if (!empty($error)): ?>
<script>Swal.fire({icon: 'error', title: 'ผิดพลาด', text: '<?= $error ?>', confirmButtonColor: '#2563eb'});</script>
<?php endif; ?>
<div class="bg-white rounded-xl p-8 shadow-sm border border-gray-200 mb-8">
<h3 class="text-lg font-bold text-gray-800 border-b border-gray-100 pb-4 mb-6"><i class="fas fa-user-plus text-blue-500 mr-2"></i> เพิ่มผู้ดูแลระบบ (Admin)</h3>
<form method="post" class="flex flex-col md:flex-row gap-6 items-end">
<div class="flex-1 w-full">
<label class="block text-gray-700 text-sm font-medium mb-2">เลขบัตรประชาชน (CID)</label>
<input type="text" name="cid" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition" required pattern="\d{13}" placeholder="กรอกเลข 13 หลัก">
</div>
<div class="flex-1 w-full">
<label class="block text-gray-700 text-sm font-medium mb-2">ตั้งรหัสผ่านเริ่มต้น</label>
<input type="password" name="password" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition" required placeholder="รหัสผ่าน">
</div>
<div class="w-full md:w-auto">
<button type="submit" name="add_admin" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2.5 px-6 rounded-lg transition duration-200 shadow-sm flex items-center justify-center gap-2">
<i class="fas fa-plus"></i> เพิ่มสิทธิ์
</button>
</div>
</form>
</div>
<div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
<div class="p-6 border-b border-gray-200">
<h3 class="text-lg font-bold text-gray-800"><i class="fas fa-list text-gray-400 mr-2"></i> รายชื่อผู้ดูแลระบบในระบบ</h3>
</div>
<div class="overflow-x-auto">
<table class="w-full text-left border-collapse">
<thead>
<tr class="bg-gray-50 text-gray-600 text-sm uppercase tracking-wider">
<th class="px-6 py-4 font-semibold border-b border-gray-200">Username / CID</th>
<th class="px-6 py-4 font-semibold border-b border-gray-200">สิทธิ์ (Role)</th>
<th class="px-6 py-4 font-semibold border-b border-gray-200">สถานะ 2FA</th>
<th class="px-6 py-4 font-semibold border-b border-gray-200">จัดการ</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
<?php foreach ($admins as $ad): ?>
<tr class="hover:bg-gray-50 transition duration-150">
<td class="px-6 py-4 text-gray-800 font-medium"><?= htmlspecialchars($ad['username']) ?></td>
<td class="px-6 py-4">
<?php if ($ad['role'] === 'superadmin'): ?>
<span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-semibold bg-red-100 text-red-700"><i class="fas fa-crown"></i> Super Admin</span>
<?php else: ?>
<span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-semibold bg-blue-100 text-blue-700"><i class="fas fa-user-shield"></i> Admin</span>
<?php endif; ?>
</td>
<td class="px-6 py-4 text-sm">
<?= $ad['google2fa_secret'] ? '<span class="text-emerald-600 font-medium flex items-center gap-1.5"><i class="fas fa-check-circle"></i> ตั้งค่าแล้ว</span>' : '<span class="text-red-500 font-medium flex items-center gap-1.5"><i class="fas fa-times-circle"></i> ยังไม่ตั้งค่า</span>' ?>
</td>
<td class="px-6 py-4">
<?php if ($ad['role'] !== 'superadmin'): ?>
<div class="flex gap-2">
<form method="post" onsubmit="return confirm('ยืนยันการรีเซ็ต 2FA?');">
<input type="hidden" name="admin_id" value="<?= $ad['id'] ?>">
<button type="submit" name="reset_2fa" class="bg-amber-500 hover:bg-amber-600 text-white px-3 py-1.5 rounded text-sm font-medium transition flex items-center gap-1.5"><i class="fas fa-sync-alt"></i> รีเซ็ต 2FA</button>
</form>
<form method="post" onsubmit="return confirm('ยืนยันการลบสิทธิ์ Admin?');">
<input type="hidden" name="admin_id" value="<?= $ad['id'] ?>">
<button type="submit" name="delete_admin" class="bg-red-500 hover:bg-red-600 text-white px-3 py-1.5 rounded text-sm font-medium transition flex items-center gap-1.5"><i class="fas fa-trash-alt"></i> ลบสิทธิ์</button>
</form>
</div>
<?php else: ?>
<span class="text-gray-400 font-medium">-</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</main>
</body>
</html>