<?php include 'header.php'; ?>
<?php
// ตรวจสอบสิทธิ์ Admin
if ($_SESSION['type'] != 'admin') {
header("Location: user_dashboard.php");
exit();
}
// 1. ดึงข้อมูลกลุ่มงานทั้งหมดเพื่อใส่ใน Dropdown
$res_g = mysqli_query($conn, "SELECT * FROM `groups` ORDER BY g_id ASC");
while ($g = mysqli_fetch_assoc($res_g)) {
$group_list[] = $g;
}
// 2. ดึงข้อมูลประเภทบุคลากรทั้งหมดเพื่อใส่ใน Dropdown
$type_list = [];
$res_t = mysqli_query($conn, "SELECT * FROM personnel_types ORDER BY p_id ASC");
while ($t = mysqli_fetch_assoc($res_t)) {
$type_list[] = $t;
}
// 3. Query ข้อมูลบุคลากร พร้อม JOIN (ใช้ Alias เพื่อความชัดเจนใน PHP 7.3)
$sql_users = "SELECT u.*, g.g_name, pt.p_name
FROM users u
LEFT JOIN `groups` g ON u.g_id = g.g_id
LEFT JOIN personnel_types pt ON u.p_id = pt.p_id
ORDER BY u.u_id DESC";
$res_users = mysqli_query($conn, $sql_users);
?>
<div class="card border-0 shadow-sm">
<div class="card-header d-flex justify-content-between align-items-center bg-white py-3">
<h5 class="mb-0 fw-bold text-primary"><i class="fas fa-users-cog me-2"></i>จัดการข้อมูลบุคลากร</h5>
<button class="btn btn-primary rounded-pill px-4" data-bs-toggle="modal" data-bs-target="#userModal">
<i class="fas fa-user-plus me-1"></i> เพิ่มบุคลากร
</button>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th>Username</th>
<th>ชื่อ-นามสกุล / ตำแหน่ง</th>
<th>ประเภทบุคลากร</th>
<th>กลุ่มงาน</th>
<th class="text-center">ลายเซ็น</th>
<th class="text-center">สิทธิ์</th>
<th class="text-center">จัดการ</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($res_users)): ?>
<tr>
<td><strong><?php echo htmlspecialchars($row['username']); ?></strong></td>
<td>
<div class="fw-bold text-dark"><?php echo htmlspecialchars($row['fullname']); ?></div>
<small class="text-muted"><?php echo htmlspecialchars($row['position']); ?></small>
</td>
<td>
<span class="badge bg-light text-dark border fw-normal">
<?php echo !empty($row['p_name']) ? htmlspecialchars($row['p_name']) : 'ยังไม่ได้ระบุ'; ?>
</span>
</td>
<td>
<small class="text-muted">
<?php echo !empty($row['g_name']) ? htmlspecialchars($row['g_name']) : '-'; ?>
</small>
</td>
<td class="text-center">
<?php if ($row['signature']): ?>
<img src="uploads/<?php echo $row['signature']; ?>" style="height: 40px; border: 1px solid #eee; background: #fff;">
<?php else: ?>
<small class="text-muted">ไม่มี</small>
<?php endif; ?>
</td>
<td class="text-center">
<span class="badge <?php echo $row['type']=='admin' ? 'bg-danger':'bg-secondary'; ?> rounded-pill">
<?php echo strtoupper($row['type']); ?>
</span>
</td>
<td class="text-center">
<button class="btn btn-sm btn-warning edit-user-btn"
data-id="<?php echo $row['u_id']; ?>"
data-username="<?php echo $row['username']; ?>"
data-fullname="<?php echo $row['fullname']; ?>"
data-position="<?php echo $row['position']; ?>"
data-gid="<?php echo $row['g_id']; ?>"
data-pid="<?php echo $row['p_id']; ?>"
data-type="<?php echo $row['type']; ?>"
data-bs-toggle="modal" data-bs-target="#editUserModal">
<i class="fas fa-edit"></i>
</button>
<a href="process.php?action=del_user&id=<?php echo $row['u_id']; ?>"
class="btn btn-sm btn-danger" onclick="return confirm('ยืนยันการลบผู้ใช้?');">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
<div class="modal fade" id="userModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<form action="process.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="add_user">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title fw-bold"><i class="fas fa-user-plus me-2"></i>เพิ่มบุคลากรใหม่</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label small fw-bold">Username</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">Password (เริ่มต้น 123456)</label>
<input type="password" name="password" class="form-control" value="123456" required>
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">ชื่อ-นามสกุล</label>
<input type="text" name="fullname" class="form-control" required>
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">ตำแหน่ง</label>
<input type="text" name="position" class="form-control" required>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">ประเภทบุคลากร</label>
<select name="p_id" class="form-select" required>
<option value="">-- เลือกประเภท --</option>
<?php foreach($type_list as $t): ?>
<option value="<?php echo $t['p_id']; ?>"><?php echo htmlspecialchars($t['p_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">กลุ่มงาน</label>
<select name="g_id" class="form-select" required>
<option value="">-- เลือกกลุ่มงาน --</option>
<?php foreach($group_list as $g): ?>
<option value="<?php echo $g['g_id']; ?>"><?php echo htmlspecialchars($g['g_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">สิทธิ์การใช้งาน</label>
<select name="type" class="form-select">
<option value="user">User (บุคลากร)</option>
<option value="admin">Admin (ผู้ดูแลระบบ)</option>
</select>
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">ไฟล์ลายเซ็น (PNG/JPG)</label>
<input type="file" name="signature" class="form-control" accept="image/*">
</div>
</div>
</div>
<div class="modal-footer bg-light text-center">
<button type="submit" class="btn btn-primary px-5 rounded-pill shadow-sm">บันทึกข้อมูลบุคลากร</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal fade" id="editUserModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<form action="process.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="edit_user">
<input type="hidden" name="u_id" id="edit_u_id">
<div class="modal-header bg-warning">
<h5 class="modal-title fw-bold"><i class="fas fa-user-edit me-2"></i>แก้ไขข้อมูลบุคลากร</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label small fw-bold">Username</label>
<input type="text" id="edit_username" class="form-control bg-light" readonly>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">รีเซ็ตรหัสผ่าน (หากต้องการ)</label>
<input type="password" name="password" class="form-control" placeholder="ใส่รหัสใหม่หากต้องการเปลี่ยน">
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">ชื่อ-นามสกุล</label>
<input type="text" name="fullname" id="edit_fullname" class="form-control" required>
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">ตำแหน่ง</label>
<input type="text" name="position" id="edit_position" class="form-control" required>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">ประเภทบุคลากร</label>
<select name="p_id" id="edit_pid" class="form-select" required>
<?php foreach($type_list as $t): ?>
<option value="<?php echo $t['p_id']; ?>"><?php echo htmlspecialchars($t['p_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">กลุ่มงาน</label>
<select name="g_id" id="edit_gid" class="form-select" required>
<?php foreach($group_list as $g): ?>
<option value="<?php echo $g['g_id']; ?>"><?php echo htmlspecialchars($g['g_name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">สิทธิ์การใช้งาน</label>
<select name="type" id="edit_type" class="form-select">
<option value="user">User (บุคลากร)</option>
<option value="admin">Admin (ผู้ดูแลระบบ)</option>
</select>
</div>
<div class="col-md-12">
<label class="form-label small fw-bold">เปลี่ยนไฟล์ลายเซ็น</label>
<input type="file" name="signature" class="form-control" accept="image/*">
</div>
</div>
</div>
<div class="modal-footer bg-light text-center">
<button type="submit" class="btn btn-warning px-5 rounded-pill shadow-sm">บันทึกการแก้ไข</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$('.edit-user-btn').on('click', function(){
$('#edit_u_id').val($(this).data('id'));
$('#edit_username').val($(this).data('username'));
$('#edit_fullname').val($(this).data('fullname'));
$('#edit_position').val($(this).data('position'));
$('#edit_gid').val($(this).data('gid'));
$('#edit_pid').val($(this).data('pid'));
$('#edit_type').val($(this).data('type'));
});
});
</script>
<?php include 'footer.php'; ?>