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

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

<?php
$formatDate = static fn (?string $d): string => $d ? date('M j, Y', strtotime($d)) : '—';
$expiryBadge = static function (?string $expiryDate) use ($expiry, $today): string {
    $status = $expiry->expiryStatus($expiryDate, $today);
    $map = [
        'expired'  => ['terminated', 'Expired'],
        'critical' => ['on_leave', '≤ 7 days'],
        'warning'  => ['on_leave', '≤ 30 days'],
        'ok'       => ['active', 'Valid'],
        'none'     => ['inactive', 'No expiry'],
    ];
    [$class, $label] = $map[$status] ?? ['inactive', '—'];
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc($label) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent' => 'Expiry Tracker',
    'workspaceTitle'        => 'Document Expiry Tracker',
    'workspaceDesc'         => 'Monitor expired and upcoming document renewals across the workforce.',
    'workspaceAction'       => [
        'href'  => base_url('documents/create'),
        'label' => 'Upload Document',
        'icon'  => 'fa-solid fa-file-arrow-up',
    ],
]) ?>

<div class="enterprise-kpi-grid mb-4">
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Expired</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-circle-xmark"></i></span></div>
        <div class="enterprise-kpi-value text-danger"><?= $summary['expired'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('documents/expiry?filter=expired') ?>" class="enterprise-kpi-badge enterprise-kpi-badge--negative text-decoration-none">view expired</a></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Next 7 Days</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-triangle-exclamation"></i></span></div>
        <div class="enterprise-kpi-value text-warning"><?= $summary['expiring_7'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('documents/expiry?filter=7') ?>" class="enterprise-kpi-badge enterprise-kpi-badge--positive text-decoration-none">critical</a></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Next 30 Days</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-clock"></i></span></div>
        <div class="enterprise-kpi-value"><?= $summary['expiring_30'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('documents/expiry?filter=30') ?>" class="enterprise-kpi-badge enterprise-kpi-badge--positive text-decoration-none">this month</a></div>
    </article>
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header"><span class="enterprise-kpi-label">Next 90 Days</span><span class="enterprise-kpi-icon"><i class="fa-solid fa-calendar"></i></span></div>
        <div class="enterprise-kpi-value"><?= $summary['expiring_90'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('documents/expiry?filter=90') ?>" class="enterprise-kpi-badge enterprise-kpi-badge--positive text-decoration-none">quarter</a></div>
    </article>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Expiry List</h2>
        <p class="enterprise-data-panel__subtitle">
            <?php
            $filterLabels = ['expired' => 'Expired documents', '7' => 'Expiring within 7 days', '30' => 'Expiring within 30 days', '90' => 'Expiring within 90 days'];
            echo esc($filterLabels[$filter] ?? 'Expiring within 30 days');
            ?>
        </p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('documents/expiry') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Filter</label>
                    <select name="filter" class="form-select form-select-sm">
                        <?php foreach (['expired' => 'Expired', '7' => '7 days', '30' => '30 days', '90' => '90 days'] as $val => $label): ?>
                            <option value="<?= $val ?>" <?= $filter === $val ? 'selected' : '' ?>><?= $label ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" value="<?= esc($filters['search'] ?? '') ?>" placeholder="Employee or document no.">
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply</button>
                    <a href="<?= base_url('documents') ?>" class="btn btn-sm btn-outline-secondary">All Documents</a>
                </div>
            </div>
        </form>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr><th>Employee</th><th>Document</th><th>Number</th><th>Expiry Date</th><th>Days</th><th>Status</th><th class="text-end">Actions</th></tr>
                </thead>
                <tbody>
                    <?php foreach ($documents as $doc): ?>
                        <?php $days = $expiry->daysUntilExpiry($doc['expiry_date'], $today); ?>
                        <tr>
                            <td>
                                <span class="enterprise-data-table__primary"><?= esc($doc['full_name']) ?></span>
                                <br><small class="text-muted"><?= esc($doc['employee_number']) ?></small>
                            </td>
                            <td><?= esc($doc['type_name']) ?></td>
                            <td><span class="enterprise-data-table__mono"><?= esc($doc['document_number'] ?: '—') ?></span></td>
                            <td><?= $formatDate($doc['expiry_date']) ?></td>
                            <td><?= $days !== null ? ($days < 0 ? abs($days) . ' days ago' : $days . ' days') : '—' ?></td>
                            <td><?= $expiryBadge($doc['expiry_date']) ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('documents/' . $doc['id']) ?>" class="btn btn-sm btn-outline-secondary me-1">View</a>
                                <a href="<?= base_url('documents/' . $doc['id'] . '/edit') ?>" class="btn btn-sm btn-primary">Renew</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($documents) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No documents match this expiry filter.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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