<?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');
$mulaiSql = mysqli_real_escape_string($conn, $mulai);
$selesaiSql = mysqli_real_escape_string($conn, $selesai);

$data = mysqli_query($conn, "
SELECT u.username, u.role, u.gaji_harian, u.gaji_lembur_per_jam, u.gaji_lembur_total,
       COUNT(a.id) AS hari_masuk,
       COALESCE(SUM(CASE
            WHEN a.jam_lembur_mulai IS NOT NULL AND a.jam_lembur_selesai IS NOT NULL
            THEN LEAST(TIMESTAMPDIFF(MINUTE, a.jam_lembur_mulai, a.jam_lembur_selesai), 1440)
            ELSE 0
       END), 0) AS lembur_menit
FROM users u
LEFT JOIN absensi a ON a.user_id = u.id
    AND a.tanggal BETWEEN '{$mulaiSql}' AND '{$selesaiSql}'
    AND a.jam_masuk IS NOT NULL
WHERE u.role IN ('admin','pelaksana','teknisi')
GROUP BY u.id, u.username, u.role, u.gaji_harian, u.gaji_lembur_per_jam, u.gaji_lembur_total
ORDER BY FIELD(u.role,'admin','pelaksana','teknisi'), u.username
");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cetak Gaji | PT Alvindo</title>
<style>
body{font-family:Arial,sans-serif;margin:24px;color:#1f2937;background:#fff}
h2{margin:0 0 4px}
.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}
@media print{.toolbar{display:none}body{margin:0}}
</style>
</head>
<body>
<h2>Rangkuman Gaji</h2>
<p class="meta">Periode <?= htmlspecialchars($mulai) ?> sampai <?= htmlspecialchars($selesai) ?>, dicetak <?= date('d-m-Y H:i:s') ?>. Lembur bisa memakai nominal total manual atau perhitungan jam sebagai cadangan.</p>
<div class="toolbar">
    <button type="button" class="primary" onclick="window.print()">Cetak / Simpan PDF</button>
    <a href="gaji.php?mulai=<?= urlencode($mulai) ?>&selesai=<?= urlencode($selesai) ?>">Kembali</a>
</div>
<table>
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Role</th>
<th>Hari Masuk</th>
<th>Gaji Harian</th>
<th>Gaji Lembur Total</th>
<th>Lembur</th>
<th>Rate Lembur/Jam</th>
<th>Total Gaji</th>
</tr>
</thead>
<tbody>
<?php $no = 1; $grand = 0; while ($row = mysqli_fetch_assoc($data)): 
    $lemburJam = komplain_absensi_overtime_hours($row);
    $rate = komplain_absensi_overtime_rate($row);
    $manualLembur = (float) ($row['gaji_lembur_total'] ?? 0);
    $lemburTotal = $manualLembur > 0 ? $manualLembur : komplain_absensi_overtime_subtotal($row, $rate);
    $total = ((int) $row['hari_masuk'] * (float) $row['gaji_harian']) + $lemburTotal;
    $grand += $total;
?>
<tr>
<td><?= $no++ ?></td>
<td><?= htmlspecialchars($row['username']) ?></td>
<td><?= htmlspecialchars($row['role']) ?></td>
<td><?= (int) $row['hari_masuk'] ?></td>
<td>Rp <?= number_format((float) $row['gaji_harian'], 0, ',', '.') ?></td>
<td>Rp <?= number_format($lemburTotal, 0, ',', '.') ?></td>
<td><?= number_format($lemburJam, 2, ',', '.') ?> jam</td>
<td>Rp <?= number_format($rate, 0, ',', '.') ?></td>
<td>Rp <?= number_format($total, 0, ',', '.') ?></td>
</tr>
<?php endwhile; ?>
<tr>
<th colspan="8">Total Keseluruhan</th>
<th>Rp <?= number_format($grand, 0, ',', '.') ?></th>
</tr>
</tbody>
</table>
<?php include '../partials/mobile_nav.php'; ?>
</body>
</html>

