<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once __DIR__ . '/../../config/database.php';
$conn = getConnection();

if (!function_exists('e')) {
    function e($value): string { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); }
}
if (!function_exists('coaching_employee_name_from_db')) {
    function coaching_employee_name_from_db(mysqli $conn, int $id): string {
        $sql = "SELECT COALESCE(NULLIF(full_name,''), NULLIF(nama,''), CONCAT('Staff #', id)) AS full_name FROM employees WHERE id = ? LIMIT 1";
        if ($stmt = $conn->prepare($sql)) {
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $res = $stmt->get_result();
            $row = $res ? $res->fetch_assoc() : null;
            $stmt->close();
            return (string)($row['full_name'] ?? '-');
        }
        return '-';
    }
}

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) exit('ID sesi tidak valid.');

$sql = "SELECT cs.*, csa.*, cc.competency_name, COALESCE(u.name,'-') AS unit_name
        FROM coaching_sessions cs
        LEFT JOIN coaching_staff_assessments csa ON csa.session_id = cs.id
        LEFT JOIN coaching_competencies cc ON cc.id = cs.competency_id
        LEFT JOIN coaching_assignments ca ON ca.id = cs.assignment_id
        LEFT JOIN units u ON u.id = ca.unit_id
        WHERE cs.id = ?
        LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$res = $stmt->get_result();
$row = $res ? $res->fetch_assoc() : null;
$stmt->close();
if (!$row) exit('Data sesi tidak ditemukan.');
$coachName = coaching_employee_name_from_db($conn, (int)$row['coach_employee_id']);
$staffName = coaching_employee_name_from_db($conn, (int)$row['staff_employee_id']);
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<title>Print Coaching</title>
<style>
body{font-family:Arial,sans-serif;color:#111;padding:28px}
h1,h2,h3{margin:0 0 12px}
.box{border:1px solid #cbd5e1;border-radius:12px;padding:14px;margin-bottom:14px}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:14px}
.label{font-weight:bold;margin-bottom:6px}
.small{color:#475569;font-size:12px}
@media print {.no-print{display:none}}
</style>
</head>
<body>
<div class="no-print" style="margin-bottom:16px"><button onclick="window.print()">Print / Save PDF</button></div>
<h1>FORM COACHING STAF BARU</h1>
<div class="small">RS BaliMed Denpasar</div>
<div class="grid" style="margin-top:18px">
  <div class="box"><div class="label">Tanggal</div><?= e($row['coaching_date']) ?></div>
  <div class="box"><div class="label">Unit</div><?= e($row['unit_name']) ?></div>
  <div class="box"><div class="label">Coach</div><?= e($coachName) ?></div>
  <div class="box"><div class="label">Staff Baru</div><?= e($staffName) ?></div>
  <div class="box"><div class="label">Topik</div><?= e($row['topic_title']) ?></div>
  <div class="box"><div class="label">Kompetensi</div><?= e($row['competency_name']) ?></div>
</div>
<div class="box"><div class="label">Pre-Briefing</div><?= nl2br(e($row['pre_briefing_notes'])) ?></div>
<div class="box"><div class="label">Observation</div><?= nl2br(e($row['observation_notes'])) ?></div>
<div class="box"><div class="label">Reflective Feedback</div><?= nl2br(e($row['reflective_feedback_notes'])) ?></div>
<div class="box"><div class="label">Action Plan</div><?= nl2br(e($row['action_plan_notes'])) ?></div>
<div class="grid">
  <div class="box"><div class="label">Skor Klinis</div><?= e($row['score_clinical']) ?></div>
  <div class="box"><div class="label">Skor Soft Skill</div><?= e($row['score_softskill']) ?></div>
  <div class="box"><div class="label">Skor Dokumentasi</div><?= e($row['score_documentation']) ?></div>
  <div class="box"><div class="label">Weighted Score</div><?= e($row['weighted_score']) ?></div>
</div>
<div class="box"><div class="label">Status Kompetensi</div><?= e(str_replace('_',' ', (string)$row['competency_status'])) ?></div>
<div class="box"><div class="label">Catatan Observasi</div><?= nl2br(e($row['observation_note'])) ?></div>
<div class="box"><div class="label">Kekuatan</div><?= nl2br(e($row['strengths_note'])) ?></div>
<div class="box"><div class="label">Area Perbaikan</div><?= nl2br(e($row['improvement_note'])) ?></div>
</body>
</html>
