<?php
require 'vendor/autoload.php';
require 'koneksi.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$bulan = isset($_GET['bulan']) ? $_GET['bulan'] : date('n');
$tahun = isset($_GET['tahun']) ? $_GET['tahun'] : date('Y');

$jumlah_hari = cal_days_in_month(CAL_GREGORIAN, $bulan, $tahun);
$tgl_awal = "$tahun-" . str_pad($bulan, 2, '0', STR_PAD_LEFT) . "-01";
$tgl_akhir = "$tahun-" . str_pad($bulan, 2, '0', STR_PAD_LEFT) . "-$jumlah_hari";

// Array hari kerja (Senin - Jumat)
$hari_kerja = [];
for ($i = 1; $i <= $jumlah_hari; $i++) {
    $tanggal = "$tahun-" . str_pad($bulan, 2, '0', STR_PAD_LEFT) . "-" . str_pad($i, 2, '0', STR_PAD_LEFT);
    $day_of_week = date('N', strtotime($tanggal)); // 1=Senin, ..., 7=Minggu
    if ($day_of_week < 6) {
        $hari_kerja[] = $tanggal;
    }
}
$total_hari_kerja = count($hari_kerja);

$query = "
    SELECT k.nama, a.tanggal, a.jam_masuk, a.jam_pulang
    FROM absensi a
    JOIN karyawan k ON a.rfid = k.rfid
    WHERE a.tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir'
    ORDER BY k.nama, a.tanggal
";
$result = mysqli_query($conn, $query);

// HTML Template
$html = '<html><body>';
$html .= '<h2 style="text-align:center;">DATA ABSENSI</h2>';
$html .= '<h4 style="text-align:center;">BULAN ' . strtoupper(date('F', mktime(0, 0, 0, $bulan, 1))) . ' ' . $tahun . '</h4>';
$html .= '<hr><br>';
$html .= '<table border="1" cellpadding="5" cellspacing="0" style="width:100%; font-size:12px; border-collapse:collapse;">';
$html .= '<tr>
    <th>Nama</th>
    <th>Hadir (Hari)</th>
    <th>Tidak Hadir (Hari)</th>
    <th>Total Hari Kerja</th>
</tr>';

$prev_name = '';
$summary = [];
$data_absen = [];

// Susun data absensi
while ($row = mysqli_fetch_assoc($result)) {
    $nama = $row['nama'];
    $tanggal = $row['tanggal'];

    if (!isset($data_absen[$nama])) {
        $data_absen[$nama] = [];
    }

    $data_absen[$nama][$tanggal] = [
        'masuk' => $row['jam_masuk'],
        'pulang' => $row['jam_pulang'],
    ];
}

// Rekap per karyawan
foreach ($data_absen as $nama => $absensi) {
    $hadir = 0;

    foreach ($hari_kerja as $tgl) {
        if (isset($absensi[$tgl]) && $absensi[$tgl]['masuk'] && $absensi[$tgl]['pulang']) {
            $hadir++;
        }
    }

    $tidak_hadir = $total_hari_kerja - $hadir;

    $html .= "<tr>
        <td>{$nama}</td>
        <td style='text-align:center'>{$hadir}</td>
        <td style='text-align:center'>{$tidak_hadir}</td>
        <td style='text-align:center'>{$total_hari_kerja}</td>
    </tr>";
}

$html .= '</table>';

$html .= '<br><div style="text-align:right; font-size:12px;">
    Jakarta, ' . date('d F Y') . '<br><br>
    <p>Admin</p><br><br>
    <hr style="width:200px;">
    <p style="text-align:center;">Mengetahui, Manajer Operasional</p>
</div>';

$html .= '</body></html>';

mysqli_close($conn);

// DOMPDF
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("Rekap_Absensi_Bulan_$bulan-$tahun.pdf", array("Attachment" => 0));
?>
