<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعديل المنشأة: <?= e($facility->name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/sa/facilities/<?= (int) $facility->id ?>" class="btn btn-outline">رجوع للتفاصيل</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<?php
$opHours = $facility->getOperatingHours();
?>

<div class="card" style="max-width:700px;padding:25px;">
    <h3 style="margin:0 0 20px;color:#1F2937;">تعديل المنشأة</h3>

    <form method="POST" action="/sa/facilities/<?= (int) $facility->id ?>">
        <?= csrf_field() ?>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">الكود <span style="color:#DC2626;">*</span></label>
                <input type="text" name="code" value="<?= e(old('code', $facility->code)) ?>" class="form-input" required maxlength="30" placeholder="مثال: FAC-001" style="direction:ltr;text-align:left;text-transform:uppercase;">
            </div>
            <div>
                <label class="form-label">نوع المنشأة <span style="color:#DC2626;">*</span></label>
                <select name="facility_type" class="form-select" required>
                    <option value="">اختر النوع</option>
                    <?php foreach ($typeOptions as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= old('facility_type', $facility->facility_type) === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">الاسم بالعربية <span style="color:#DC2626;">*</span></label>
                <input type="text" name="name_ar" value="<?= e(old('name_ar', $facility->name_ar)) ?>" class="form-input" required maxlength="300">
            </div>
            <div>
                <label class="form-label">الاسم بالإنجليزية</label>
                <input type="text" name="name_en" value="<?= e(old('name_en', $facility->name_en ?? '')) ?>" class="form-input" maxlength="300" dir="ltr">
            </div>
        </div>

        <div style="margin-bottom:15px;">
            <label class="form-label">اللعبة / النشاط</label>
            <select name="discipline_id" class="form-select">
                <option value="">بدون تحديد</option>
                <?php foreach ($disciplines as $disc): ?>
                <option value="<?= (int) $disc['id'] ?>" <?= old('discipline_id', $facility->discipline_id ?? '') == $disc['id'] ? 'selected' : '' ?>><?= e($disc['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div style="margin-bottom:15px;">
            <label class="form-label">وصف الموقع</label>
            <input type="text" name="location_description" value="<?= e(old('location_description', $facility->location_description ?? '')) ?>" class="form-input" maxlength="500" placeholder="مثال: الدور الأول - جناح B">
        </div>

        <div style="background:#F9FAFB;border:1px solid #E5E7EB;border-radius:8px;padding:15px;margin-bottom:20px;">
            <h4 style="margin:0 0 12px;color:#374151;font-size:14px;">ساعات التشغيل</h4>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;">
                <div>
                    <label class="form-label">وقت البدء <span style="color:#DC2626;">*</span></label>
                    <input type="time" name="operating_start" value="<?= e(old('operating_start', $opHours['start'] ?? '06:00')) ?>" class="form-input" required>
                </div>
                <div>
                    <label class="form-label">وقت الانتهاء <span style="color:#DC2626;">*</span></label>
                    <input type="time" name="operating_end" value="<?= e(old('operating_end', $opHours['end'] ?? '22:00')) ?>" class="form-input" required>
                </div>
                <div>
                    <label class="form-label">مدة الفترة (دقيقة)</label>
                    <input type="number" name="slot_minutes" value="<?= e(old('slot_minutes', (string) ($opHours['slot_minutes'] ?? 60))) ?>" class="form-input" min="15" max="240" step="15">
                </div>
            </div>
        </div>

        <div style="background:#F9FAFB;border:1px solid #E5E7EB;border-radius:8px;padding:15px;margin-bottom:20px;">
            <h4 style="margin:0 0 12px;color:#374151;font-size:14px;">إعدادات الشبكة</h4>
            <p style="font-size:12px;color:#6B7280;margin:0 0 12px;">تفعيل الشبكة يتيح إدارة المناطق بصرياً على خريطة المنشأة. اتركها فارغة أو 0 لإيقاف الشبكة.</p>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
                <div>
                    <label class="form-label">عدد الصفوف</label>
                    <input type="number" name="grid_rows" value="<?= e(old('grid_rows', (string) ($facility->pool_grid_rows ?? ''))) ?>" class="form-input" min="0" max="25" placeholder="مثال: 10">
                </div>
                <div>
                    <label class="form-label">عدد الأعمدة</label>
                    <input type="number" name="grid_cols" value="<?= e(old('grid_cols', (string) ($facility->pool_grid_cols ?? ''))) ?>" class="form-input" min="0" max="25" placeholder="مثال: 20">
                </div>
            </div>
        </div>

        <div style="display:flex;gap:10px;">
            <button type="submit" class="btn btn-primary">حفظ التعديلات</button>
            <a href="/sa/facilities/<?= (int) $facility->id ?>" class="btn btn-outline">إلغاء</a>
        </div>
    </form>
</div>

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