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

<?php 
require_once 'config.php';
require_once 'functions.php';

// ตรวจสอบการ Login
if(!isset($_SESSION['u_id'])){ header("Location: index.php"); exit(); }

$current_user_id = $_SESSION['u_id'];
$is_admin = ($_SESSION['type'] == 'admin');

// 1. ป้องกันการแก้ไข GET ID ใน URL
$att_id = isset($_GET['id']) ? $_GET['id'] : null;

// ตรวจสอบว่าเป็นตัวเลขเท่านั้น
if ($att_id !== null && !is_numeric($att_id)) {
    die("Access Denied: Invalid ID Format");
}

// ดึงข้อมูลการเข้างาน และตรวจสอบสิทธิ์ (ถ้าไม่ใช่ Admin ต้องดูได้เฉพาะของตัวเอง)
$where_clause = "";
if ($att_id) {
    $where_clause = " AND a.att_id = " . (int)$att_id;
}

$sql = "SELECT a.*, u.fullname, u.position, u.signature, g.g_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
        WHERE 1=1 ";

if ($att_id > 0) {
    $sql .= " AND a.att_id = '$att_id' ";
}

if (!$is_admin) {
    $sql .= " AND u.u_id = '$current_user_id' ";
}

$sql .= " ORDER BY a.att_date DESC, a.time_in DESC";

$res = mysqli_query($conn, $sql);

if (!$res) {
    die("SQL Error: " . mysqli_error($conn));
}

$data_list = [];
while($row = mysqli_fetch_assoc($res)){ 
    $data_list[] = $row; 
}

if (empty($data_list)) { 
    die("<div style='text-align:center; padding:50px;'>ไม่พบข้อมูลที่ต้องการพิมพ์</div>");
}

$user_info = $data_list[0];
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <title>รายงานการปฏิบัติงาน - <?php echo $user_info['fullname']; ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;600&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Sarabun', sans-serif; font-size: 16px; color: #000; margin: 0; padding: 40px; }
        .header { text-align: center; margin-bottom: 30px; line-height: 1.8; }
        .title { font-size: 20px; font-weight: bold; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        table, th, td { border: 1px solid #333; }
        th { background-color: #f2f2f2; padding: 12px; text-align: center; }
        td { padding: 10px; vertical-align: top; }
        
        .signature-container {
            margin-top: 50px;
            float: right;
            width: 350px;
            text-align: center;
        }
        .signature-img {
            max-height: 80px;
            display: block;
            margin: 0 auto -15px auto; /* ดึงภาพลงมาทับเส้นบรรทัดเล็กน้อยเพื่อความสมจริง */
        }
        .sign-line { margin-bottom: 10px; }
        
        @media print {
            .no-print { display: none; }
            body { padding: 0; }
        }
    </style>
</head>
<body onload="window.print()">

    <div class="no-print" style="margin-bottom: 20px; text-align: right;">
        <button onclick="window.print()" style="padding: 10px 20px; cursor: pointer;">พิมพ์รายงานนี้ (Print)</button>
        <button onclick="window.close()" style="padding: 10px 20px; cursor: pointer;">ปิดหน้าต่าง</button>
    </div>

    <div class="header">
        <div class="title">แบบบันทึกการปฏิบัติงานรายวัน</div>
        <div><strong>ชื่อ-นามสกุล:</strong> <?php echo $user_info['fullname']; ?> &nbsp;&nbsp; <strong>กลุ่มงาน:</strong> <?php echo $user_info['g_name']; ?></div>
    </div>

    <table>
        <thead>
            <tr>
                <th width="15%">วันที่</th>
                <th width="12%">เวลาเข้า</th>
                <th width="12%">เวลากลับ</th>
                <th>รายละเอียดภารกิจการปฏิบัติงาน</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($data_list as $row): ?>
            <tr>
                <td align="center"><?php echo DateThai($row['att_date']); ?></td>
                <td align="center"><?php echo $row['time_in']; ?></td>
                <td align="center"><?php echo ($row['time_out'] == "00:00:00" ? "-" : $row['time_out']); ?></td>
                <td class="work-content-cell"><?php echo $row['work_detail']; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <div class="signature-container">
        <div class="sign-wrapper">
            <?php if(!empty($user_info['signature'])): ?>
                <img src="uploads/<?php echo $user_info['signature']; ?>" class="signature-img">
            <?php else: ?>
                <div style="height: 65px;"></div>
            <?php endif; ?>
            
            <div class="sign-line">ลงชื่อ..........................................................</div>
            <div>( <?php echo $user_info['fullname']; ?> )</div>
            <div>ตำแหน่ง <?php echo $user_info['position']; ?></div>
        </div>
    </div>

</body>
</html>