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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Messages',
    'workspaceCrumbCurrent' => 'Groups',
    'workspaceTitle'        => 'Message Groups',
    'workspaceDesc'         => 'Create team channels (e.g. HRM) and choose who can join.',
    'workspaceAction'       => [
        'href'  => base_url('messages/groups/create'),
        'label' => 'New Group',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Groups</h2>
        <p class="enterprise-data-panel__subtitle">
            <a href="<?= base_url('messages') ?>">← Back to messages</a>
        </p>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="table enterprise-table mb-0">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Members</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($groups)): ?>
                        <tr>
                            <td colspan="4" class="text-muted text-center py-4">No groups yet. Create one such as HRM.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($groups as $g): ?>
                            <tr>
                                <td class="fw-semibold"><?= esc($g['name']) ?></td>
                                <td class="text-muted"><?= esc($g['description'] ?? '—') ?></td>
                                <td><?= (int) ($g['member_count'] ?? 0) ?></td>
                                <td class="text-end">
                                    <a href="<?= base_url('messages?c=' . $g['id']) ?>" class="btn btn-sm btn-outline-secondary">Open</a>
                                    <a href="<?= base_url('messages/groups/' . $g['id'] . '/edit') ?>" class="btn btn-sm btn-outline-primary">Edit</a>
                                    <form action="<?= base_url('messages/groups/' . $g['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this group and all its messages?');">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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