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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent'   => 'New Department',
    'workspaceTitle'        => 'Department Provisioning Workspace',
    'workspaceDesc'         => 'Define a new organizational unit with branch alignment, managerial assignment, and operational status configuration.',
]) ?>

<article class="enterprise-settings-card" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-sitemap"></i>
        <h2 class="enterprise-settings-card__title">Department Configuration</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('departments') ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <div class="enterprise-settings-fields enterprise-settings-fields--single">
            <div class="enterprise-field">
                <label class="enterprise-field-label">Department Name <span class="text-danger">*</span></label>
                <input type="text" name="name" class="form-control" value="<?= esc(old('name')) ?>" required>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Branch</label>
                <select name="branch_id" class="form-select">
                    <option value="">— None —</option>
                    <?php foreach ($branches as $b): ?>
                        <option value="<?= $b['id'] ?>" <?= old('branch_id') == $b['id'] ? 'selected' : '' ?>><?= esc($b['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Manager</label>
                <select name="manager_id" class="form-select">
                    <option value="">— None —</option>
                    <?php foreach ($managers as $m): ?>
                        <option value="<?= $m->id ?>" <?= old('manager_id') == $m->id ? 'selected' : '' ?>><?= esc($m->getFullName()) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Operational Status</label>
                <select name="status" class="form-select">
                    <option value="active">Active</option>
                    <option value="inactive">Inactive</option>
                </select>
            </div>
        </div>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('departments') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Create Department</button>
        </div>
    </form>
</article>

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