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

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

<?php
$formatDate = static fn (?string $d): string => $d ? date('M j, Y', strtotime($d)) : '—';
$statusPill = static function (string $status): string {
    $map = [
        'draft'      => 'inactive',
        'active'     => 'active',
        'expired'    => 'inactive',
        'terminated' => 'inactive',
        'cancelled'  => 'inactive',
    ];
    $class = $map[$status] ?? 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => 'Active Contracts',
    'workspaceTitle'        => 'Active Contracts',
    'workspaceDesc'         => 'Live product, service, and project contracts currently in force.',
    'workspaceAction'       => [
        'href'  => base_url('contracts/contracts/create'),
        'label' => 'New Contract',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Active Contracts</h2>
        <p class="enterprise-data-panel__subtitle">Filter by customer or contract type.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('contracts/active') ?>" 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, customer"
                           value="<?= esc($filters['search']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Type</label>
                    <select name="contract_type" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['product', 'service', 'project'] as $t): ?>
                            <option value="<?= $t ?>" <?= $filters['type'] === $t ? 'selected' : '' ?>><?= ucfirst($t) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">Customer</label>
                    <select name="company_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($companies as $c): ?>
                            <option value="<?= $c['id'] ?>" <?= (string) $filters['companyId'] === (string) $c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </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>Start</th>
                        <th>End</th>
                        <th>Value</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['start_date'] ?? null)) ?></td>
                            <td><?= esc($formatDate($row['end_date'] ?? null)) ?></td>
                            <td><?= number_format((float) $row['value'], 2) ?></td>
                            <td><?= $statusPill($row['status']) ?></td>
                            <td class="text-end enterprise-row-actions">
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="dropdown"><i class="fa-solid fa-ellipsis"></i></button>
                                    <ul class="dropdown-menu dropdown-menu-end enterprise-nav-menu">
                                        <li><a class="dropdown-item" href="<?= base_url('contracts/contracts/' . $row['id']) ?>"><i class="fa-solid fa-eye me-2"></i>View</a></li>
                                        <li><a class="dropdown-item" href="<?= base_url('contracts/contracts/' . $row['id'] . '/edit') ?>"><i class="fa-solid fa-pen me-2"></i>Edit</a></li>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($contracts) === 0): ?>
                        <tr><td colspan="9" class="text-center text-muted py-5">No active contracts found.</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() ?>
