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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'assigned'    => 'default',
        'accepted'    => 'on_leave',
        'in_progress' => 'active',
        'done'        => '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>';
};

$isOpen = in_array($job['status'], ['assigned', 'accepted', 'in_progress'], true);
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $job['number'],
    'workspaceTitle'        => esc($job['number']),
    'workspaceDesc'         => esc($job['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-user-gear"></i>
                <h2 class="enterprise-settings-card__title">Job 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($job['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($job['title']) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($job['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Priority</dt>
                <dd class="col-sm-8"><?= $priorityPill($job['priority']) ?></dd>
                <dt class="col-sm-4 text-muted">Technician</dt>
                <dd class="col-sm-8"><?= esc($job['employee_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Role</dt>
                <dd class="col-sm-8"><?= esc($job['role'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Service Visit</dt>
                <dd class="col-sm-8"><?= esc($job['visit_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Due</dt>
                <dd class="col-sm-8"><?= esc($job['due_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Assigned</dt>
                <dd class="col-sm-8"><?= esc($job['assigned_at'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($job['notes'] ?: '—')) ?></dd>
                <?php if ($job['started_at']): ?>
                    <dt class="col-sm-4 text-muted">Started</dt>
                    <dd class="col-sm-8"><?= esc($job['started_at']) ?></dd>
                <?php endif; ?>
                <?php if ($job['status'] === 'done'): ?>
                    <dt class="col-sm-4 text-muted">Completed</dt>
                    <dd class="col-sm-8"><?= esc($job['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/field-service/technician-jobs/' . $job['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit
                    </a>
                    <?php if (in_array($job['status'], ['assigned', 'accepted'], true)): ?>
                        <form action="<?= base_url('operations/field-service/technician-jobs/' . $job['id'] . '/start') ?>" method="post">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                                <i class="fa-solid fa-play me-1"></i> Start
                            </button>
                        </form>
                    <?php endif; ?>
                    <form action="<?= base_url('operations/field-service/technician-jobs/' . $job['id'] . '/complete') ?>" method="post"
                          onsubmit="return confirm('Mark this job as done?');">
                        <?= csrf_field() ?>
                        <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/field-service/technician-jobs/' . $job['id'] . '/cancel') ?>" method="post"
                          onsubmit="return confirm('Cancel this job?');">
                        <?= 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($job['status'], ['assigned', 'cancelled'], true)): ?>
                    <form action="<?= base_url('operations/field-service/technician-jobs/' . $job['id'] . '/delete') ?>" method="post"
                          onsubmit="return confirm('Delete this job 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/technician-jobs') ?>" class="btn btn-outline-secondary btn-sm">Back to Jobs</a>
            </div>
        </article>
    </div>
</div>

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