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

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

<?php
$formatDate = static fn (?string $d): string => $d ? date('M j, Y', strtotime($d)) : '—';
$daysLeft = static function (?string $endDate, string $today): ?int {
    if (! $endDate) {
        return null;
    }
    return (int) floor((strtotime($endDate) - strtotime($today)) / 86400);
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => 'Renewals',
    'workspaceTitle'        => 'Contract Renewals',
    'workspaceDesc'         => 'Active contracts approaching end date that may need renewal.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Renewal Candidates</h2>
        <p class="enterprise-data-panel__subtitle">Active contracts ending within the selected window.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('contracts/renewals') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Within</label>
                    <select name="within" class="form-select form-select-sm">
                        <?php foreach ([30 => '30 days', 60 => '60 days', 90 => '90 days', 180 => '180 days'] as $val => $label): ?>
                            <option value="<?= $val ?>" <?= (int) $filters['within'] === $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" placeholder="Number, title, customer"
                           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>Number</th>
                        <th>Title</th>
                        <th>Customer</th>
                        <th>Type</th>
                        <th>End Date</th>
                        <th>Days Left</th>
                        <th>Value</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($contracts as $row): ?>
                        <?php $left = $daysLeft($row['end_date'] ?? null, $today); ?>
                        <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>
                                <?php if ($left === null): ?>
                                    —
                                <?php elseif ($left <= 7): ?>
                                    <span class="enterprise-status-pill enterprise-status-pill--on_leave"><?= $left ?> days</span>
                                <?php elseif ($left <= 30): ?>
                                    <span class="enterprise-status-pill enterprise-status-pill--inactive"><?= $left ?> days</span>
                                <?php else: ?>
                                    <?= $left ?> days
                                <?php endif; ?>
                            </td>
                            <td><?= number_format((float) $row['value'], 2) ?></td>
                            <td class="text-end enterprise-row-actions">
                                <a href="<?= base_url('contracts/contracts/' . $row['id']) ?>" class="btn btn-sm btn-outline-primary">Review</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($contracts) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No contracts due for renewal in this window.</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() ?>
