<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>المؤسسات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.institution.manage')): ?>
<a href="/sa/institutions/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة مؤسسة</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:15px;padding:12px 15px;">
    <form method="GET" action="/sa/institutions" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="search" class="form-control" value="<?= e($filters['search'] ?? '') ?>" placeholder="اسم أو كود...">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">النوع</label>
            <select name="type" class="form-select">
                <option value="">-- الكل --</option>
                <option value="school" <?= ($filters['type'] ?? '') === 'school' ? 'selected' : '' ?>>مدرسة</option>
                <option value="university" <?= ($filters['type'] ?? '') === 'university' ? 'selected' : '' ?>>جامعة</option>
                <option value="company" <?= ($filters['type'] ?? '') === 'company' ? 'selected' : '' ?>>شركة</option>
                <option value="club" <?= ($filters['type'] ?? '') === 'club' ? 'selected' : '' ?>>نادي</option>
                <option value="government" <?= ($filters['type'] ?? '') === 'government' ? 'selected' : '' ?>>حكومي</option>
                <option value="other" <?= ($filters['type'] ?? '') === 'other' ? 'selected' : '' ?>>أخرى</option>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:14px;height:14px;"></i> بحث</button>
    </form>
</div>

<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الكود</th>
                    <th>الاسم</th>
                    <th>النوع</th>
                    <th>المسؤول</th>
                    <th>الهاتف</th>
                    <th>عدد الحجوزات</th>
                    <th>الحالة</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($institutions)): ?>
                <?php foreach ($institutions as $inst): ?>
                <?php
                $typeMap = ['school'=>'مدرسة','university'=>'جامعة','company'=>'شركة','club'=>'نادي','government'=>'حكومي','other'=>'أخرى'];
                ?>
                <tr>
                    <td style="font-family:monospace;font-size:12px;"><?= e($inst['code']) ?></td>
                    <td style="font-weight:500;"><a href="/sa/institutions/<?= (int)$inst['id'] ?>"><?= e($inst['name_ar']) ?></a></td>
                    <td><span style="background:#EFF6FF;color:#2563EB;padding:2px 8px;border-radius:8px;font-size:11px;"><?= e($typeMap[$inst['institution_type']] ?? $inst['institution_type']) ?></span></td>
                    <td><?= e($inst['contact_person'] ?? '—') ?></td>
                    <td style="direction:ltr;text-align:right;"><?= e($inst['phone'] ?? '—') ?></td>
                    <td style="text-align:center;"><?= (int)($inst['booking_count'] ?? 0) ?></td>
                    <td>
                        <?php if ((int)$inst['is_active']): ?>
                        <span style="background:#ECFDF5;color:#059669;padding:2px 8px;border-radius:8px;font-size:11px;">نشط</span>
                        <?php else: ?>
                        <span style="background:#F3F4F6;color:#6B7280;padding:2px 8px;border-radius:8px;font-size:11px;">معطل</span>
                        <?php endif; ?>
                    </td>
                    <td style="white-space:nowrap;">
                        <a href="/sa/institutions/<?= (int)$inst['id'] ?>" class="btn btn-sm btn-outline" style="font-size:11px;">عرض</a>
                        <?php if (can('sa.institution.manage')): ?>
                        <a href="/sa/institutions/<?= (int)$inst['id'] ?>/edit" class="btn btn-sm btn-outline" style="font-size:11px;">تعديل</a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php else: ?>
                <tr><td colspan="8" style="text-align:center;padding:40px;color:#6B7280;">لا توجد مؤسسات مسجلة</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>document.addEventListener('DOMContentLoaded', function() { if (typeof lucide !== 'undefined') lucide.createIcons(); });</script>
<?php $__template->endSection(); ?>
