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

<h3 class="mb-3">سجل النشاطات <?= session()->get('is_super_admin') ? '(كل الشركات)' : '(شركتك)' ?></h3>

<div class="card p-3 mb-3">
<form method="get" class="row g-2 align-items-end">
    <?php if (session()->get('is_super_admin')): ?>
        <div class="col-auto">
            <label class="form-label small">فلترة بالشركة</label>
            <select name="company_id" class="form-select form-select-sm">
                <option value="">كل الشركات</option>
                <?php foreach ($companies as $id => $name): ?>
                    <option value="<?= $id ?>" <?= $selectedCompany == $id ? 'selected' : '' ?>><?= esc($name) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    <?php endif; ?>
    <div class="col-auto">
        <label class="form-label small">فلترة بنوع العملية</label>
        <input type="text" name="action" class="form-control form-control-sm" placeholder="مثال: invoice" value="<?= esc($selectedAction ?? '') ?>">
    </div>
    <div class="col-auto">
        <button class="btn btn-dark btn-sm">فلترة</button>
        <a href="/admin/logs" class="btn btn-light btn-sm">إعادة تعيين</a>
    </div>
</form>
</div>

<div class="card p-3">
<table class="table table-sm">
    <thead><tr>
        <th>التاريخ والوقت</th>
        <th>المستخدم</th>
        <?php if (session()->get('is_super_admin')): ?><th>الشركة</th><?php endif; ?>
        <th>العملية</th>
        <th>التفاصيل</th>
        <th>IP</th>
    </tr></thead>
    <tbody>
    <?php foreach ($logs as $log): ?>
        <tr>
            <td class="small text-muted"><?= esc($log['created_at']) ?></td>
            <td><?= esc($log['user_name'] ?? 'غير معروف') ?></td>
            <?php if (session()->get('is_super_admin')): ?>
                <td><?= esc($companies[$log['company_id']] ?? '-') ?></td>
            <?php endif; ?>
            <td><span class="badge bg-secondary"><?= esc($log['action']) ?></span></td>
            <td class="small"><?= esc($log['description']) ?></td>
            <td class="small text-muted"><?= esc($log['ip_address']) ?></td>
        </tr>
    <?php endforeach; ?>
    <?php if (empty($logs)): ?><tr><td colspan="6" class="text-center text-muted"><?= lang('App.no_data') ?></td></tr><?php endif; ?>
    </tbody>
</table>
<?= $pager->links() ?>
</div>
<?= $this->endSection() ?>
