<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الموظف: <?= e($employee->full_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/users/<?= (int) $employee->id ?>/edit" class="btn btn-outline">تعديل</a>
<a href="/users/<?= (int) $employee->id ?>/permissions" class="btn btn-outline">الصلاحيات</a>
<a href="/users/<?= (int) $employee->id ?>/activity" class="btn btn-outline">سجل النشاط</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
    <div class="card" style="padding:20px;">
        <h3 style="margin-bottom:15px;color:#0D7377;">البيانات الشخصية</h3>
        <table style="width:100%;font-size:14px;">
            <tr><td style="padding:8px 0;color:#6B7280;width:40%;">اسم المستخدم</td><td style="padding:8px 0;font-weight:600;"><?= e($employee->username) ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">الاسم بالعربي</td><td style="padding:8px 0;"><?= e($employee->full_name_ar) ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">الاسم بالإنجليزي</td><td style="padding:8px 0;"><?= e($employee->full_name_en ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">البريد الإلكتروني</td><td style="padding:8px 0;"><?= e($employee->email ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">الهاتف</td><td style="padding:8px 0;"><?= e($employee->phone ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">الفرع</td><td style="padding:8px 0;"><?= e($employee->getBranchName()) ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">الحالة</td><td style="padding:8px 0;"><?= $employee->is_active ? '<span style="color:#059669;">● نشط</span>' : '<span style="color:#DC2626;">● معطل</span>' ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">آخر تسجيل دخول</td><td style="padding:8px 0;"><?= $employee->last_login_at ? arabic_date($employee->last_login_at) : 'لم يسجل دخول' ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">آخر IP</td><td style="padding:8px 0;direction:ltr;text-align:right;"><?= e($employee->last_login_ip ?? '—') ?></td></tr>
            <tr><td style="padding:8px 0;color:#6B7280;">تاريخ الإنشاء</td><td style="padding:8px 0;"><?= arabic_date($employee->created_at) ?></td></tr>
        </table>
    </div>
    <div>
        <div class="card" style="padding:20px;margin-bottom:20px;">
            <h3 style="margin-bottom:15px;color:#0D7377;">الأدوار</h3>
            <?php if (empty($roles)): ?>
                <p style="color:#6B7280;">لا توجد أدوار مُعيّنة</p>
            <?php else: ?>
                <?php foreach ($roles as $role): ?>
                    <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid #F3F4F6;">
                        <span style="font-weight:600;"><?= e($role['name_ar']) ?></span>
                        <small style="color:#6B7280;"><?= e($role['role_code']) ?></small>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
        <div class="card" style="padding:20px;margin-bottom:20px;">
            <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:15px;">
                <h3 style="margin:0;color:#0D7377;">الصلاحيات الفعّالة</h3>
                <a href="/users/<?= (int) $employee->id ?>/permissions" style="font-size:12px;color:#0D7377;">إدارة &larr;</a>
            </div>
            <?php
            $allPerms = $employee->getAllPermissions();
            $hasStar = in_array('*', $allPerms, true);
            $permsByGroup = [];
            $allGrouped = \App\Core\Registries\PermissionRegistry::getAllGrouped();
            foreach ($allGrouped as $gName => $gPerms) {
                $cnt = 0;
                foreach ($gPerms as $pk => $_) {
                    if ($hasStar || in_array($pk, $allPerms, true)) $cnt++;
                }
                if ($cnt > 0) $permsByGroup[$gName] = $cnt;
            }
            ?>
            <div style="text-align:center;padding:10px 0;margin-bottom:10px;">
                <span style="font-size:28px;font-weight:700;color:#059669;"><?= $hasStar ? '∞' : count($allPerms) ?></span>
                <span style="color:#6B7280;font-size:13px;display:block;">إجمالي الصلاحيات</span>
            </div>
            <?php if (!empty($permsByGroup)): ?>
            <div style="font-size:13px;">
                <?php foreach ($permsByGroup as $gn => $gc): ?>
                <div style="display:flex;justify-content:space-between;padding:3px 0;border-bottom:1px solid #F3F4F6;">
                    <span style="color:#6B7280;"><?= e($gn) ?></span>
                    <span style="font-weight:600;"><?= $gc ?></span>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
        <div class="card" style="padding:20px;">
            <h3 style="margin-bottom:15px;color:#0D7377;">الجلسات النشطة</h3>
            <?php if (empty($sessions)): ?>
                <p style="color:#6B7280;">لا توجد جلسات نشطة</p>
            <?php else: ?>
                <?php foreach ($sessions as $s): ?>
                    <div style="padding:8px 0;border-bottom:1px solid #F3F4F6;font-size:13px;">
                        <div>IP: <span style="direction:ltr;display:inline-block;"><?= e($s['ip_address']) ?></span></div>
                        <div style="color:#6B7280;">آخر نشاط: <?= e($s['last_activity_at']) ?></div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>