<?php
/** @var array $wo */
/** @var array $tenant */
/** @var array $tasks */
$companyName = \Core\Settings::get('company_name') ?: APP_NAME;
?>
<style>
    body { font-family: sans-serif; color: #1e2d3d; font-size: 10pt; }
    .brand { color: #0b2545; font-weight: 800; font-size: 16pt; }
    .sub { color: #6b7a8d; font-size: 9pt; text-transform: uppercase; letter-spacing: 1.5px; }
    h1 { font-size: 18pt; margin: 8mm 0 2mm; color: #0b2545; }
    h2 { font-size: 12pt; margin: 6mm 0 2mm; color: #1a4d8c; border-bottom: 1px solid #d1d5db; padding-bottom: 1mm; }
    table.def { width: 100%; border-collapse: collapse; margin: 2mm 0 4mm; }
    table.def td { padding: 1mm 2mm; vertical-align: top; }
    table.def td.k { color: #6b7a8d; width: 30%; text-transform: uppercase; font-size: 8pt; letter-spacing: .5px; }
    table.def td.v { color: #1e2d3d; font-weight: 600; }
    table.tasks { width: 100%; border-collapse: collapse; margin-top: 3mm; }
    table.tasks th, table.tasks td { border-bottom: 1px solid #e2e8f0; padding: 2mm; text-align: left; font-size: 9pt; }
    table.tasks th { background: #f7f9fc; color: #6b7a8d; text-transform: uppercase; font-size: 8pt; letter-spacing: .5px; }
    .cb { display: inline-block; width: 12px; height: 12px; border: 1.5px solid #1a4d8c; text-align: center; line-height: 10px; }
    .cb.done { background: #22c55e; border-color: #22c55e; color: #fff; }
    .sig-row { margin-top: 8mm; display: flex; gap: 15mm; }
    .sig { flex: 1; border-top: 1px solid #1e2d3d; padding-top: 2mm; font-size: 8pt; color: #6b7a8d; }
    .foot { margin-top: 12mm; color: #94a3b8; font-size: 7pt; text-align: center; }
</style>
<div>
    <div class="brand"><?= e($companyName) ?></div>
    <div class="sub"><?= e($tenant['name'] ?? '') ?> · Work Order</div>
</div>

<h1><?= e($wo['code'] ?? '#' . $wo['work_order_id']) ?> · <?= e($wo['title']) ?></h1>

<h2>Details</h2>
<table class="def">
    <tr><td class="k">Type</td>       <td class="v"><?= e(strtoupper($wo['wo_type'])) ?></td>
        <td class="k">Priority</td>    <td class="v"><?= e(strtoupper($wo['priority'])) ?></td></tr>
    <tr><td class="k">Status</td>     <td class="v"><?= e(strtoupper($wo['status'])) ?></td>
        <td class="k">Assigned to</td> <td class="v"><?= e($wo['assigned_username'] ?? 'unassigned') ?></td></tr>
    <tr><td class="k">Site</td>       <td class="v"><?= e($wo['site_name'] ?? '—') ?></td>
        <td class="k">Asset</td>       <td class="v"><?= e($wo['asset_name'] ?? '—') ?></td></tr>
    <tr><td class="k">Scheduled</td>  <td class="v"><?= e($wo['scheduled_at'] ?? '—') ?></td>
        <td class="k">Due</td>         <td class="v"><?= e($wo['due_at'] ?? '—') ?></td></tr>
    <tr><td class="k">Started</td>    <td class="v"><?= e($wo['started_at'] ?? '—') ?></td>
        <td class="k">Completed</td>   <td class="v"><?= e($wo['completed_at'] ?? '—') ?></td></tr>
    <tr><td class="k">Est. Hours</td> <td class="v"><?= e((string) ($wo['estimated_hours'] ?? '—')) ?></td>
        <td class="k">Actual Hours</td><td class="v"><?= e((string) ($wo['actual_hours'] ?? '—')) ?></td></tr>
</table>

<?php if (!empty($wo['description'])): ?>
    <h2>Description</h2>
    <p><?= nl2br(e($wo['description'])) ?></p>
<?php endif; ?>

<h2>Tasks (<?= count($tasks) ?>)</h2>
<?php if (empty($tasks)): ?>
    <p style="color:#6b7a8d;">No tasks defined.</p>
<?php else: ?>
    <table class="tasks">
        <thead>
            <tr>
                <th style="width:14px;">✓</th>
                <th>Task</th>
                <th style="width:24mm;">Response</th>
                <th style="width:30mm;">Remarks</th>
                <th style="width:30mm;">Remedy</th>
                <th style="width:25mm;">Done by</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($tasks as $t): ?>
                <tr>
                    <td><span class="cb <?= $t['is_done'] ? 'done' : '' ?>"><?= $t['is_done'] ? '✓' : '' ?></span></td>
                    <td>
                        <strong><?= e($t['title']) ?></strong>
                        <?php if (!empty($t['description'])): ?><br><span style="color:#6b7a8d;font-size:8pt;"><?= nl2br(e($t['description'])) ?></span><?php endif; ?>
                    </td>
                    <td><strong><?= e($t['response_value'] ?? '') ?></strong></td>
                    <td><?= e($t['result_note'] ?? '') ?></td>
                    <td><?= e($t['remedy_note'] ?? '') ?></td>
                    <td><?= e($t['done_username'] ?? '') ?><?php if (!empty($t['done_at'])): ?><br><small style="color:#6b7a8d;"><?= e($t['done_at']) ?></small><?php endif; ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

<?php if (!empty($wo['notes'])): ?>
    <h2>Notes</h2>
    <p><?= nl2br(e($wo['notes'])) ?></p>
<?php endif; ?>

<div class="sig-row">
    <div class="sig">Technician signature &nbsp; · &nbsp; Date</div>
    <div class="sig">Supervisor signature &nbsp; · &nbsp; Date</div>
</div>

<div class="foot">
    Generated <?= date('Y-m-d H:i') ?> · <?= e($companyName) ?>
</div>
