<?php
// Variables: $entry (assoc array), $page (['w_mm'=>float,'h_mm'=>float])
$e = $entry ?? [];
function esc_html($s)
{
  return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8');
}
$schoolYearRaw = (string)($e['school_year'] ?? '');
// Reduce school year to "xx-xx" if possible (e.g., 2025-2026 -> 25-26)
$schoolYearFmt = $schoolYearRaw;
if (preg_match('/(\d{2,4})\D+(\d{2,4})/', $schoolYearRaw, $m)) {
  $a = substr($m[1], -2);
  $b = substr($m[2], -2);
  $schoolYearFmt = $a . '-' . $b;
} elseif (preg_match('/^\d{4}$/', $schoolYearRaw)) {
  $y = (int)$schoolYearRaw;
  if ($y > 0) {
    $schoolYearFmt = substr($schoolYearRaw, -2) . '-' . substr((string)($y + 1), -2);
  }
}
$schoolYear  = esc_html($schoolYearFmt);
$studentName = esc_html($e['student_name'] ?? '');
$dateRaw     = $e['date'] ?? '';
// Normalize slip date without flipping month/day.
if (!function_exists('format_slip_date')) {
  function format_slip_date($raw): string
  {
    $raw = trim((string)$raw);
    if ($raw === '') return '';
    if (preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $raw)) {
      $dt = DateTime::createFromFormat('!Y-m-d', $raw);
      return $dt ? $dt->format('m-d-Y') : $raw;
    }
    if (preg_match('/^\\d{1,2}\\/\\d{1,2}\\/\\d{4}$/', $raw)) {
      $dt = DateTime::createFromFormat('!m/d/Y', $raw);
      return $dt ? $dt->format('m-d-Y') : $raw;
    }
    if (preg_match('/^\\d{1,2}-\\d{1,2}-\\d{4}$/', $raw)) {
      $dt = DateTime::createFromFormat('!m-d-Y', $raw);
      return $dt ? $dt->format('m-d-Y') : $raw;
    }
    try {
      return local_date($raw, 'm-d-Y');
    } catch (Throwable $e) {
      return $raw;
    }
  }
}
$date        = $dateRaw !== '' ? esc_html(format_slip_date($dateRaw)) : '';
$timeIn      = esc_html($e['time_in']      ?? '');
$grade       = esc_html($e['grade']        ?? '');
$reason      = esc_html($e['reason']       ?? '');
$adminName   = esc_html($e['admin_name']   ?? '');
$page        = isset($page) && is_array($page) ? $page : ['w_mm' => 85.60, 'h_mm' => 53.98];
?>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Late Slip</title>
  <style>
    /* Page size dynamic based on controller selection */
    @page {
      size: <?= number_format((float)$page['w_mm'], 2) ?>mm <?= number_format((float)$page['h_mm'], 2) ?>mm;
      margin: 3mm;
    }

    body {
      font-family: "Courier New", Courier, monospace;
      font-size: 13pt;
      color: #000;
    }

    .page { page-break-inside: avoid; }
    .line { line-height: 2; white-space: nowrap; overflow: hidden; text-overflow: clip; width: 100%; page-break-inside: avoid; }

    .label {
      font-weight: 700;
    }

    .sp {
      display: inline-block;
      width: 4mm;
    }

    .title {
      text-align: center;
      font-weight: 700;
      text-decoration: underline;
      font-size: 18pt;
      margin-bottom: 3mm;
    }
  </style>
</head>

<body>
  <div class="page">
    <div class="title">Late Slip</div>
    <div class="line"><span class="label">Al Rahma School at ISGL <?= $schoolYear ?></span></div>
    <div class="line"><span class="label">Student:</span> <?= $studentName ?></div>
    <div class="line"><span class="label">Date:</span> <?= $date ?></div>
    <div class="line"><span class="label">Time In:</span> <?= $timeIn ?></div>
    <div class="line"><span class="label">Grade:</span> <?= $grade ?></div>
    <div class="line"><span class="label">Reason:</span> <?= $reason ?></div>
    <div class="line"><span class="label">Admin:</span> <?= $adminName ?></div>
  </div>
</body>

</html>
