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

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

<?php
$statusPill = static function (string $status): string {
    $class = $status === 'active' ? 'active' : 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Contracts',
    'workspaceCrumbCurrent' => $contract['number'],
    'workspaceTitle'        => esc($contract['title']),
    'workspaceDesc'         => ucfirst($contract['contract_type']) . ' contract · ' . esc($company['name'] ?? 'Unknown customer'),
]) ?>

<div class="row g-4">
    <div class="col-lg-8">
        <article class="enterprise-settings-card mb-4">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-file-contract"></i>
                <h2 class="enterprise-settings-card__title">Contract Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Number</dt>
                <dd class="col-sm-8"><code><?= esc($contract['number']) ?></code></dd>
                <dt class="col-sm-4 text-muted">Customer</dt>
                <dd class="col-sm-8">
                    <?php if ($company): ?>
                        <a href="<?= base_url('contracts/customers/' . $company['id']) ?>"><?= esc($company['name']) ?></a>
                    <?php else: ?>
                        —
                    <?php endif; ?>
                </dd>
                <dt class="col-sm-4 text-muted">Type</dt>
                <dd class="col-sm-8"><?= esc(ucfirst($contract['contract_type'])) ?> based</dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($contract['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Dates</dt>
                <dd class="col-sm-8">
                    <?= esc($contract['start_date'] ?: '—') ?> → <?= esc($contract['end_date'] ?: '—') ?>
                </dd>
                <dt class="col-sm-4 text-muted">Value</dt>
                <dd class="col-sm-8 fw-semibold"><?= number_format((float) $contract['value'], 2) ?></dd>
                <?php if ($contract['contract_type'] === 'service'): ?>
                    <dt class="col-sm-4 text-muted">Hours</dt>
                    <dd class="col-sm-8"><?= esc($contract['total_hours']) ?> @ <?= number_format((float) $contract['hourly_rate'], 2) ?>/hr</dd>
                    <dt class="col-sm-4 text-muted">Location</dt>
                    <dd class="col-sm-8"><?= esc($contract['location'] ?: '—') ?></dd>
                <?php endif; ?>
                <?php if ($contract['contract_type'] === 'project'): ?>
                    <dt class="col-sm-4 text-muted">Project</dt>
                    <dd class="col-sm-8"><?= esc($contract['project_name'] ?: '—') ?></dd>
                    <dt class="col-sm-4 text-muted">Description</dt>
                    <dd class="col-sm-8"><?= esc($contract['project_description'] ?: '—') ?></dd>
                <?php endif; ?>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= esc($contract['notes'] ?: '—') ?></dd>
            </dl>
        </article>

        <?php if ($contract['contract_type'] === 'product'): ?>
            <article class="enterprise-settings-card mb-4">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-boxes-stacked"></i>
                    <h2 class="enterprise-settings-card__title">Product Lines</h2>
                </div>
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Code</th>
                                <th>Item</th>
                                <th>Qty</th>
                                <th>Unit</th>
                                <th>Unit Price</th>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($lines as $line): ?>
                                <tr>
                                    <td><?= esc($line['item_code'] ?: '—') ?></td>
                                    <td><?= esc($line['item_name']) ?></td>
                                    <td><?= esc($line['quantity']) ?></td>
                                    <td><?= esc($line['unit']) ?></td>
                                    <td><?= number_format((float) $line['unit_price'], 2) ?></td>
                                    <td><?= number_format((float) $line['line_total'], 2) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($lines) === 0): ?>
                                <tr><td colspan="6" class="text-muted">No lines.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </article>
        <?php endif; ?>

        <?php if ($contract['contract_type'] === 'service'): ?>
            <article class="enterprise-settings-card mb-4">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-users"></i>
                    <h2 class="enterprise-settings-card__title">Assigned Employees</h2>
                </div>
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Employee</th>
                                <th>Role</th>
                                <th>Hours allocated</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($assignments as $a): ?>
                                <tr>
                                    <td>
                                        <?= esc($a['full_name'] ?? 'Employee #' . $a['employee_id']) ?>
                                        <div class="text-muted small"><?= esc($a['employee_number'] ?? '') ?></div>
                                    </td>
                                    <td><?= esc($a['role_on_contract'] ?: '—') ?></td>
                                    <td><?= $a['hours_allocated'] !== null ? esc($a['hours_allocated']) : '—' ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($assignments) === 0): ?>
                                <tr><td colspan="3" class="text-muted">No employees assigned.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </article>
        <?php endif; ?>

        <?php if ($contract['contract_type'] === 'project'): ?>
            <article class="enterprise-settings-card mb-4">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-flag-checkered"></i>
                    <h2 class="enterprise-settings-card__title">Milestones</h2>
                </div>
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Title</th>
                                <th>Due</th>
                                <th>Amount</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($milestones as $m): ?>
                                <tr>
                                    <td><?= esc($m['title']) ?></td>
                                    <td><?= esc($m['due_date'] ?: '—') ?></td>
                                    <td><?= number_format((float) $m['amount'], 2) ?></td>
                                    <td><?= esc(ucfirst($m['status'])) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($milestones) === 0): ?>
                                <tr><td colspan="4" class="text-muted">No milestones.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </article>
        <?php endif; ?>
    </div>

    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-bolt"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <div class="d-grid gap-2">
                <a href="<?= base_url('contracts/contracts/' . $contract['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                    <i class="fa-solid fa-pen me-1"></i> Edit Contract
                </a>
                <a href="<?= base_url('contracts/active') ?>" class="btn btn-outline-secondary btn-sm">Back to List</a>
                <form action="<?= base_url('contracts/contracts/' . $contract['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this contract?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                        <i class="fa-solid fa-trash me-1"></i> Delete
                    </button>
                </form>
            </div>
        </article>
    </div>
</div>

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