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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'HRM',
    'workspaceCrumbCurrent'   => 'Edit Department',
    'workspaceTitle'        => 'Department Amendment Workspace',
    'workspaceDesc'         => 'Update organizational unit configuration for ' . esc($department['name']) . ' including branch mapping and managerial oversight.',
]) ?>

<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/' . $department['id']) ?>" 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', $department['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'] ?>" <?= (string) old('branch_id', $department['branch_id']) === (string) $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 ?>" <?= (string) old('manager_id', $department['manager_id']) === (string) $m->id ? 'selected' : '' ?>><?= esc($m->getFullName()) ?></option>
                    <?php endforeach; ?>
                </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">Save Amendments</button>
        </div>
    </form>
</article>

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