179 lines
10 KiB
PHP
179 lines
10 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
|
|
// Check if user is superadmin
|
|
if ($_SESSION['role'] !== 'superadmin') {
|
|
echo "<script>
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'ปฏิเสธการเข้าถึง',
|
|
text: 'เฉพาะ Super Admin เท่านั้นที่สามารถเข้าใช้งานหน้านี้ได้',
|
|
confirmButtonText: 'กลับหน้าหลัก'
|
|
}).then(() => {
|
|
window.location.href = 'dashboard.php';
|
|
});
|
|
});
|
|
</script>";
|
|
require_once 'includes/footer.php';
|
|
exit;
|
|
}
|
|
|
|
$msg = $_GET['msg'] ?? '';
|
|
$error = $_GET['error'] ?? '';
|
|
?>
|
|
|
|
<div class="page-content">
|
|
<div class="page-header">
|
|
<div>
|
|
<h2 class="page-title">จัดการผู้ดูแลระบบ (Users)</h2>
|
|
<p class="page-subtitle">เพิ่มหรือลบสิทธิ์ผู้ดูแลระบบ (Admin) จากรหัสบัตรประชาชน 13 หลัก</p>
|
|
</div>
|
|
<div>
|
|
<button class="btn btn-primary rounded-pill px-4 shadow-sm" data-bs-toggle="modal" data-bs-target="#addAdminModal">
|
|
<i class="bi bi-person-plus-fill me-1"></i> เพิ่ม Admin ใหม่
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($msg == 'success'): ?>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
window.showToast('ดำเนินการสำเร็จ', 'success');
|
|
});
|
|
</script>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
window.showToast('<?= htmlspecialchars($error) ?>', 'danger');
|
|
});
|
|
</script>
|
|
<?php endif; ?>
|
|
|
|
<div class="card-premium">
|
|
<div class="card-body p-4">
|
|
<div class="table-responsive">
|
|
<table id="usersTable" class="table table-hover align-middle mb-0 w-100">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th class="ps-3">ID</th>
|
|
<th>เลขบัตรประชาชน (CID)</th>
|
|
<th class="text-center">ชื่อ - นามสกุล</th>
|
|
<th>สิทธิ์การใช้งาน (Role)</th>
|
|
<th>วันที่เพิ่ม</th>
|
|
<th class="text-end pe-3">จัดการ</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
// ดึงข้อมูล Master Super Admin จาก config ก่อน
|
|
global $master_super_admin_cid;
|
|
$master_name = 'ไม่ทราบชื่อ (โปรดอัปเดตข้อมูลใน HOSxP)';
|
|
|
|
try {
|
|
$stmtMaster = $pdo_hos->prepare("SELECT e.HR_PREFIX_NAME, a.HR_FNAME, a.HR_LNAME
|
|
FROM hr_person a
|
|
LEFT JOIN hr_prefix e ON a.HR_PREFIX_ID = e.HR_PREFIX_ID
|
|
WHERE a.HR_CID = :cid");
|
|
$stmtMaster->execute(['cid' => $master_super_admin_cid]);
|
|
$masterRow = $stmtMaster->fetch();
|
|
if ($masterRow) {
|
|
$master_name = $masterRow['HR_PREFIX_NAME'] . $masterRow['HR_FNAME'] . " " . $masterRow['HR_LNAME'];
|
|
}
|
|
|
|
echo "<tr>";
|
|
echo "<td class='text-center text-muted fw-medium'>0</td>";
|
|
echo "<td class='text-center fw-medium text-dark'>" . htmlspecialchars($master_super_admin_cid) . "</td>";
|
|
echo "<td class='text-center'>" . htmlspecialchars($master_name) . " <i class='bi bi-patch-check-fill text-danger ms-1' title='Master Account'></i></td>";
|
|
echo "<td class='text-center'><span class='badge-soft bg-danger-subtle text-danger'>Master Super Admin</span></td>";
|
|
echo "<td class='text-center'>-</td>";
|
|
echo "<td class='text-center text-muted small'>ไม่สามารถลบได้</td>";
|
|
echo "</tr>";
|
|
|
|
$stmt = $pdo_sys->query("SELECT * FROM system_users ORDER BY id DESC");
|
|
while ($row = $stmt->fetch()) {
|
|
// ดึงชื่อจาก HOSxP
|
|
$name = 'ไม่พบชื่อใน HOSxP';
|
|
$stmtName = $pdo_hos->prepare("SELECT e.HR_PREFIX_NAME, a.HR_FNAME, a.HR_LNAME
|
|
FROM hr_person a
|
|
LEFT JOIN hr_prefix e ON a.HR_PREFIX_ID = e.HR_PREFIX_ID
|
|
WHERE a.HR_CID = :cid");
|
|
$stmtName->execute(['cid' => $row['cid']]);
|
|
$nameRow = $stmtName->fetch();
|
|
if ($nameRow) {
|
|
$name = $nameRow['HR_PREFIX_NAME'] . $nameRow['HR_FNAME'] . " " . $nameRow['HR_LNAME'];
|
|
}
|
|
|
|
$roleBadge = $row['role'] === 'superadmin' ? 'badge-soft-primary' : 'badge-soft-info';
|
|
|
|
echo "<tr>";
|
|
echo "<td class='text-center text-muted fw-medium'>" . $row['id'] . "</td>";
|
|
echo "<td class='text-center fw-medium text-dark'>" . htmlspecialchars($row['cid']) . "</td>";
|
|
echo "<td class='text-center'>" . htmlspecialchars($name) . "</td>";
|
|
echo "<td class='text-center'><span class='badge-soft {$roleBadge} text-uppercase'>" . htmlspecialchars($row['role']) . "</span></td>";
|
|
echo "<td class='text-center' data-order='" . $row['created_at'] . "'>" . format_thai_date($row['created_at'], true) . "</td>";
|
|
echo "<td class='text-center'>";
|
|
echo "<a href='users_action.php?action=delete&id=" . $row['id'] . "' class='btn btn-sm btn-light text-danger btn-icon-only mx-auto' style='width: 32px; height: 32px;' onclick='return confirm(\"ยืนยันการลบสิทธิ์ Admin บัญชีนี้?\")'><i class='bi bi-trash fs-6'></i></a>";
|
|
echo "</td>";
|
|
echo "</tr>";
|
|
}
|
|
} catch (PDOException $e) {
|
|
echo "<tr><td colspan='6' class='text-danger'>Error: " . $e->getMessage() . "</td></tr>";
|
|
}
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Add -->
|
|
<div class="modal fade" id="addAdminModal" tabindex="-1" aria-labelledby="addAdminModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content" style="border-radius: 16px; border: none; box-shadow: var(--shadow-lg);">
|
|
<div class="modal-header border-0 pb-0 pt-4 px-4">
|
|
<h5 class="modal-title fw-bold" id="addAdminModalLabel">เพิ่มผู้ดูแลระบบใหม่</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<form action="users_action.php" method="POST">
|
|
<input type="hidden" name="action" value="create">
|
|
<div class="modal-body p-4">
|
|
<div class="mb-3">
|
|
<label class="form-label fw-medium text-muted small text-uppercase">เลขบัตรประชาชน 13 หลัก</label>
|
|
<input type="text" name="cid" class="form-control form-control-lg bg-light border-0" required pattern="\d{13}" title="กรุณากรอกตัวเลข 13 หลัก" placeholder="เช่น 1234567890123">
|
|
<div class="form-text mt-2 text-muted"><i class="bi bi-info-circle me-1"></i> บุคคลนี้จะต้องมีรายชื่อในระบบบุคลากร (hr_person) ก่อน</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label fw-medium text-muted small text-uppercase">สิทธิ์การใช้งาน (Role)</label>
|
|
<select name="role" class="form-select form-control-lg bg-light border-0" required>
|
|
<option value="admin" selected>Admin (จัดการข้อมูลทั่วไป)</option>
|
|
<option value="superadmin">Super Admin (จัดการแอดมินได้)</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer border-0 pt-0 pb-4 px-4">
|
|
<button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">ยกเลิก</button>
|
|
<button type="submit" class="btn btn-primary rounded-pill px-4">เพิ่มสิทธิ์</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|
|
|
|
<script>
|
|
$(document).ready(function() {
|
|
$('#usersTable').DataTable({
|
|
"language": {
|
|
"url": "//cdn.datatables.net/plug-ins/1.13.6/i18n/th.json"
|
|
},
|
|
"order": [[0, "asc"]],
|
|
"pageLength": 10,
|
|
"dom": '<"row mb-3"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6"f>>t<"row mt-3"<"col-sm-12 col-md-5"i><"col-sm-12 col-md-7"p>>',
|
|
});
|
|
});
|
|
</script>
|