| Server IP : 104.21.80.248 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/work/ |
Upload File : |
<?php include 'header.php'; ?>
<?php
// ตรวจสอบสิทธิ์ Admin
if($_SESSION['type'] != 'admin'){
header("Location: user_dashboard.php");
exit();
}
// 1. ดึงข้อมูลกลุ่มงานทั้งหมด
$groups = mysqli_query($conn, "SELECT * FROM groups ORDER BY g_id ASC");
$group_list = [];
while($g = mysqli_fetch_assoc($groups)) {
$group_list[] = $g;
}
// 2. ดึงข้อมูลประเภทบุคลากรทั้งหมด
$types = mysqli_query($conn, "SELECT * FROM personnel_types ORDER BY p_id ASC");
$type_list = [];
while($t = mysqli_fetch_assoc($types)) {
$type_list[] = $t;
}
// 3. ดึงข้อมูล User ทั้งหมดพร้อม JOIN ชื่อกลุ่มและชื่อประเภทบุคลากร
$sql_users = "SELECT users.*, groups.g_name, personnel_types.p_name
FROM users
LEFT JOIN groups ON users.g_id = groups.g_id
LEFT JOIN personnel_types ON users.p_id = personnel_types.p_id
WHERE users.type = 'user'
ORDER BY users.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 $row['username']; ?></strong></td>
<td>
<div class="fw-bold text-dark"><?php echo $row['fullname']; ?></div>
<small class="text-muted"><?php echo $row['position']; ?></small>
</td>
<td>
<span class="badge bg-light text-dark border fw-normal">
<?php echo $row['p_name'] ? $row['p_name'] : '<span class="text-danger">ยังไม่ระบุ</span>'; ?>
</span>
</td>
<td><small class="text-muted"><?php echo $row['g_name'] ? $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; padding: 2px; 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">Username</label>
<input type="text" name="username" class="form-control" required placeholder="ใช้สำหรับ Login">
</div>
<div class="col-md-6">
<label class="form-label">Password (ค่าเริ่มต้น: 123456)</label>
<input type="password" name="password" class="form-control" value="123456" required>
</div>
<div class="col-md-12">
<label class="form-label">ชื่อ-นามสกุล</label>
<input type="text" name="fullname" class="form-control" required>
</div>
<div class="col-md-12">
<label class="form-label">ตำแหน่ง</label>
<input type="text" name="position" class="form-control" required placeholder="เช่น นักวิชาการคอมพิวเตอร์ชำนาญการ">
</div>
<div class="col-md-6">
<label class="form-label">ประเภทบุคลากร</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 $t['p_name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label">กลุ่มงาน</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 $g['g_name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-12">
<label class="form-label">สิทธิ์การใช้งานระบบ</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">รูปภาพลายเซ็น (PNG พื้นหลังโปร่งใส)</label>
<input type="file" name="signature" class="form-control" accept="image/*">
</div>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">ยกเลิก</button>
<button type="submit" class="btn btn-primary px-4">บันทึกข้อมูล</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 text-muted">Username (แก้ไขไม่ได้)</label>
<input type="text" id="edit_username" class="form-control bg-light" readonly>
</div>
<div class="col-md-6">
<label class="form-label">Password ใหม่ (เว้นว่างไว้หากไม่เปลี่ยน)</label>
<input type="password" name="password" class="form-control" placeholder="ใส่หากต้องการเปลี่ยน">
</div>
<div class="col-md-12">
<label class="form-label">ชื่อ-นามสกุล</label>
<input type="text" name="fullname" id="edit_fullname" class="form-control" required>
</div>
<div class="col-md-12">
<label class="form-label">ตำแหน่ง</label>
<input type="text" name="position" id="edit_position" class="form-control" required>
</div>
<div class="col-md-6">
<label class="form-label">ประเภทบุคลากร</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 $t['p_name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label class="form-label">กลุ่มงาน</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 $g['g_name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-12">
<label class="form-label">สิทธิ์การใช้งานระบบ</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">เปลี่ยนลายเซ็น (เลือกใหม่เพื่อเปลี่ยน)</label>
<input type="file" name="signature" class="form-control" accept="image/*">
</div>
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">ยกเลิก</button>
<button type="submit" class="btn btn-warning px-4">บันทึกการแก้ไข</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(){
var id = $(this).data('id');
var user = $(this).data('username');
var name = $(this).data('fullname');
var pos = $(this).data('position');
var gid = $(this).data('gid');
var pid = $(this).data('pid');
var type = $(this).data('type');
$('#edit_u_id').val(id);
$('#edit_username').val(user);
$('#edit_fullname').val(name);
$('#edit_position').val(pos);
$('#edit_gid').val(gid);
$('#edit_pid').val(pid);
$('#edit_type').val(type);
});
});
</script>
<?php include 'footer.php'; ?>