<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>اللوكرات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.locker.manage')): ?>
<a href="/sa/lockers/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'); ?>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/lockers" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($search ?? '') ?>" placeholder="ابحث بالكود أو الاسم..." class="form-input">
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">المرفق</label>
            <select name="facility_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($facilities as $fac): ?>
                <option value="<?= (int) $fac['id'] ?>" <?= ($facilityId ?? 0) == $fac['id'] ? 'selected' : '' ?>><?= e($fac['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:150px;">
            <label class="form-label" style="font-size:12px;">اللعبة</label>
            <select name="discipline_id" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($disciplines as $disc): ?>
                <option value="<?= (int) $disc['id'] ?>" <?= ($disciplineId ?? 0) == $disc['id'] ? 'selected' : '' ?>><?= e($disc['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:120px;">
            <label class="form-label" style="font-size:12px;">النوع</label>
            <select name="locker_type" class="form-select">
                <option value="">-- الكل --</option>
                <?php foreach ($typeOptions as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($lockerType ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <?php if (($search ?? '') !== '' || ($facilityId ?? 0) > 0 || ($disciplineId ?? 0) > 0 || ($lockerType ?? '') !== ''): ?>
        <a href="/sa/lockers" class="btn btn-outline" style="color:#6B7280;">مسح</a>
        <?php endif; ?>
    </form>
</div>

<!-- Results -->
<?php if (!empty($lockers)): ?>
<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 ($lockers as $locker): ?>
                <tr>
                    <td><code style="font-size:12px;background:#F3F4F6;padding:2px 6px;border-radius:4px;"><?= e($locker['code'] ?? '') ?></code></td>
                    <td>
                        <a href="/sa/lockers/<?= (int) $locker['id'] ?>" style="color:#1e3a5f;text-decoration:none;font-weight:600;">
                            <?= e($locker['name_ar'] ?? '') ?>
                        </a>
                        <?php if (!empty($locker['name_en'])): ?>
                        <div style="font-size:11px;color:#9CA3AF;"><?= e($locker['name_en']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td><?= e($locker['facility_name'] ?? '-') ?></td>
                    <td><?= e($locker['discipline_name'] ?? '-') ?></td>
                    <td><?= e($typeOptions[$locker['locker_type'] ?? ''] ?? '-') ?></td>
                    <td>
                        <?php if (!empty($locker['is_occupied'])): ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:#FEE2E2;color:#DC2626;">مشغول</span>
                        <?php elseif ((int) ($locker['is_active'] ?? 0)): ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:#D1FAE5;color:#059669;">متاح</span>
                        <?php else: ?>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:#F3F4F6;color:#6B7280;">معطّل</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div style="display:flex;gap:4px;">
                            <a href="/sa/lockers/<?= (int) $locker['id'] ?>" class="btn btn-sm btn-outline" title="عرض">
                                <i data-lucide="eye" style="width:14px;height:14px;"></i>
                            </a>
                            <?php if (can('sa.locker.manage')): ?>
                            <a href="/sa/lockers/<?= (int) $locker['id'] ?>/edit" class="btn btn-sm btn-outline" title="تعديل">
                                <i data-lucide="edit-3" style="width:14px;height:14px;"></i>
                            </a>
                            <form method="POST" action="/sa/lockers/<?= (int) $locker['id'] ?>/toggle" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-outline" title="<?= (int) $locker['is_active'] ? 'تعطيل' : 'تفعيل' ?>" onclick="return confirm('<?= (int) $locker['is_active'] ? 'هل أنت متأكد من تعطيل هذا اللوكر؟' : 'هل أنت متأكد من تفعيل هذا اللوكر؟' ?>');">
                                    <i data-lucide="<?= (int) $locker['is_active'] ? 'toggle-right' : 'toggle-left' ?>" style="width:14px;height:14px;"></i>
                                </button>
                            </form>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <?php if (($pagination['last_page'] ?? 1) > 1): ?>
    <div style="padding:15px;">
        <?php
        $queryParams = '';
        if (($search ?? '') !== '') $queryParams .= 'q=' . urlencode($search) . '&';
        if (($facilityId ?? 0) > 0) $queryParams .= 'facility_id=' . $facilityId . '&';
        if (($disciplineId ?? 0) > 0) $queryParams .= 'discipline_id=' . $disciplineId . '&';
        if (($lockerType ?? '') !== '') $queryParams .= 'locker_type=' . urlencode($lockerType) . '&';
        ?>
        <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination, 'baseUrl' => '/sa/lockers?' . $queryParams]); ?>
    </div>
    <?php endif; ?>
</div>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="lock" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد لوكرات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?php if (($search ?? '') !== '' || ($facilityId ?? 0) > 0 || ($disciplineId ?? 0) > 0 || ($lockerType ?? '') !== ''): ?>
            لا توجد نتائج مطابقة. جرب تغيير معايير البحث.
        <?php else: ?>
            ابدأ بإضافة لوكر جديد.
        <?php endif; ?>
    </p>
    <?php if (can('sa.locker.manage')): ?>
    <a href="/sa/lockers/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; ?>

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