<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * This template is rendered inside TCPDF context.
 * Variables usually available:
 * - $pdf (TCPDF instance)
 * - $applicationData (your passed data)
 */

// $applicationData structure from your debug:
// [ 'decl' => [...], 'items' => [...], 'files' => [...] ]
$header = $decl['decl']  ?? [];
$items  = $decl['items'] ?? [];
$files  = $decl['files'] ?? [];

// Safe values
$staffName   = $header['staff_name'] ?? '-';
$staffCode   = $header['staff_code'] ?? ($header['staff_id'] ?? '-');
$position    = $header['position'] ?? '-';
$department  = $header['department'] ?? '-';
$signature   = $header['employee_signature'] ?? '-';
$declDate    = $header['employee_date'] ?? '-';
$createdAt   = $header['created_at'] ?? '-';
$updatedAt   = $header['updated_at'] ?? '-';
$declId      = $header['id'] ?? '';

$css = <<<CSS
<style>
  body { font-family: dejavusans, sans-serif; font-size: 10.5pt; color: #0f172a; }
  .wrap { border-top: 4px solid #00829B; padding-top: 10px; }
  .title { font-size: 16pt; font-weight: bold; color: #00829B; margin: 0 0 6px; }
  .muted { color: #6b7280; font-size: 9pt; margin: 0 0 10px; }
  .meta { width: 100%; border-collapse: collapse; margin-top: 8px; }
  .meta td { padding: 4px 6px; vertical-align: top; }
  .label { width: 22%; color: #374151; font-weight: bold; }
  .value { width: 28%; }
  hr { border: 0; border-top: 1px solid #e5e7eb; margin: 10px 0; }
  .section { font-size: 11.5pt; font-weight: bold; color: #0f4a56; margin: 8px 0 6px; }
  table.items { width: 100%; border-collapse: collapse; }
  table.items th, table.items td { border: 1px solid #e5e7eb; padding: 6px; font-size: 9.5pt; }
  table.items th { background: #eaf7f8; color: #004f5c; font-weight: bold; }
  .right { text-align: right; }
  .small { font-size: 9pt; color: #374151; }
  ul.attach { margin: 6px 0 0; padding-left: 16px; }
  ul.attach li { margin: 2px 0; }
</style>
CSS;

// Build rows HTML
$rowsHtml = '';
if (!empty($items)) {
    foreach ($items as $i => $it) {
        $no      = $i + 1;
        $d       = $it['date_received'] ?? '';
        $desc    = $it['description'] ?? '';
        $val     = $it['estimated_value'] ?? '';
        $from    = $it['gift_from'] ?? '';
        $rel     = $it['relationship'] ?? '';
        $purpose = $it['purpose'] ?? '';
        $action  = $it['action_taken'] ?? '';

        // Escape (TCPDF understands basic HTML entities)
        $rowsHtml .= '<tr>'
            . '<td style="width:4%;">' . (int)$no . '</td>'
            . '<td style="width:10%;">' . ($d) . '</td>'
            . '<td style="width:22%;">' . ($desc) . '</td>'
            . '<td style="width:10%;" class="right">' . ($val) . '</td>'
            . '<td style="width:14%;">' . ($from) . '</td>'
            . '<td style="width:12%;">' . ($rel) . '</td>'
            . '<td style="width:14%;">' . ($purpose) . '</td>'
            . '<td style="width:14%;">' . ($action) . '</td>'
            . '</tr>';
    }
} else {
    $rowsHtml = '<tr><td colspan="8" class="small">No gifts/entertainment items declared.</td></tr>';
}

// Attachments list
$attachHtml = '';
if (!empty($files)) {
    $attachHtml .= '<div class="section">Attachments</div><ul class="attach">';
    foreach ($files as $f) {
        $nm = $f['original_name'] ?: ($f['file_name'] ?? '');
        $attachHtml .= '<li>' . ($nm) . '</li>';
    }
    $attachHtml .= '</ul>';
}

$html = <<<HTML
$css
<div class="wrap">
  <div class="title">INCEIF Gift Disclosure Form</div>
  <div class="muted">Declaration form for gifts, hospitality or entertainment. (ID: #{$declId})</div>

  <div class="section">Part A — Employee Information</div>
  <table class="meta">
    <tr>
      <td class="label">Name</td><td class="value">$staffName</td>
      <td class="label">Staff ID</td><td class="value">$staffCode</td>
    </tr>
    <tr>
      <td class="label">Position</td><td class="value">$position</td>
      <td class="label">Department</td><td class="value">$department</td>
    </tr>
  </table>

  <hr />

  <div class="section">Part B — Gifts / Entertainment</div>
  <table class="items">
    <thead>
      <tr>
        <th style="width:4%;">No</th>
        <th style="width:10%;">Date</th>
        <th style="width:22%;">Description</th>
        <th style="width:10%;">Value (RM)</th>
        <th style="width:14%;">From</th>
        <th style="width:12%;">Relationship</th>
        <th style="width:14%;">Purpose</th>
        <th style="width:14%;">Action</th>
      </tr>
    </thead>
    <tbody>
      $rowsHtml
    </tbody>
  </table>

  <hr />

  <div class="section">Part C — Declaration</div>
  <table class="meta">
    <tr>
      <td class="label">Signature</td><td class="value">$signature</td>
      <td class="label">Date</td><td class="value">$declDate</td>
    </tr>
    <tr>
      <td class="label">Created</td><td class="value">$createdAt</td>
      <td class="label">Updated</td><td class="value">$updatedAt</td>
    </tr>
  </table>

  $attachHtml
</div>
HTML;

// IMPORTANT: do not echo/print anything.
// Only write to TCPDF.
$pdf->writeHTML($html, true, false, true, false, '');
