<?php
include "../config/koneksi.php";
include "../middleware/auth.php";
include "../config/komplain_workflow.php";

sikompro_require_role('admin');

$mulai = $_GET['mulai'] ?? date('Y-m-01');
$selesai = $_GET['selesai'] ?? date('Y-m-t');
$role = $_GET['role'] ?? '';
$mulaiSql = mysqli_real_escape_string($conn, $mulai);
$selesaiSql = mysqli_real_escape_string($conn, $selesai);
$roleSql = in_array($role, ['admin', 'pelaksana', 'teknisi'], true) ? " AND u.role='" . mysqli_real_escape_string($conn, $role) . "'" : "";

$data = mysqli_query($conn, "
    SELECT a.*, u.username, u.role AS user_role
    FROM absensi a
    JOIN users u ON a.user_id = u.id
    WHERE a.tanggal BETWEEN '{$mulaiSql}' AND '{$selesaiSql}' {$roleSql}
    ORDER BY a.tanggal DESC, a.id DESC
");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cetak Absensi | PT Alvindo</title>
<style>
body{font-family:Arial,sans-serif;margin:24px;color:#1f2937;background:#fff}
h2{margin:0 0 6px}
.meta{margin:0;color:#6b7280}
table{width:100%;border-collapse:collapse;margin-top:18px}
th,td{border:1px solid #cbd5e1;padding:8px;font-size:12px;vertical-align:top}
th{background:#e2e8f0;text-align:left}
.toolbar{margin:16px 0;display:flex;gap:8px;flex-wrap:wrap}
.toolbar a,.toolbar button{padding:7px 12px;border:1px solid #94a3b8;border-radius:8px;background:#fff;color:#1f2937;text-decoration:none;cursor:pointer}
.toolbar .primary{background:#0d6efd;border-color:#0d6efd;color:#fff}
.thumb{width:54px;height:54px;object-fit:cover;border-radius:8px;border:1px solid #d1d5db}
@media print{.toolbar{display:none}body{margin:0}.thumb{width:42px;height:42px}}
@media (max-width: 768px){
    body{margin:12px;font-size:13px}
    h2{font-size:1.2rem}
    .toolbar{margin:12px 0}
    .toolbar a,.toolbar button{width:100%;justify-content:center}
    .toolbar a,.toolbar button{display:flex;align-items:center}
    table{
        display:block;
        width:max-content;
        min-width:1200px;
        overflow-x:auto;
        -webkit-overflow-scrolling:touch;
    }
    th,td{
        white-space:nowrap;
        font-size:11px;
    }
}
</style>
</head>
<body>
<h2>Data Absensi</h2>
<p class="meta">Periode <?= htmlspecialchars($mulai) ?> sampai <?= htmlspecialchars($selesai) ?><?= $role ? ' - ' . htmlspecialchars($role) : '' ?></p>
<div class="toolbar">
    <button type="button" class="primary" onclick="window.print()">Cetak / Simpan PDF</button>
    <a href="data_absensi.php?mulai=<?= urlencode($mulai) ?>&selesai=<?= urlencode($selesai) ?>&role=<?= urlencode($role) ?>">Kembali</a>
</div>
<table>
<thead>
<tr>
<th>No</th>
<th>User</th>
<th>Role</th>
<th>Tanggal</th>
<th>Masuk</th>
<th>Pulang</th>
<th>Lembur</th>
<th>Status</th>
<th>Lokasi</th>
<th>Foto Masuk</th>
<th>Foto Pulang</th>
<th>Foto Lembur</th>
<th>Keterangan Lembur</th>
</tr>
</thead>
<tbody>
<?php $no = 1; while ($r = mysqli_fetch_assoc($data)): ?>
<tr>
<td><?= $no++ ?></td>
<td><?= htmlspecialchars($r['username']) ?></td>
<td><?= htmlspecialchars($r['user_role']) ?></td>
<td><?= htmlspecialchars($r['tanggal']) ?></td>
<td><?= htmlspecialchars((string) $r['jam_masuk']) ?></td>
<td><?= htmlspecialchars((string) ($r['jam_pulang'] ?: '-')) ?></td>
<td><?= htmlspecialchars(komplain_absensi_overtime_label($r)) ?></td>
<td><?= htmlspecialchars((string) $r['status']) ?></td>
<td><?= htmlspecialchars(komplain_absensi_location_label($r)) ?></td>
<td>
    <?php if (!empty($r['foto'])): ?>
        <img class="thumb" src="../uploads/absen/<?= rawurlencode($r['foto']) ?>" alt="Foto masuk">
    <?php else: ?>
        -
    <?php endif; ?>
</td>
<td>
    <?php if (!empty($r['foto_pulang'])): ?>
        <img class="thumb" src="../uploads/absen/<?= rawurlencode($r['foto_pulang']) ?>" alt="Foto pulang">
    <?php else: ?>
        -
    <?php endif; ?>
</td>
<td>
    <?php if (!empty($r['foto_lembur'])): ?>
        <img class="thumb" src="../uploads/absen/<?= rawurlencode($r['foto_lembur']) ?>" alt="Foto lembur">
    <?php else: ?>
        -
    <?php endif; ?>
</td>
<td><?= nl2br(htmlspecialchars((string) ($r['keterangan_lembur'] ?? '-'))) ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php include '../partials/mobile_nav.php'; ?>
</body>
</html>

