<?php include 'header.php'; ?>
<?php
// ตรวจสอบสิทธิ์ Admin
if ($_SESSION['type'] != 'admin') {
header("Location: user_dashboard.php");
exit();
}
// ปรับปรุงการดึงข้อมูลกลุ่มงาน
$sql_g = "SELECT * FROM `groups` ORDER BY g_id ASC";
$res_groups = mysqli_query($conn, $sql_g);
// ตรวจสอบ Error ของ Query สำหรับ PHP 7.3
if (!$res_groups) {
echo "Error: " . mysqli_error($conn);
}
?>
<div class="row">
<div class="col-md-8 mx-auto">
<div class="card border-0 shadow-sm">
<div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
<h5 class="mb-0 fw-bold text-primary"><i class="fas fa-layer-group me-2"></i>จัดการกลุ่มงาน</h5>
<button class="btn btn-sm btn-success rounded-pill px-3" data-bs-toggle="modal" data-bs-target="#addGroupModal">
<i class="fas fa-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 width="15%" class="text-center">ลำดับที่</th>
<th>ชื่อกลุ่มงาน</th>
<th width="20%" class="text-center">จัดการ</th>
</tr>
</thead>
<tbody>
<?php
if (mysqli_num_rows($res_groups) > 0) {
$no = 1;
while($row = mysqli_fetch_assoc($res_groups)):
?>
<tr>
<td class="text-center"><?php echo $no++; ?></td>
<td><strong><?php echo htmlspecialchars($row['g_name']); ?></strong></td>
<td class="text-center">
<a href="process.php?action=del_group&id=<?php echo $row['g_id']; ?>"
class="btn btn-sm btn-outline-danger" onclick="return confirm('ยืนยันการลบกลุ่มงาน?');">
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
<?php
endwhile;
} else {
echo "<tr><td colspan='3' class='text-center py-4 text-muted'>ไม่มีข้อมูลกลุ่มงาน</td></tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="addGroupModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content border-0 shadow">
<form action="process.php" method="post">
<input type="hidden" name="action" value="add_group">
<div class="modal-header">
<h5 class="modal-title fw-bold">เพิ่มกลุ่มงานใหม่</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">ชื่อกลุ่มงาน</label>
<input type="text" name="g_name" class="form-control" placeholder="กรอกชื่อกลุ่มงาน..." required>
</div>
</div>
<div class="modal-footer bg-light">
<button type="submit" class="btn btn-success px-4">บันทึกข้อมูล</button>
</div>
</form>
</div>
</div>
</div>
<?php include 'footer.php'; ?>