<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>المنشآت الرياضية<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<?php if (can('sa.facility.manage')): ?>
<a href="/sa/facilities/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:20px;padding:15px;">
    <form method="GET" action="/sa/facilities" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="الكود أو الاسم..." class="form-input" style="min-width:200px;">
        </div>
        <div>
            <label class="form-label" style="font-size:12px;">النوع</label>
            <select name="type" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($typeOptions as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline">بحث</button>
        <a href="/sa/facilities" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<?php if (!empty($rows)): ?>
<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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                <tr>
                    <td style="font-family:monospace;font-size:13px;"><?= e($row['code']) ?></td>
                    <td>
                        <a href="/sa/facilities/<?= (int) $row['id'] ?>" style="color:#0D7377;font-weight:600;"><?= e($row['name_ar']) ?></a>
                        <?php if (!empty($row['name_en'])): ?>
                        <br><small style="color:#9CA3AF;"><?= e($row['name_en']) ?></small>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?= e($typeOptions[$row['facility_type']] ?? $row['facility_type']) ?>
                    </td>
                    <td style="font-size:13px;"><?= e($row['discipline_name'] ?? '—') ?></td>
                    <td style="text-align:center;">
                        <a href="/sa/facilities/<?= (int) $row['id'] ?>/units" style="color:#0D7377;"><?= (int) $row['unit_count'] ?></a>
                    </td>
                    <td>
                        <?php if ($row['is_active']): ?>
                        <span style="color:#059669;font-weight:600;">مفعّلة</span>
                        <?php else: ?>
                        <span style="color:#DC2626;font-weight:600;">معطّلة</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div style="display:flex;gap:5px;flex-wrap:wrap;">
                            <a href="/sa/facilities/<?= (int) $row['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
                            <?php if (can('sa.facility.manage')): ?>
                            <a href="/sa/facilities/<?= (int) $row['id'] ?>/edit" class="btn btn-sm btn-outline">تعديل</a>
                            <form method="POST" action="/sa/facilities/<?= (int) $row['id'] ?>/toggle" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm <?= $row['is_active'] ? 'btn-outline' : 'btn-primary' ?>" style="font-size:11px;">
                                    <?= $row['is_active'] ? 'تعطيل' : 'تفعيل' ?>
                                </button>
                            </form>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
    <div style="padding:15px;">
        <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
    </div>
    <?php endif; ?>
</div>
<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد منشآت</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?php if (!empty($filters['q']) || !empty($filters['type'])): ?>
            لا توجد نتائج مطابقة لبحثك. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة منشأة رياضية جديدة.
        <?php endif; ?>
    </p>
    <?php if (can('sa.facility.manage')): ?>
    <a href="/sa/facilities/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إضافة منشأة</a>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php $__template->endSection(); ?>
