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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'draft'     => 'inactive',
        'scheduled' => 'default',
        'en_route'  => 'on_leave',
        'on_site'   => '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>';
};

$isOpen = in_array($visit['status'], ['draft', 'scheduled', 'en_route', 'on_site'], true);
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $visit['number'],
    'workspaceTitle'        => esc($visit['number']),
    'workspaceDesc'         => esc($visit['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-map-location-dot"></i>
                <h2 class="enterprise-settings-card__title">Visit 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($visit['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($visit['title']) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($visit['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Priority</dt>
                <dd class="col-sm-8"><?= $priorityPill($visit['priority']) ?></dd>
                <dt class="col-sm-4 text-muted">Company</dt>
                <dd class="col-sm-8"><?= esc($visit['company_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Work Order</dt>
                <dd class="col-sm-8"><?= esc($visit['work_order_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Technician</dt>
                <dd class="col-sm-8"><?= esc($visit['technician_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Location</dt>
                <dd class="col-sm-8"><?= esc($visit['location'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Scheduled Date</dt>
                <dd class="col-sm-8"><?= esc($visit['scheduled_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Window</dt>
                <dd class="col-sm-8">
                    <?= esc($visit['scheduled_start'] ?: '—') ?>
                    →
                    <?= esc($visit['scheduled_end'] ?: '—') ?>
                </dd>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= nl2br(esc($visit['description'] ?: '—')) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($visit['notes'] ?: '—')) ?></dd>
                <?php if ($visit['arrived_at']): ?>
                    <dt class="col-sm-4 text-muted">Arrived</dt>
                    <dd class="col-sm-8"><?= esc($visit['arrived_at']) ?></dd>
                <?php endif; ?>
                <?php if ($visit['status'] === 'completed'): ?>
                    <dt class="col-sm-4 text-muted">Completed</dt>
                    <dd class="col-sm-8"><?= esc($visit['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/service-visits/' . $visit['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit
                    </a>
                    <?php if (in_array($visit['status'], ['draft', 'scheduled'], true)): ?>
                        <form action="<?= base_url('operations/field-service/service-visits/' . $visit['id'] . '/en-route') ?>" method="post">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                                <i class="fa-solid fa-truck me-1"></i> En Route
                            </button>
                        </form>
                    <?php endif; ?>
                    <?php if (in_array($visit['status'], ['scheduled', 'en_route'], true)): ?>
                        <form action="<?= base_url('operations/field-service/service-visits/' . $visit['id'] . '/on-site') ?>" method="post">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                                <i class="fa-solid fa-location-dot me-1"></i> On Site
                            </button>
                        </form>
                    <?php endif; ?>
                    <form action="<?= base_url('operations/field-service/service-visits/' . $visit['id'] . '/complete') ?>" method="post"
                          onsubmit="return confirm('Mark this visit as completed?');">
                        <?= 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/service-visits/' . $visit['id'] . '/cancel') ?>" method="post"
                          onsubmit="return confirm('Cancel this visit?');">
                        <?= 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($visit['status'], ['draft', 'cancelled'], true)): ?>
                    <form action="<?= base_url('operations/field-service/service-visits/' . $visit['id'] . '/delete') ?>" method="post"
                          onsubmit="return confirm('Delete this visit 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/service-visits') ?>" class="btn btn-outline-secondary btn-sm">Back to Visits</a>
            </div>
        </article>
    </div>
</div>

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