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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'open'        => '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>';
};

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $task['number'],
    'workspaceTitle'        => esc($task['number']),
    'workspaceDesc'         => esc($task['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-list-check"></i>
                <h2 class="enterprise-settings-card__title">Task 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($task['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($task['title']) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($task['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Priority</dt>
                <dd class="col-sm-8"><?= $priorityPill($task['priority']) ?></dd>
                <dt class="col-sm-4 text-muted">Due</dt>
                <dd class="col-sm-8"><?= esc($task['due_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Assignee</dt>
                <dd class="col-sm-8"><?= esc($task['assignee_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= nl2br(esc($task['description'] ?: '—')) ?></dd>
                <?php if ($task['status'] === 'completed'): ?>
                    <dt class="col-sm-4 text-muted">Completed</dt>
                    <dd class="col-sm-8"><?= esc($task['completed_at'] ?: '—') ?></dd>
                <?php endif; ?>
            </dl>
        </article>

        <div class="enterprise-data-panel mt-4">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Checklist</h2>
                <?php if ($isOpen): ?>
                    <a href="<?= base_url('operations/tasks/checklists?task_id=' . $task['id']) ?>" class="btn btn-sm btn-outline-primary">Manage</a>
                <?php endif; ?>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Item</th>
                                <th>Done</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($checklist as $item): ?>
                                <tr>
                                    <td><?= esc($item['title']) ?></td>
                                    <td><?= ! empty($item['is_done']) ? 'Yes' : 'No' ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($checklist) === 0): ?>
                                <tr><td colspan="2" class="text-center text-muted py-4">No checklist items.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </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/tasks/' . $task['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit
                    </a>
                    <a href="<?= base_url('operations/tasks/checklists?task_id=' . $task['id']) ?>" class="btn btn-outline-secondary btn-sm">
                        <i class="fa-solid fa-square-check me-1"></i> Checklist
                    </a>
                    <form action="<?= base_url('operations/tasks/' . $task['id'] . '/complete') ?>" method="post"
                          onsubmit="return confirm('Mark this task 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/tasks/' . $task['id'] . '/cancel') ?>" method="post"
                          onsubmit="return confirm('Cancel this task?');">
                        <?= 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($task['status'], ['open', 'cancelled'], true)): ?>
                    <form action="<?= base_url('operations/tasks/' . $task['id'] . '/delete') ?>" method="post"
                          onsubmit="return confirm('Delete this task 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/tasks') ?>" class="btn btn-outline-secondary btn-sm">Back to Tasks</a>
            </div>
        </article>
    </div>
</div>

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