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

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

<?php
$statusPill = static function (string $status): string {
    $label    = ucwords(str_replace('_', ' ', $status));
    $active   = ['approved', 'sent', 'converted', 'fulfilled', 'delivered'];
    $inactive = ['cancelled', 'rejected'];
    if (in_array($status, $active, true)) {
        $class = 'active';
    } elseif (in_array($status, $inactive, true)) {
        $class = 'inactive';
    } else {
        $class = 'default';
    }
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc($label) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Quotation Approval',
    'workspaceTitle'        => 'Quotation Approval',
    'workspaceDesc'         => 'Review and approve or reject quotations awaiting decision.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Pending Approvals</h2>
        <p class="enterprise-data-panel__subtitle">Quotations submitted for approval workflow.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/quotation-approval') ?>" 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, company, notes"
                           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>Company</th>
                        <th>Date</th>
                        <th>Valid Until</th>
                        <th>Total</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($quotations as $q): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><code><?= esc($q['number']) ?></code></span></td>
                            <td><?= esc($q['company_name'] ?? '—') ?></td>
                            <td><?= esc($q['quotation_date'] ?? '—') ?></td>
                            <td><?= $q['valid_until'] ? esc($q['valid_until']) : '—' ?></td>
                            <td><?= number_format((float) ($q['total'] ?? 0), 2) ?></td>
                            <td><?= $statusPill($q['status']) ?></td>
                            <td class="text-end enterprise-row-actions">
                                <div class="d-flex flex-column align-items-end gap-2">
                                    <form action="<?= base_url('sale/quotation-approval/' . $q['id'] . '/approve') ?>" method="post" class="d-inline">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-primary">
                                            <i class="fa-solid fa-check me-1"></i>Approve
                                        </button>
                                    </form>
                                    <form action="<?= base_url('sale/quotation-approval/' . $q['id'] . '/reject') ?>" method="post" class="w-100" style="max-width:280px;">
                                        <?= csrf_field() ?>
                                        <label class="enterprise-field-label">Rejection reason</label>
                                        <textarea name="rejection_reason" class="form-control form-control-sm mb-1" rows="2" required placeholder="Reason for rejection"></textarea>
                                        <button type="submit" class="btn btn-sm btn-outline-danger w-100">
                                            <i class="fa-solid fa-xmark me-1"></i>Reject
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($quotations) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No quotations pending approval.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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