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

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

<?php
$formatDate = static fn (?string $d): string => $d ? date('M j, Y', strtotime($d)) : '—';
$prevMonth  = date('Y-m', strtotime($month . '-01 -1 month'));
$nextMonth  = date('Y-m', strtotime($month . '-01 +1 month'));
$monthLabel = date('F Y', strtotime($month . '-01'));
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => 'Contract Schedule',
    'workspaceTitle'        => 'Contract Schedule',
    'workspaceDesc'         => 'Contract periods and milestones for the selected month.',
]) ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Schedule — <?= esc($monthLabel) ?></h2>
        <p class="enterprise-data-panel__subtitle">Contracts overlapping this month and milestone due dates.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('contracts/schedule') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Month</label>
                    <input type="month" name="month" class="form-control form-control-sm" value="<?= esc($filters['month']) ?>">
                </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 d-flex gap-2 align-items-end">
                    <a href="<?= base_url('contracts/schedule?month=' . $prevMonth . ($filters['search'] !== '' ? '&q=' . rawurlencode($filters['search']) : '')) ?>" class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-chevron-left"></i></a>
                    <button type="submit" class="btn btn-sm btn-primary">Apply</button>
                    <a href="<?= base_url('contracts/schedule?month=' . $nextMonth . ($filters['search'] !== '' ? '&q=' . rawurlencode($filters['search']) : '')) ?>" class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-chevron-right"></i></a>
                </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>Status</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><span class="enterprise-status-pill enterprise-status-pill--<?= $row['status'] === 'active' ? 'active' : 'inactive' ?>"><?= esc(ucfirst($row['status'])) ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($contracts) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No contracts scheduled for this month.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Milestones Due</h2>
        <p class="enterprise-data-panel__subtitle">Project milestones with due dates in <?= esc($monthLabel) ?>.</p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Due Date</th>
                        <th>Milestone</th>
                        <th>Contract</th>
                        <th>Customer</th>
                        <th>Amount</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($milestones as $m): ?>
                        <tr>
                            <td><?= esc($formatDate($m['due_date'] ?? null)) ?></td>
                            <td><?= esc($m['title']) ?></td>
                            <td>
                                <a href="<?= base_url('contracts/contracts/' . $m['contract_id']) ?>" class="text-decoration-none">
                                    <code><?= esc($m['contract_number']) ?></code>
                                </a>
                                <div class="small text-muted"><?= esc($m['contract_title']) ?></div>
                            </td>
                            <td><?= esc($m['company_name'] ?? '—') ?></td>
                            <td><?= number_format((float) $m['amount'], 2) ?></td>
                            <td>
                                <span class="enterprise-status-pill enterprise-status-pill--<?= $m['status'] === 'done' ? 'active' : 'inactive' ?>">
                                    <?= esc(ucfirst($m['status'])) ?>
                                </span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($milestones) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No milestones due this month.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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