<?php
// modules/[nama_file].php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Path BENAR untuk file di folder modules/
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/app.php';

checkAuth();
$conn = getConnection();
$date = $_GET['date'] ?? date('Y-m-d');

// Set headers for Excel download
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="rekap_kegiatan_' . $date . '.xls"');

$jenis_kegiatan = [
    'asuhan_keperawatan' => 'Asuhan Keperawatan',
    'manajerial' => 'Manajerial',
    'koordinasi' => 'Koordinasi',
    'supervisi' => 'Supervisi',
    'evaluasi' => 'Evaluasi',
    'monitoring' => 'Monitoring',
    'lainnya' => 'Lainnya'
];

// Get all units
$units = [];
$result = $conn->query("SELECT * FROM units WHERE is_active = 1 ORDER BY name");
while ($row = $result->fetch_assoc()) {
    $units[] = $row;
}

// Get activities
$activities = [];
$stmt = $conn->prepare("
    SELECT a.*, u.name as unit_name 
    FROM daily_activities a
    JOIN units u ON a.unit_id = u.id
    WHERE a.activity_date = ?
    ORDER BY u.name, a.start_time
");
$stmt->bind_param("s", $date);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    $activities[] = $row;
}

// Hitung durasi
function hitungDurasi($start, $end) {
    if (!$start || !$end) return 0;
    $start_time = strtotime($start);
    $end_time = strtotime($end);
    if ($end_time <= $start_time) return 0;
    return ($end_time - $start_time) / 60;
}

// Output Excel
echo "<html>";
echo "<head>";
echo "<meta charset='UTF-8'>";
echo "<title>Rekap Kegiatan $date</title>";
echo "<style>";
echo "th { background: #ddd; }";
echo "td, th { border: 1px solid #999; padding: 5px; }";
echo "table { border-collapse: collapse; }";
echo "</style>";
echo "</head>";
echo "<body>";

echo "<h2>REKAP KEGIATAN HARIAN KEPERAWATAN</h2>";
echo "<h3>Tanggal: " . date('d-m-Y', strtotime($date)) . "</h3>";

// Tabel per Unit
foreach ($units as $unit) {
    $unit_activities = array_filter($activities, fn($a) => $a['unit_id'] == $unit['id']);
    
    echo "<h3>" . $unit['name'] . "</h3>";
    echo "<table>";
    echo "<tr>";
    echo "<th>No</th>";
    echo "<th>Level</th>";
    echo "<th>Jenis Kegiatan</th>";
    echo "<th>Nama Kegiatan</th>";
    echo "<th>Jam Mulai</th>";
    echo "<th>Jam Selesai</th>";
    echo "<th>Durasi (menit)</th>";
    echo "<th>Keterangan</th>";
    echo "</tr>";
    
    if (empty($unit_activities)) {
        echo "<tr><td colspan='8' align='center'>Tidak ada kegiatan</td></tr>";
    } else {
        $no = 1;
        foreach ($unit_activities as $act) {
            $durasi = hitungDurasi($act['start_time'], $act['end_time']);
            echo "<tr>";
            echo "<td>" . $no++ . "</td>";
            echo "<td>" . $act['level'] . "</td>";
            echo "<td>" . ($jenis_kegiatan[$act['jenis_kegiatan']] ?? $act['jenis_kegiatan']) . "</td>";
            echo "<td>" . $act['activity_name'] . "</td>";
            echo "<td>" . ($act['start_time'] ? substr($act['start_time'], 0, 5) : '-') . "</td>";
            echo "<td>" . ($act['end_time'] ? substr($act['end_time'], 0, 5) : '-') . "</td>";
            echo "<td>" . ($durasi ?: '-') . "</td>";
            echo "<td>" . ($act['description'] ?? '-') . "</td>";
            echo "</tr>";
        }
    }
    
    echo "</table>";
    echo "<br>";
}

// Rekap Jenis Kegiatan
echo "<h3>REKAP JENIS KEGIATAN</h3>";
echo "<table>";
echo "<tr>";
echo "<th>Jenis Kegiatan</th>";
echo "<th>Jumlah</th>";
echo "<th>Total Durasi (jam)</th>";
echo "</tr>";

$rekap = [];
foreach ($activities as $act) {
    $jenis = $act['jenis_kegiatan'] ?? 'lainnya';
    if (!isset($rekap[$jenis])) {
        $rekap[$jenis] = ['total' => 0, 'durasi' => 0];
    }
    $rekap[$jenis]['total']++;
    $rekap[$jenis]['durasi'] += hitungDurasi($act['start_time'], $act['end_time']);
}

foreach ($rekap as $jenis => $data) {
    echo "<tr>";
    echo "<td>" . ($jenis_kegiatan[$jenis] ?? $jenis) . "</td>";
    echo "<td align='center'>" . $data['total'] . "</td>";
    echo "<td align='center'>" . round($data['durasi'] / 60, 1) . "</td>";
    echo "</tr>";
}

echo "</table>";
echo "</body>";
echo "</html>";
?>