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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'planned'     => 'default',
        'in_progress' => 'active',
        'completed'   => 'active',
        'cancelled'   => 'terminated',
    ];
    $class = $map[$status] ?? 'inactive';

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

$priorityPill = static function (string $priority): string {
    $map = ['low' => 'inactive', 'normal' => 'default', 'high' => 'on_leave', 'urgent' => 'terminated'];
    $class = $map[$priority] ?? 'default';

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

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

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

$isOpen = in_array($inspection['status'], ['planned', 'in_progress'], true);
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $inspection['number'],
    'workspaceTitle'        => esc($inspection['number']),
    'workspaceDesc'         => esc($inspection['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-magnifying-glass"></i>
                <h2 class="enterprise-settings-card__title">Inspection 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($inspection['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($inspection['title']) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($inspection['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Priority</dt>
                <dd class="col-sm-8"><?= $priorityPill($inspection['priority']) ?></dd>
                <dt class="col-sm-4 text-muted">Result</dt>
                <dd class="col-sm-8"><?= $resultPill($inspection['overall_result']) ?></dd>
                <dt class="col-sm-4 text-muted">Scheduled</dt>
                <dd class="col-sm-8"><?= esc($inspection['scheduled_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Location</dt>
                <dd class="col-sm-8"><?= esc($inspection['location'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Score</dt>
                <dd class="col-sm-8"><?= esc($inspection['score'] !== null && $inspection['score'] !== '' ? $inspection['score'] : '—') ?></dd>
                <dt class="col-sm-4 text-muted">Inspector</dt>
                <dd class="col-sm-8"><?= esc($inspection['inspector_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Company</dt>
                <dd class="col-sm-8"><?= esc($inspection['company_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Work Order</dt>
                <dd class="col-sm-8"><?= esc($inspection['work_order_number'] ?? '—') ?></dd>
                <?php if (! empty($inspection['service_visit_number'])): ?>
                    <dt class="col-sm-4 text-muted">Service Visit</dt>
                    <dd class="col-sm-8"><?= esc($inspection['service_visit_number']) ?></dd>
                <?php elseif (! empty($inspection['service_visit_id'])): ?>
                    <dt class="col-sm-4 text-muted">Service Visit ID</dt>
                    <dd class="col-sm-8"><?= (int) $inspection['service_visit_id'] ?></dd>
                <?php endif; ?>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= nl2br(esc($inspection['description'] ?: '—')) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($inspection['notes'] ?: '—')) ?></dd>
                <?php if ($inspection['status'] === 'completed'): ?>
                    <dt class="col-sm-4 text-muted">Completed</dt>
                    <dd class="col-sm-8"><?= esc($inspection['completed_at'] ?: '—') ?></dd>
                <?php endif; ?>
            </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 ($isOpen): ?>
                    <a href="<?= base_url('operations/quality-control/inspections/' . $inspection['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/quality-control/inspections/' . $inspection['id'] . '/complete') ?>" method="post"
                          onsubmit="return confirm('Mark this inspection as completed?');">
                        <?= csrf_field() ?>
                        <label class="enterprise-field-label">Result on complete</label>
                        <select name="overall_result" class="form-select form-select-sm mb-2">
                            <?php foreach (['pass', 'fail', 'na'] as $r): ?>
                                <option value="<?= $r ?>" <?= ($inspection['overall_result'] ?? '') === $r ? 'selected' : '' ?>>
                                    <?= esc(strtoupper($r)) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-check me-1"></i> Complete
                        </button>
                    </form>
                    <form action="<?= base_url('operations/quality-control/inspections/' . $inspection['id'] . '/cancel') ?>" method="post"
                          onsubmit="return confirm('Cancel this inspection?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-ban me-1"></i> Cancel
                        </button>
                    </form>
                <?php endif; ?>
                <?php if (in_array($inspection['status'], ['planned', 'cancelled'], true)): ?>
                    <form action="<?= base_url('operations/quality-control/inspections/' . $inspection['id'] . '/delete') ?>" method="post"
                          onsubmit="return confirm('Delete this inspection 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/quality-control/inspections') ?>" class="btn btn-outline-secondary btn-sm">Back to Inspections</a>
            </div>
        </article>
    </div>
</div>

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