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

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

<?php
$statusPill = static function (string $status): string {
    $map = ['captured' => 'active', 'voided' => 'terminated'];
    $class = $map[$status] ?? 'inactive';

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

$isCaptured = $signature['status'] === 'captured';
?>

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

<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-signature"></i>
                <h2 class="enterprise-settings-card__title">Signature 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($signature['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Signer</dt>
                <dd class="col-sm-8"><?= esc($signature['signer_name']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($signature['signer_title'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($signature['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Company</dt>
                <dd class="col-sm-8"><?= esc($signature['company_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Service Visit</dt>
                <dd class="col-sm-8"><?= esc($signature['visit_number'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Signed At</dt>
                <dd class="col-sm-8"><?= esc($signature['signed_at'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($signature['signature_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 ($isCaptured): ?>
                    <a href="<?= base_url('operations/field-service/customer-signatures/' . $signature['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/customer-signatures/' . $signature['id'] . '/void') ?>" method="post"
                          onsubmit="return confirm('Void this signature?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-ban me-1"></i> Void
                        </button>
                    </form>
                <?php endif; ?>
                <form action="<?= base_url('operations/field-service/customer-signatures/' . $signature['id'] . '/delete') ?>" method="post"
                      onsubmit="return confirm('Delete this signature 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>
                <a href="<?= base_url('operations/field-service/customer-signatures') ?>" class="btn btn-outline-secondary btn-sm">Back to Signatures</a>
            </div>
        </article>
    </div>
</div>

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