<?= $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 $endDate, string $today): string {
    if (! $endDate) {
        return '<span class="enterprise-status-pill enterprise-status-pill--inactive">No end date</span>';
    }
    $days = (int) floor((strtotime($endDate) - strtotime($today)) / 86400);
    if ($days < 0) {
        return '<span class="enterprise-status-pill enterprise-status-pill--terminated">Expired</span>';
    }
    if ($days <= 7) {
        return '<span class="enterprise-status-pill enterprise-status-pill--on_leave">≤ 7 days</span>';
    }
    if ($days <= 30) {
        return '<span class="enterprise-status-pill enterprise-status-pill--on_leave">≤ 30 days</span>';
    }
    return '<span class="enterprise-status-pill enterprise-status-pill--inactive">≤ 90 days</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => 'Expiry Alerts',
    'workspaceTitle'        => 'Contract Expiry Alerts',
    'workspaceDesc'         => 'Monitor expired and upcoming contract end dates.',
]) ?>

<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"><?= (int) $summary['expired'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('contracts/expiry-alerts?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"><?= (int) $summary['expiring_7'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('contracts/expiry-alerts?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"><?= (int) $summary['expiring_30'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('contracts/expiry-alerts?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"><?= (int) $summary['expiring_90'] ?></div>
        <div class="enterprise-kpi-footer"><a href="<?= base_url('contracts/expiry-alerts?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 contracts', '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('contracts/expiry-alerts') ?>" 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 === (string) $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="Number, title, customer">
                </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>Number</th>
                        <th>Title</th>
                        <th>Customer</th>
                        <th>Type</th>
                        <th>End Date</th>
                        <th>Alert</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($contracts as $row): ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('contracts/contracts/' . $row['id']) ?>" class="enterprise-data-table__primary text-decoration-none">
                                    <code><?= esc($row['number']) ?></code>
                                </a>
                            </td>
                            <td><?= esc($row['title']) ?></td>
                            <td><?= esc($row['company_name'] ?? '—') ?></td>
                            <td><?= esc(ucfirst($row['contract_type'])) ?></td>
                            <td><?= esc($formatDate($row['end_date'] ?? null)) ?></td>
                            <td><?= $expiryBadge($row['end_date'] ?? null, $today) ?></td>
                            <td>
                                <span class="enterprise-status-pill enterprise-status-pill--<?= $row['status'] === 'active' ? 'active' : 'inactive' ?>">
                                    <?= esc(ucfirst($row['status'])) ?>
                                </span>
                            </td>
                            <td class="text-end enterprise-row-actions">
                                <a href="<?= base_url('contracts/contracts/' . $row['id']) ?>" class="btn btn-sm btn-outline-primary">View</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($contracts) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No contracts match this expiry filter.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if ($pager): ?>
            <div class="enterprise-data-panel__footer"><?= $pager->links() ?></div>
        <?php endif; ?>
    </div>
</div>

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