<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>المراية — إدارة الشبكات<?php $__template->endSection(); ?>

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

<!-- Create New Grid -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="plus-circle" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">إنشاء مراية جديدة</h3>
    </div>
    <div style="padding:20px;">
        <form method="POST" action="/mirror/create" style="display:flex;gap:12px;align-items:flex-end;flex-wrap:wrap;">
            <?= csrf_field() ?>
            <div class="form-group" style="margin:0;flex:1;min-width:200px;">
                <label class="form-label">المرفق</label>
                <select name="facility_id" class="form-select" required>
                    <option value="">-- اختر المرفق --</option>
                    <?php foreach ($facilities as $f): ?>
                    <option value="<?= (int) $f['id'] ?>"><?= e($f['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group" style="margin:0;flex:1;min-width:180px;">
                <label class="form-label">اسم المراية</label>
                <input type="text" name="name_ar" class="form-input" required placeholder="مثال: مراية حمام السباحة الأولمبي">
            </div>
            <div class="form-group" style="margin:0;width:100px;">
                <label class="form-label">الصفوف</label>
                <input type="number" name="rows_count" class="form-input" value="4" min="1" max="10" style="direction:ltr;text-align:center;">
            </div>
            <div class="form-group" style="margin:0;width:100px;">
                <label class="form-label">الأعمدة</label>
                <input type="number" name="cols_count" class="form-input" value="6" min="1" max="12" style="direction:ltr;text-align:center;">
            </div>
            <button type="submit" class="btn btn-primary" style="height:38px;">
                <i data-lucide="grid-3x3" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> إنشاء
            </button>
        </form>
    </div>
</div>

<!-- Existing Grids -->
<?php if (!empty($grids)): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(300px, 1fr));gap:20px;">
    <?php foreach ($grids as $g): ?>
    <div class="card" style="padding:20px;border-top:4px solid #0D7377;">
        <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:12px;">
            <div>
                <h3 style="margin:0 0 4px;font-size:16px;color:#1A1A2E;"><?= e($g['name_ar']) ?></h3>
                <div style="font-size:12px;color:#6B7280;"><?= e($g['facility_name'] ?? '') ?></div>
            </div>
            <div style="width:40px;height:40px;border-radius:10px;background:#0D737715;display:flex;align-items:center;justify-content:center;">
                <i data-lucide="grid-3x3" style="width:20px;height:20px;color:#0D7377;"></i>
            </div>
        </div>
        <div style="font-size:13px;color:#6B7280;margin-bottom:15px;">
            <?= (int) $g['rows_count'] ?> صفوف × <?= (int) $g['cols_count'] ?> أعمدة
            = <?= (int) $g['rows_count'] * (int) $g['cols_count'] ?> خانة
        </div>
        <div style="display:flex;gap:8px;">
            <a href="/mirror/<?= (int) $g['id'] ?>" class="btn btn-primary" style="font-size:13px;flex:1;text-align:center;">
                <i data-lucide="eye" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> فتح المراية
            </a>
            <form method="POST" action="/mirror/<?= (int) $g['id'] ?>/delete" style="margin:0;" onsubmit="return confirm('هل أنت متأكد من حذف هذه المراية؟')">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline" style="font-size:13px;color:#DC2626;border-color:#DC2626;">
                    <i data-lucide="trash-2" style="width:14px;height:14px;vertical-align:middle;"></i>
                </button>
            </form>
        </div>
    </div>
    <?php endforeach; ?>
</div>
<?php else: ?>
<div class="card" style="padding:40px;text-align:center;">
    <i data-lucide="grid-3x3" style="width:48px;height:48px;color:#D1D5DB;margin-bottom:12px;"></i>
    <p style="color:#6B7280;font-size:14px;">لا توجد مرايات بعد. أنشئ واحدة جديدة للبدء.</p>
</div>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});
</script>

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