<?= $this->extend('admin/layout/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3><?= lang('App.users') ?></h3>
    <a href="/admin/users/create" class="btn btn-dark btn-sm"><?= lang('App.add_new') ?></a>
</div>

<div class="card p-3">
<table class="table">
    <thead><tr>
        <th><?= lang('App.name') ?></th>
        <th><?= lang('App.email') ?></th>
        <?php if (session()->get('is_super_admin')): ?><th><?= lang('App.companies') ?></th><?php endif; ?>
        <th>النوع</th>
        <th>حالة الصلاحية</th>
        <th><?= lang('App.actions') ?></th>
    </tr></thead>
    <tbody>
    <?php foreach ($users as $u): ?>
        <?php
            $isExpired = $model->isExpired($u);
            $daysLeft = $model->daysUntilExpiry($u);
        ?>
        <tr class="<?= $isExpired ? 'table-danger' : ($daysLeft !== null && $daysLeft <= 7 ? 'table-warning' : '') ?>">
            <td><?= esc($u['name']) ?></td>
            <td><?= esc($u['email']) ?></td>
            <?php if (session()->get('is_super_admin')): ?>
                <td><?= $u['company_id'] ? esc($companies[$u['company_id']] ?? '-') : '<span class="text-muted">—</span>' ?></td>
            <?php endif; ?>
            <td>
                <?php if ($u['is_super_admin']): ?>
                    <span class="badge bg-dark">سوبر أدمن</span>
                <?php else: ?>
                    <span class="badge bg-secondary">مدير شركة</span>
                <?php endif; ?>
            </td>
            <td class="small">
                <?php if (empty($u['expires_at'])): ?>
                    <span class="text-muted">دائم (بدون انتهاء)</span>
                <?php elseif ($isExpired): ?>
                    <span class="text-danger fw-bold">⛔ منتهي منذ <?= esc($u['expires_at']) ?></span>
                <?php else: ?>
                    <span class="<?= $daysLeft <= 7 ? 'text-warning fw-bold' : 'text-success' ?>">
                        ينتهي في <?= esc($u['expires_at']) ?> (<?= $daysLeft ?> يوم)
                    </span>
                <?php endif; ?>

                <?php if (!empty($u['expires_at'])): ?>
                    <form method="post" action="/admin/users/<?= $u['id'] ?>/renew" class="d-flex gap-1 mt-1">
                        <?= csrf_field() ?>
                        <select name="renew_months" class="form-select form-select-sm" style="width:auto;">
                            <option value="1">شهر</option>
                            <option value="3">3 شهور</option>
                            <option value="6">6 شهور</option>
                            <option value="12">سنة</option>
                        </select>
                        <button class="btn btn-sm btn-success">تجديد</button>
                    </form>
                <?php endif; ?>
            </td>
            <td>
                <a href="/admin/users/<?= $u['id'] ?>/edit" class="btn btn-sm btn-outline-primary"><?= lang('App.edit') ?></a>
                <form method="post" action="/admin/users/<?= $u['id'] ?>/delete" class="d-inline" onsubmit="return confirm('?')">
                    <?= csrf_field() ?>
                    <button class="btn btn-sm btn-outline-danger"><?= lang('App.delete') ?></button>
                </form>
            </td>
        </tr>
    <?php endforeach; ?>
    <?php if (empty($users)): ?><tr><td colspan="6" class="text-center text-muted"><?= lang('App.no_data') ?></td></tr><?php endif; ?>
    </tbody>
</table>
</div>
<?= $this->endSection() ?>
