<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$statusPill = static function (string $status): string {
    $map = [
        'draft'     => 'inactive',
        'submitted' => 'on_leave',
        'approved'  => 'active',
        'rejected'  => 'terminated',
    ];
    $class = $map[$status] ?? 'inactive';

    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">'
        . esc(ucwords(str_replace('_', ' ', $status))) . '</span>';
};

$resultPill = static function (string $result): string {
    $map = ['pass' => 'active', 'fail' => 'terminated', 'conditional' => 'on_leave', 'pending' => 'inactive'];
    $class = $map[$result] ?? 'default';

    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">'
        . esc(ucfirst($result)) . '</span>';
};

$canEdit = in_array($report['status'], ['draft', 'rejected'], true);
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $report['number'],
    'workspaceTitle'        => esc($report['number']),
    'workspaceDesc'         => esc($report['title']),
]) ?>

<div class="row g-4">
    <div class="col-lg-8">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-file-circle-check"></i>
                <h2 class="enterprise-settings-card__title">Report Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Number</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($report['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($report['title']) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($report['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Result</dt>
                <dd class="col-sm-8"><?= $resultPill($report['result']) ?></dd>
                <dt class="col-sm-4 text-muted">Inspector</dt>
                <dd class="col-sm-8"><?= esc($report['inspector_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Service Visit</dt>
                <dd class="col-sm-8"><?= esc($report['visit_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Work Order</dt>
                <dd class="col-sm-8"><?= esc($report['work_order_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Inspected At</dt>
                <dd class="col-sm-8"><?= esc($report['inspected_at'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Findings</dt>
                <dd class="col-sm-8"><?= nl2br(esc($report['findings'] ?: '—')) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($report['notes'] ?: '—')) ?></dd>
            </dl>
        </article>
    </div>

    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-bolt"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <div class="d-grid gap-2">
                <?php if ($canEdit): ?>
                    <a href="<?= base_url('operations/field-service/inspection-reports/' . $report['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit
                    </a>
                    <form action="<?= base_url('operations/field-service/inspection-reports/' . $report['id'] . '/submit') ?>" method="post"
                          onsubmit="return confirm('Submit this report for approval?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-paper-plane me-1"></i> Submit
                        </button>
                    </form>
                <?php endif; ?>
                <?php if ($report['status'] === 'submitted'): ?>
                    <form action="<?= base_url('operations/field-service/inspection-reports/' . $report['id'] . '/approve') ?>" method="post"
                          onsubmit="return confirm('Approve this report?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-check me-1"></i> Approve
                        </button>
                    </form>
                    <form action="<?= base_url('operations/field-service/inspection-reports/' . $report['id'] . '/reject') ?>" method="post"
                          onsubmit="return confirm('Reject this report?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-xmark me-1"></i> Reject
                        </button>
                    </form>
                <?php endif; ?>
                <?php if ($canEdit): ?>
                    <form action="<?= base_url('operations/field-service/inspection-reports/' . $report['id'] . '/delete') ?>" method="post"
                          onsubmit="return confirm('Delete this report permanently?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                            <i class="fa-solid fa-trash me-1"></i> Delete
                        </button>
                    </form>
                <?php endif; ?>
                <a href="<?= base_url('operations/field-service/inspection-reports') ?>" class="btn btn-outline-secondary btn-sm">Back to Reports</a>
            </div>
        </article>
    </div>
</div>

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