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

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

<?php
$statusPill = static function (string $status): string {
    $label = ucwords(str_replace('_', ' ', $status));
    $active = ['approved', 'confirmed', 'fulfilled', 'delivered', 'dispatched', 'in_transit', 'sent', 'converted'];
    $inactive = ['cancelled', 'rejected'];
    $mod = '';
    if (in_array($status, $active, true)) {
        $mod = ' enterprise-status-pill--active';
    } elseif (in_array($status, $inactive, true)) {
        $mod = ' enterprise-status-pill--inactive';
    }

    return '<span class="enterprise-status-pill' . $mod . '">' . esc($label) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Order Approval',
    'workspaceTitle'        => 'Order Approval',
    'workspaceDesc'         => 'Approve sales orders before fulfillment begins.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Pending Approval</h2>
        <p class="enterprise-data-panel__subtitle">Orders waiting for review and decision.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/order-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, quotation"
                           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>Quotation</th>
                        <th>Order Date</th>
                        <th>Status</th>
                        <th class="text-end">Total</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $o): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><code><?= esc($o['number']) ?></code></span></td>
                            <td><?= esc($o['company_name'] ?? '—') ?></td>
                            <td><?= ! empty($o['quotation_number']) ? '<code>' . esc($o['quotation_number']) . '</code>' : '—' ?></td>
                            <td><?= esc($o['order_date']) ?></td>
                            <td><?= $statusPill($o['status']) ?></td>
                            <td class="text-end"><?= number_format((float) ($o['total'] ?? 0), 2) ?></td>
                            <td class="text-end">
                                <form action="<?= base_url('sale/order-approval/' . $o['id'] . '/approve') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-success" title="Approve"><i class="fa-solid fa-check"></i></button>
                                </form>
                                <button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#rejectOrder<?= $o['id'] ?>" title="Reject">
                                    <i class="fa-solid fa-xmark"></i>
                                </button>
                                <div class="modal fade" id="rejectOrder<?= $o['id'] ?>" tabindex="-1">
                                    <div class="modal-dialog modal-sm">
                                        <div class="modal-content">
                                            <form action="<?= base_url('sale/order-approval/' . $o['id'] . '/reject') ?>" method="post">
                                                <?= csrf_field() ?>
                                                <div class="modal-header">
                                                    <h6 class="modal-title">Reject Order</h6>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <label class="enterprise-field-label">Rejection Reason</label>
                                                    <textarea name="rejection_reason" class="form-control form-control-sm" rows="3" placeholder="Reason (optional)"></textarea>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" class="btn btn-sm btn-danger">Reject</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($orders) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No orders pending approval.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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