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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'New Leave Request',
    'workspaceTitle'      => 'Leave Request Submission',
    'workspaceDesc'       => 'Submit a leave request for approval. Balance will be checked automatically.',
]) ?>

<article class="enterprise-settings-card" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-plane-departure"></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('leave/requests') ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <div class="enterprise-settings-fields enterprise-settings-fields--single">
            <div class="enterprise-field">
                <label class="enterprise-field-label">Employee <span class="text-danger">*</span></label>
                <select name="employee_id" class="form-select" required>
                    <option value="">— Select —</option>
                    <?php foreach ($employees as $e): ?>
                        <option value="<?= $e['id'] ?>" <?= old('employee_id') == $e['id'] ? 'selected' : '' ?>>
                            <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Leave Type <span class="text-danger">*</span></label>
                <select name="leave_type_id" class="form-select" required>
                    <option value="">— Select —</option>
                    <?php foreach ($types as $t): ?>
                        <option value="<?= $t['id'] ?>" <?= old('leave_type_id') == $t['id'] ? 'selected' : '' ?>><?= esc($t['name']) ?> (<?= esc($t['code']) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Start Date <span class="text-danger">*</span></label>
                <input type="date" name="start_date" class="form-control" value="<?= esc(old('start_date')) ?>" required>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">End Date <span class="text-danger">*</span></label>
                <input type="date" name="end_date" class="form-control" value="<?= esc(old('end_date')) ?>" required>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Reason</label>
                <textarea name="reason" class="form-control" rows="3"><?= esc(old('reason')) ?></textarea>
            </div>
        </div>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('leave/requests') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Submit Request</button>
        </div>
    </form>
</article>

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