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

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

// ตรวจสอบสิทธิ์ Admin [cite: 2025-07-09]
if($_SESSION['type'] != 'admin'){ 
    exit("Access Denied: คุณไม่มีสิทธิ์เข้าถึงรายงานนี้"); 
}

// รับค่าจาก GET [cite: 2025-07-09]
$date = isset($_GET['date']) ? mysqli_real_escape_string($conn, $_GET['date']) : date('Y-m-d');
$p_id = isset($_GET['p_id']) ? (int)$_GET['p_id'] : 0;

// ดึงชื่อประเภทบุคลากร (กรณีมีการกรองประเภท) [cite: 2025-07-09]
$type_name = "บุคลากรทุกประเภท";
if($p_id > 0) {
    $t_res = mysqli_query($conn, "SELECT p_name FROM personnel_types WHERE p_id = '$p_id'");
    $t_row = mysqli_fetch_assoc($t_res);
    if($t_row) $type_name = $t_row['p_name'];
}

// สร้างคำสั่ง SQL ตามเงื่อนไขที่เลือก [cite: 2025-07-09]
$where = " WHERE a.att_date = '$date' ";
if($p_id > 0) {
    $where .= " AND u.p_id = '$p_id' ";
}

$sql = "SELECT a.*, u.fullname, u.signature, pt.p_name 
        FROM attendance a 
        JOIN users u ON a.u_id = u.u_id 
        LEFT JOIN personnel_types pt ON u.p_id = pt.p_id
        $where 
        ORDER BY a.time_in ASC";
$res = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <title>รายงานการลงเวลา - <?php echo $type_name; ?></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: 14px; color: #000; padding: 20px; line-height: 1.4; }
        .header { text-align: center; margin-bottom: 25px; }
        .title { font-size: 18px; font-weight: bold; margin-bottom: 5px; }
        .subtitle { font-size: 15px; margin-bottom: 5px; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        table, th, td { border: 1px solid #000; }
        th { padding: 10px 5px; background-color: #f5f5f5; text-align: center; font-weight: 600; }
        td { padding: 8px 6px; vertical-align: middle; }
        
        .sig-container { height: 50px; display: flex; align-items: center; justify-content: center; }
        .sig-img { max-height: 45px; max-width: 110px; filter: grayscale(1); } /* ปรับเป็นขาวดำ [cite: 2025-07-09] */
        
        .footer-sign { margin-top: 40px; float: right; width: 300px; text-align: center; }
        
        @media print {
            @page { size: A4; margin: 1.5cm; }
            .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: 8px 20px; background: #333; color: #fff; border: none; cursor: pointer; border-radius: 5px;">
            <i class="fas fa-print"></i> พิมพ์รายงาน
        </button>
    </div>

    <div class="header">
        <div class="title">บัญชีลงเวลาปฏิบัติงานบุคลากร</div>
        <div class="subtitle"><strong>ประเภท:</strong> <?php echo $type_name; ?></div>
        <div>ประจำวันที่ <?php echo DateThai($date); ?></div>
    </div>

    <table>
        <thead>
            <tr>
                <th width="5%">ที่</th>
                <th width="22%">ชื่อ - นามสกุล</th>
                <th width="10%">เวลามา</th>
                <th width="23%">ลายเซ็น (มา)</th>
                <th width="10%">เวลากลับ</th>
                <th width="23%">ลายเซ็น (กลับ)</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $i = 1;
            while($row = mysqli_fetch_assoc($res)): 
            ?>
            <tr>
                <td align="center"><?php echo $i++; ?></td>
                <td><?php echo $row['fullname']; ?></td>
                <td align="center"><?php echo $row['time_in']; ?></td>
                <td align="center">
                    <div class="sig-container">
                        <?php if($row['signature']): ?>
                            <img src="uploads/<?php echo $row['signature']; ?>" class="sig-img">
                        <?php endif; ?>
                    </div>
                </td>
                <td align="center">
                    <?php echo ($row['time_out'] == "00:00:00" || empty($row['time_out'])) ? "-" : $row['time_out']; ?>
                </td>
                <td align="center">
                    <div class="sig-container">
                        <?php 
                        // ลายเซ็นกลับจะปรากฏเมื่อมีการลงเวลากลับแล้วเท่านั้น [cite: 2025-07-09]
                        if($row['signature'] && $row['time_out'] != "00:00:00" && !empty($row['time_out'])): 
                        ?>
                            <img src="uploads/<?php echo $row['signature']; ?>" class="sig-img">
                        <?php endif; ?>
                    </div>
                </td>
            </tr>
            <?php endwhile; ?>
            
            <?php if(mysqli_num_rows($res) == 0): ?>
                <tr>
                    <td colspan="6" align="center" style="padding: 40px; color: #666;">
                        ไม่พบข้อมูลการปฏิบัติงานของ<?php echo $type_name; ?> ในวันที่เลือก
                    </td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

    <div class="footer-sign">
        <p>ตรวจสอบแล้วถูกต้อง</p>
        <br><br>
        <p>..........................................................</p>
        <p>(..........................................................)</p>
        <p>ตำแหน่ง......................................................</p>
    </div>

</body>
</html>