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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => 'New Request',
    'workspaceTitle'        => 'New Approval Request',
    'workspaceDesc'         => 'Describe what you need and send it to the person who must approve it.',
]) ?>

<article class="enterprise-settings-card" style="max-width:860px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-paper-plane"></i>
        <h2 class="enterprise-settings-card__title">Request Details</h2>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form action="<?= base_url('operations/approvals/requests') ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <div class="enterprise-settings-fields">
            <div class="enterprise-field enterprise-field--full">
                <label class="enterprise-field-label">Title <span class="text-danger">*</span></label>
                <input type="text" name="title" class="form-control" maxlength="200" required
                       value="<?= esc(old('title')) ?>" placeholder="e.g. Buy a company car">
            </div>
            <div class="enterprise-field enterprise-field--full">
                <label class="enterprise-field-label">Description</label>
                <textarea name="description" class="form-control" rows="4"
                          placeholder="Explain why you need this and any important details"><?= esc(old('description')) ?></textarea>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Request Type <span class="text-danger">*</span></label>
                <select name="request_type" class="form-select" required>
                    <?php foreach ($types as $type): ?>
                        <option value="<?= esc($type) ?>" <?= old('request_type', 'purchase') === $type ? 'selected' : '' ?>>
                            <?= esc(ucfirst($type)) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Amount (optional)</label>
                <input type="number" name="amount" class="form-control" step="0.01" min="0"
                       value="<?= esc(old('amount')) ?>" placeholder="0.00">
            </div>
            <div class="enterprise-field enterprise-field--full">
                <label class="enterprise-field-label">Send to (Approver) <span class="text-danger">*</span></label>
                <select name="approver_user_id" class="form-select" required>
                    <option value="">Select your boss / manager</option>
                    <?php foreach ($approvers as $a): ?>
                        <option value="<?= (int) $a['id'] ?>" <?= (string) old('approver_user_id') === (string) $a['id'] ? 'selected' : '' ?>>
                            <?= esc($a['label']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <div class="form-text">Only this person will see the request in their inbox to approve or reject.</div>
            </div>
        </div>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('operations/approvals/my-requests') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">
                <i class="fa-solid fa-paper-plane me-1"></i> Send Request
            </button>
        </div>
    </form>
</article>

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