<?php helper('letter'); ?>
<?= $this->extend('layouts/print') ?>
<?= $this->section('content') ?>

<style>
    @media print {
        .no-print { display: none !important; }
    }
</style>

<div class="no-print" style="padding:10px 0 16px; display:flex; gap:10px;">
    <button onclick="window.print()" class="btn-print">
        <i class="bi bi-printer"></i> Cetak
    </button>
    <button onclick="window.history.back()" class="btn-back">
        <i class="bi bi-arrow-left"></i> Kembali
    </button>
</div>

<div id="surat">
    <?= view('letter/_kop') ?>

    <!-- JUDUL SURAT TUGAS -->
    <div style="text-align: center; margin-bottom: 18px;">
        <div style="font-family: 'Bookman Old Style', Bookman, Georgia, serif; font-size: 12pt; font-weight: bold; text-transform: uppercase;
                    text-decoration: underline; letter-spacing: 1px; margin: 0;">
            SURAT TUGAS
        </div>
        <div style="font-family: 'Bookman Old Style', Bookman, Georgia, serif; font-size: 12pt; margin: 2px 0 0;">
            Nomor: <?= esc($log['letter_number']) ?>
        </div>
    </div>

    <!-- ISI SURAT TUGAS -->
    <div style="font-family: 'Bookman Old Style', Bookman, Georgia, serif; font-size: 12pt; line-height: 2; text-align: justify;">

        <?php if (!empty($log['is_reference']) && (int)$log['is_reference'] === 1): ?>
        <p style="margin: 0 0 10px 0; text-indent: 40px; text-align: justify;">
            Menindaklanjuti Surat <?= esc($log['reference_officer']) ?> Nomor: <?= esc($log['reference_number']) ?> tanggal <?= format_tanggal_indonesia($log['reference_date']) ?> tentang "<?= esc($log['reference_theme']) ?>", Kepala <?= esc($schoolSettings['school_name'] ?? '') ?> dengan ini memberikan tugas kepada:
        </p>
        <?php else: ?>
        <p style="margin: 0 0 10px 0; text-indent: 40px; text-align: justify;">
            Yang bertanda tangan di bawah ini, Kepala <?= esc($schoolSettings['school_name'] ?? '') ?>, dengan ini menugaskan kepada:
        </p>
        <?php endif; ?>

        <!-- DATA PELAKSANA -->
        <table style="width: 100%; margin-bottom: 14px; line-height: 1.8;">
            <tr>
                <td style="width: 185px; vertical-align: top;">Nama</td>
                <td style="width: 10px;">:</td>
                <td style="font-weight: bold;"><?= esc($log['officer_name'] ?? '-') ?></td>
            </tr>
            <?php if (!empty($log['officer_nip'])): ?>
            <tr>
                <td><?= esc($log['teacher_id_label'] ?? 'NIP') ?></td>
                <td>:</td>
                <td><?= esc($log['officer_nip']) ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($log['officer_rank'])): ?>
            <tr>
                <td>Pangkat / Golongan</td>
                <td>:</td>
                <td><?= esc($log['officer_rank']) ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($log['officer_position'])): ?>
            <tr>
                <td>Jabatan</td>
                <td>:</td>
                <td><?= esc($log['officer_position']) ?></td>
            </tr>
            <?php endif; ?>
            <tr>
                <td>Unit Kerja</td>
                <td>:</td>
                <td><?= esc($schoolSettings['school_name'] ?? '') ?></td>
            </tr>
        </table>

        <!-- URAIAN PENUGASAN -->
        <p style="margin: 0 0 10px 0;">
            Untuk melaksanakan tugas:
        </p>

        <table style="width: 100%; margin-bottom: 14px; line-height: 1.8;">
            <tr>
                <td style="width: 185px; vertical-align: top;">Uraian Tugas</td>
                <td style="width: 10px;">:</td>
                <td><?= esc($log['task_description'] ?? '-') ?></td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Tempat</td>
                <td>:</td>
                <td><?= esc($log['origin'] ?? '-') ?></td>
            </tr>
            <tr>
                <td>Waktu Pelaksanaan</td>
                <td>:</td>
                <td>
                    <?php
                    $start = $log['date_start'] ?? null;
                    $end   = $log['date_end'] ?? null;
                    if ($start && $end && $start !== $end) {
                        echo format_tanggal_indonesia($start) . ' s.d. ' . format_tanggal_indonesia($end);
                    } else {
                        echo format_tanggal_indonesia($start ?? $log['letter_date']);
                    }
                    ?>
                </td>
            </tr>
        </table>

        <p style="margin: 0 0 6px 0; text-indent: 40px; text-align: justify;">
            Demikian surat tugas ini dibuat untuk dilaksanakan dengan sebaik-baiknya.
        </p>
    </div>

    <?= view('letter/_ttd') ?>
</div>

<?= $this->endSection() ?>