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

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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => 'Priorities',
    'workspaceTitle'        => 'Priorities',
    'workspaceDesc'         => 'Open tasks ordered by urgency — urgent and high first.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Priority Queue</h2>
        <p class="enterprise-data-panel__subtitle">Focus on the highest-priority open work.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('operations/tasks/priorities') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Number, title"
                           value="<?= esc($filters['search'] ?? '') ?>">
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Priority</th>
                        <th>Number</th>
                        <th>Title</th>
                        <th>Status</th>
                        <th>Due</th>
                        <th>Assignee</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <tr>
                            <td><?= $priorityPill($e['priority']) ?></td>
                            <td><a href="<?= base_url('operations/tasks/' . $e['id']) ?>"><code><?= esc($e['number']) ?></code></a></td>
                            <td><span class="enterprise-data-table__primary"><?= esc($e['title']) ?></span></td>
                            <td><?= $statusPill($e['status']) ?></td>
                            <td><?= esc($e['due_date'] ?: '—') ?></td>
                            <td><?= esc($e['assignee_name'] ?? '—') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($entries) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No open tasks.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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