<?php
/**
 * Address book aside panel — people browser with tenant/department hierarchy.
 *
 * Rendered by the module slot system. The outer <nav> with id, role, aria
 * attributes is created by the slot renderer in app-main-aside.phtml.
 * This template provides only the inner content.
 *
 * Available in scope via include: $layoutNav, $layoutAuth, $viewAuth
 */
$addressBook = is_array($layoutNav['addressbook.nav'] ?? null) ? $layoutNav['addressbook.nav'] : [];
$addressBookUrl = trim((string) ($addressBook['url'] ?? lurl('address-book')));
$activeAddressSearch = trim((string) ($addressBook['activeSearch'] ?? ''));
$activeAddressTenants = is_array($addressBook['activeTenants'] ?? null) ? $addressBook['activeTenants'] : [];
$activeAddressDepartments = is_array($addressBook['activeDepartments'] ?? null) ? $addressBook['activeDepartments'] : [];
$activeAddressRoles = is_array($addressBook['activeRoles'] ?? null) ? $addressBook['activeRoles'] : [];
$activeAddressCustomFilters = is_array($addressBook['activeCustomFilters'] ?? null) ? $addressBook['activeCustomFilters'] : [];
$peopleGroups = is_array($addressBook['peopleGroups'] ?? null) ? $addressBook['peopleGroups'] : [];
$addressBookActive = navActive('address-book', true);
?>
<ul>
    <?php if (!$peopleGroups): ?>
        <li>
            <a href="<?php e($addressBookUrl); ?>" class="<?php e($addressBookActive['class'] ?? ''); ?>"
                <?php echo $addressBookActive['aria'] ?? ''; ?>>
                <?php e(t('Address book')); ?>
            </a>
        </li>
    <?php else: ?>
        <?php foreach ($peopleGroups as $index => $group): ?>
            <?php
            $tenant = $group['tenant'] ?? [];
            $departments = $group['departments'] ?? [];
            $tenantUuid = (string) ($tenant['uuid'] ?? '');
            $tenantName = (string) ($tenant['description'] ?? '');
            if ($tenantUuid === '') {
                continue;
            }
            $baseHref = $addressBookUrl . '?tenants=' . urlencode($tenantUuid);
            $isActiveTenant = $addressBookActive['isActive']
                && !$activeAddressDepartments
                && count($activeAddressTenants) === 1
                && $activeAddressTenants[0] === $tenantUuid;
            ?>
            <li class="app-sidebar-group">
                    <small>
                        <a href="<?php e($baseHref); ?>" class="<?php e($isActiveTenant ? 'active' : 'muted'); ?>"
                            <?php echo $isActiveTenant ? 'aria-current="page"' : ''; ?>>
                            <?php e($tenantName); ?>
                        </a>
                    </small>
                    <ul>
                        <?php if (!$departments): ?>
                            <li>
                                <?php
                                $emptyState = [
                                    'message' => t('No departments'),
                                    'size' => 'small',
                                    'align' => 'center',
                                ];
                                require templatePath('partials/app-empty-state.phtml');
                                ?>
                            </li>
                        <?php else: ?>
                            <?php foreach ($departments as $department): ?>
                                <?php
                                $departmentId = (int) ($department['id'] ?? 0);
                                if ($departmentId <= 0) {
                                    continue;
                                }
                                $departmentName = (string) ($department['description'] ?? '');
                                $href = $baseHref . '&departments=' . $departmentId;
                                $isActive = $addressBookActive['isActive']
                                    && in_array($tenantUuid, $activeAddressTenants, true)
                                    && in_array($departmentId, $activeAddressDepartments, true);
                                ?>
                                <li>
                                    <a href="<?php e($href); ?>" class="<?php e($isActive ? 'active' : ''); ?>"
                                        <?php echo $isActive ? 'aria-current="page"' : ''; ?>>
                                        <?php e($departmentName); ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </ul>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>
