<?php
$errors = session()->getFlashdata('errors') ?? [];
$oldName = old('name', $group['name'] ?? '');
$oldDesc = old('description', $group['description'] ?? '');
$selected = old('member_ids', $memberIds ?? []);
if (! is_array($selected)) {
    $selected = [];
}
$selected = array_map('intval', $selected);
?>

<form method="post" action="<?= esc($action) ?>">
    <?= csrf_field() ?>

    <div class="row g-3 mb-4">
        <div class="col-md-6">
            <label class="enterprise-field-label" for="name">Group name</label>
            <input
                type="text"
                name="name"
                id="name"
                class="form-control<?= isset($errors['name']) ? ' is-invalid' : '' ?>"
                value="<?= esc($oldName) ?>"
                placeholder="e.g. HRM"
                required
                maxlength="150"
            >
            <?php if (isset($errors['name'])): ?>
                <div class="invalid-feedback"><?= esc($errors['name']) ?></div>
            <?php endif; ?>
        </div>
        <div class="col-md-6">
            <label class="enterprise-field-label" for="description">Description</label>
            <input
                type="text"
                name="description"
                id="description"
                class="form-control<?= isset($errors['description']) ? ' is-invalid' : '' ?>"
                value="<?= esc($oldDesc) ?>"
                placeholder="Optional"
                maxlength="500"
            >
            <?php if (isset($errors['description'])): ?>
                <div class="invalid-feedback"><?= esc($errors['description']) ?></div>
            <?php endif; ?>
        </div>
    </div>

    <div class="mb-4">
        <label class="enterprise-field-label">Members</label>
        <p class="text-muted small mb-2">Select users who can see and post in this group.</p>
        <div class="msg-member-grid border rounded p-3" style="max-height: 320px; overflow: auto;">
            <?php if (empty($users)): ?>
                <p class="text-muted mb-0">No active users found.</p>
            <?php else: ?>
                <?php foreach ($users as $u): ?>
                    <?php
                    $uid = (int) $u['id'];
                    $label = trim(($u['first_name'] ?? '') . ' ' . ($u['last_name'] ?? '')) ?: ($u['username'] ?? 'User');
                    $checked = in_array($uid, $selected, true);
                    ?>
                    <label class="form-check mb-2">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            name="member_ids[]"
                            value="<?= $uid ?>"
                            <?= $checked ? 'checked' : '' ?>
                        >
                        <span class="form-check-label">
                            <?= esc($label) ?>
                            <span class="text-muted small">(<?= esc($u['username'] ?? '') ?>)</span>
                        </span>
                    </label>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <div class="d-flex gap-2">
        <button type="submit" class="btn btn-primary">Save group</button>
        <a href="<?= base_url('messages/groups') ?>" class="btn btn-outline-secondary">Cancel</a>
    </div>
</form>
