Path : /var/www/html/work123/
File Upload :
Current File : /var/www/html/work123/admin_dashboard.php

<?php include 'header.php'; ?>

<?php
// ตรวจสอบสิทธิ์การเข้าใช้งานเฉพาะ Admin [cite: 2025-07-09]
if ($_SESSION['type'] != 'admin') {
    header("Location: user_dashboard.php");
    exit();
}

// รับค่าฟิลเตอร์และจัดการความปลอดภัยของข้อมูล [cite: 2025-07-09]
$view_date = isset($_GET['view_date']) ? mysqli_real_escape_string($conn, $_GET['view_date']) : date('Y-m-d');
$p_id = isset($_GET['p_id']) ? (int)$_GET['p_id'] : 0;

// ดึงประเภทบุคลากรเพื่อทำ Dropdown
$types_res = mysqli_query($conn, "SELECT * FROM personnel_types ORDER BY p_id ASC");

// กำหนดเงื่อนไข SQL (ใส่ Backticks ครอบ `groups` เพื่อรองรับ PHP 7.3 และ MySQL 5.7+) [cite: 2025-07-09]
$where = " WHERE a.att_date = '$view_date' ";
if ($p_id > 0) { 
    $where .= " AND u.p_id = '$p_id' "; 
}

$sql_list = "SELECT a.*, u.fullname, u.position, g.g_name, pt.p_name 
             FROM attendance a 
             JOIN users u ON a.u_id = u.u_id 
             LEFT JOIN `groups` g ON u.g_id = g.g_id 
             LEFT JOIN personnel_types pt ON u.p_id = pt.p_id
             $where 
             ORDER BY a.time_in ASC";

$res_list = mysqli_query($conn, $sql_list);

// ตรวจสอบ Error ของ Query หากข้อมูลไม่ขึ้น
if (!$res_list) {
    die("SQL Error: " . mysqli_error($conn));
}

$count_today = mysqli_num_rows($res_list);
?>

<div class="row mb-4 align-items-center">
    <div class="col-md-5">
        <h4 class="fw-bold text-dark mb-0">
            <i class="fas fa-desktop me-2 text-primary"></i>แผงควบคุมผู้ดูแลระบบ
        </h4>
        <small class="text-muted">ตรวจสอบการปฏิบัติงานประจำวันที่ <?php echo DateThai($view_date); ?></small>
    </div>
    <div class="col-md-7 text-md-end mt-3 mt-md-0">
        <div class="btn-group shadow-sm">
            <a href="admin_users.php" class="btn btn-white border border-end-0 bg-white">
                <i class="fas fa-users-cog me-1 text-primary"></i> จัดการบุคลากร
            </a>
            <a href="admin_groups.php" class="btn btn-white border border-end-0 bg-white">
                <i class="fas fa-layer-group me-1 text-success"></i> จัดการกลุ่มงาน
            </a>
            <a href="admin_types.php" class="btn btn-white border bg-white">
                <i class="fas fa-id-card-alt me-1 text-info"></i> จัดการประเภทบุคลากร
            </a>
        </div>
    </div>
</div>

<hr class="mb-4 opacity-50">

<div class="card border-0 shadow-sm mb-4">
    <div class="card-body bg-light rounded-3">
        <form method="get" action="admin_dashboard.php" class="row g-3 align-items-end">
            <div class="col-md-4">
                <label class="form-label small fw-bold">เลือกวันที่</label>
                <input type="date" name="view_date" class="form-control" value="<?php echo $view_date; ?>" onchange="this.form.submit()">
            </div>
            <div class="col-md-4">
                <label class="form-label small fw-bold">ประเภทบุคลากร</label>
                <select name="p_id" class="form-select" onchange="this.form.submit()">
                    <option value="0">-- ทุกประเภท --</option>
                    <?php 
                    mysqli_data_seek($types_res, 0);
                    while($t = mysqli_fetch_assoc($types_res)): 
                    ?>
                        <option value="<?php echo $t['p_id']; ?>" <?php if($p_id == $t['p_id']) echo 'selected'; ?>>
                            <?php echo htmlspecialchars($t['p_name']); ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="col-md-4 text-md-end">
                <a href="print_daily_report.php?date=<?php echo $view_date; ?>&p_id=<?php echo $p_id; ?>" target="_blank" class="btn btn-dark w-100">
                    <i class="fas fa-print me-2"></i>พิมพ์รายงานประจำวัน
                </a>
            </div>
        </form>
    </div>
</div>

<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">รายชื่อบุคลากรที่มาปฏิบัติงาน</h5>
        <span class="badge bg-primary rounded-pill">มาปฏิบัติงาน <?php echo $count_today; ?> คน</span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th width="5%" class="text-center">ที่</th>
                        <th width="12%" class="text-center">เวลามา</th>
                        <th width="12%" class="text-center">เวลากลับ</th>
                        <th>ชื่อ-นามสกุล / ตำแหน่ง</th>
                        <th>ประเภท / กลุ่มงาน</th>
                        <th width="15%" class="text-center">จัดการ</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $no = 1; 
                    while($row = mysqli_fetch_assoc($res_list)): 
                    ?>
                    <tr>
                        <td class="text-center"><?php echo $no++; ?></td>
                        <td class="text-center">
                            <span class="badge bg-light text-primary border px-2"><?php echo $row['time_in']; ?></span>
                        </td>
                        <td class="text-center">
                            <?php if($row['time_out'] != "00:00:00" && !empty($row['time_out'])): ?>
                                <span class="badge bg-light text-danger border px-2"><?php echo $row['time_out']; ?></span>
                            <?php else: ?>
                                <small class="text-muted">ยังไม่กลับ</small>
                            <?php endif; ?>
                        </td>
                        <td class="ps-3">
                            <div class="fw-bold"><?php echo htmlspecialchars($row['fullname']); ?></div>
                            <small class="text-muted"><?php echo htmlspecialchars($row['position']); ?></small>
                        </td>
                        <td>
                            <div class="badge bg-secondary fw-normal mb-1"><?php echo htmlspecialchars($row['p_name']); ?></div><br>
                            <small class="text-muted"><?php echo htmlspecialchars($row['g_name']); ?></small>
                        </td>
                        <td class="text-center">
                            <button class="btn btn-sm btn-outline-info view-work-btn" 
                                    data-name="<?php echo htmlspecialchars($row['fullname']); ?>"
                                    data-work='<?php echo htmlspecialchars($row['work_detail'], ENT_QUOTES); ?>'
                                    data-bs-toggle="modal" data-bs-target="#viewWorkModal">
                                <i class="fas fa-search"></i>
                            </button>
                            <a href="print_report.php?id=<?php echo $row['att_id']; ?>" target="_blank" class="btn btn-sm btn-outline-secondary ms-1">
                                <i class="fas fa-print"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                    <?php if(mysqli_num_rows($res_list) == 0) echo "<tr><td colspan='6' class='text-center py-5 text-muted'>ไม่พบข้อมูลการปฏิบัติงานในวันที่เลือก</td></tr>"; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="viewWorkModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-info text-white border-0">
                <h5 class="modal-title fw-bold"><i class="fas fa-tasks me-2"></i>รายละเอียดภารกิจ</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-4">
                <h6 class="fw-bold text-primary mb-3" id="work_person_name"></h6>
                <div id="work_detail_content" class="p-3 bg-light rounded border shadow-sm" style="min-height: 150px;"></div>
            </div>
            <div class="modal-footer border-0">
                <button type="button" class="btn btn-secondary px-4 rounded-pill" data-bs-dismiss="modal">ปิดหน้าต่าง</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
    $('.view-work-btn').on('click', function(){
        var name = $(this).data('name');
        var work = $(this).data('work');
        $('#work_person_name').text('คุณ' + name);
        $('#work_detail_content').html(work ? work : '<span class="text-muted">ไม่ได้ระบุรายละเอียดภารกิจ</span>');
    });
});
</script>

<?php include 'footer.php'; ?>