<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>المنشأة: <?= e($facility->name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<div style="display:flex;gap:8px;">
    <?php if (can('sa.facility.manage')): ?>
    <a href="/sa/facilities/<?= (int) $facility->id ?>/edit" class="btn btn-outline">تعديل</a>
    <?php endif; ?>
    <a href="/sa/facilities/<?= (int) $facility->id ?>/units" class="btn btn-outline">الوحدات</a>
    <a href="/sa/facilities/<?= (int) $facility->id ?>/brackets" class="btn btn-outline">الفترات الزمنية</a>
    <a href="/sa/facilities" class="btn btn-outline">رجوع للقائمة</a>
</div>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

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

<!-- Facility Details -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:15px;">
        <h3 style="margin:0;color:#1F2937;"><?= e($facility->name_ar) ?></h3>
        <span style="padding:4px 12px;border-radius:20px;font-size:12px;font-weight:600;<?= $facility->is_active ? 'background:#D1FAE5;color:#059669;' : 'background:#FEE2E2;color:#DC2626;' ?>">
            <?= $facility->is_active ? 'مفعّلة' : 'معطّلة' ?>
        </span>
    </div>

    <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(200px, 1fr));gap:15px;">
        <div>
            <span style="font-size:12px;color:#6B7280;">الكود</span>
            <div style="font-weight:600;font-family:monospace;"><?= e($facility->code) ?></div>
        </div>
        <div>
            <span style="font-size:12px;color:#6B7280;">النوع</span>
            <div style="font-weight:600;"><?= e($typeOptions[$facility->facility_type] ?? $facility->facility_type) ?></div>
        </div>
        <div>
            <span style="font-size:12px;color:#6B7280;">اللعبة</span>
            <div style="font-weight:600;"><?= e($discipline['name_ar'] ?? '—') ?></div>
        </div>
        <div>
            <span style="font-size:12px;color:#6B7280;">الموقع</span>
            <div style="font-weight:600;"><?= e($facility->location_description ?? '—') ?></div>
        </div>
        <div>
            <span style="font-size:12px;color:#6B7280;">ساعات التشغيل</span>
            <div style="font-weight:600;"><?= e($opHours['start'] ?? '—') ?> - <?= e($opHours['end'] ?? '—') ?></div>
        </div>
        <div>
            <span style="font-size:12px;color:#6B7280;">مدة الفترة</span>
            <div style="font-weight:600;"><?= (int) ($opHours['slot_minutes'] ?? 60) ?> دقيقة</div>
        </div>
        <?php if (!empty($facility->name_en)): ?>
        <div>
            <span style="font-size:12px;color:#6B7280;">الاسم بالإنجليزية</span>
            <div style="font-weight:600;" dir="ltr"><?= e($facility->name_en) ?></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Units Section -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
        <h4 style="margin:0;color:#1F2937;">الوحدات (<?= count($units) ?>)</h4>
        <a href="/sa/facilities/<?= (int) $facility->id ?>/units/create" class="btn btn-sm btn-primary">إضافة وحدة</a>
    </div>

    <?php if (!empty($units)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الكود</th>
                    <th>الاسم</th>
                    <th>النوع</th>
                    <th>نمط الحجز</th>
                    <th>السعة</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($units as $unit): ?>
                <tr>
                    <td style="font-family:monospace;font-size:13px;"><?= e(is_object($unit) ? $unit->code : $unit['code']) ?></td>
                    <td style="font-weight:600;"><?= e(is_object($unit) ? $unit->name_ar : $unit['name_ar']) ?></td>
                    <td><?= e($unitTypeOptions[is_object($unit) ? $unit->unit_type : $unit['unit_type']] ?? '') ?></td>
                    <td><?= e($bookingModes[is_object($unit) ? $unit->booking_mode : $unit['booking_mode']] ?? '') ?></td>
                    <td style="text-align:center;"><?= (int) (is_object($unit) ? $unit->max_capacity : $unit['max_capacity']) ?></td>
                    <td>
                        <a href="/sa/facilities/<?= (int) $facility->id ?>/units/<?= (int) (is_object($unit) ? $unit->id : $unit['id']) ?>/edit" class="btn btn-sm btn-outline">تعديل</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:#6B7280;text-align:center;padding:20px 0;margin:0;">لا توجد وحدات مسجلة</p>
    <?php endif; ?>
</div>

<!-- Time Brackets Section -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
        <h4 style="margin:0;color:#1F2937;">الفترات الزمنية (<?= count($brackets) ?>)</h4>
        <a href="/sa/facilities/<?= (int) $facility->id ?>/brackets" class="btn btn-sm btn-outline">إدارة الفترات</a>
    </div>

    <?php if (!empty($brackets)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الاسم</th>
                    <th>النوع</th>
                    <th>من</th>
                    <th>إلى</th>
                    <th>الأيام</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $dayNames = ['الأحد', 'الإثنين', 'الثلاثاء', 'الأربعاء', 'الخميس', 'الجمعة', 'السبت'];
                ?>
                <?php foreach ($brackets as $bracket): ?>
                <?php
                    $bObj = is_object($bracket) ? $bracket : (object) $bracket;
                    $days = is_string($bObj->days_of_week_json) ? json_decode($bObj->days_of_week_json, true) : ($bObj->days_of_week_json ?: []);
                    $dayLabels = array_map(fn($d) => $dayNames[(int) $d] ?? '', $days ?: []);
                ?>
                <tr>
                    <td style="font-weight:600;"><?= e($bObj->name_ar) ?></td>
                    <td><?= e($bracketTypes[$bObj->bracket_type] ?? $bObj->bracket_type) ?></td>
                    <td style="font-family:monospace;"><?= e($bObj->start_time) ?></td>
                    <td style="font-family:monospace;"><?= e($bObj->end_time) ?></td>
                    <td style="font-size:12px;"><?= e(implode('، ', $dayLabels)) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:#6B7280;text-align:center;padding:20px 0;margin:0;">لا توجد فترات زمنية</p>
    <?php endif; ?>
</div>

<!-- Training Groups at this Facility -->
<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="users" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">المجموعات المتدربة هنا</h3>
        <span style="margin-right:auto;background:#EFF6FF;color:#2563EB;padding:2px 8px;border-radius:8px;font-size:11px;"><?= count($trainingGroups) ?></span>
    </div>
    <?php if (!empty($trainingGroups)): ?>
    <?php
    $dayNames = ['الأحد', 'الاثنين', 'الثلاثاء', 'الأربعاء', 'الخميس', 'الجمعة', 'السبت'];
    ?>
    <div style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;">
            <thead>
                <tr style="background:#F9FAFB;">
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">المجموعة</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">المدرب</th>
                    <th style="padding:10px 12px;text-align:right;font-size:12px;color:#6B7280;">المواعيد</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($trainingGroups as $tg): ?>
                <tr style="border-top:1px solid #F3F4F6;">
                    <td style="padding:10px 12px;"><a href="/sa/groups/<?= (int) $tg['id'] ?>" style="color:#2563EB;font-size:13px;font-weight:600;text-decoration:none;"><?= e($tg['name_ar']) ?></a></td>
                    <td style="padding:10px 12px;font-size:13px;color:#374151;"><?= e($tg['coach_name'] ?? '—') ?></td>
                    <td style="padding:10px 12px;font-size:12px;color:#6B7280;">
                        <?php
                        $parts = explode(',', $tg['schedule_info'] ?? '');
                        foreach ($parts as $part):
                            $split = explode('|', $part);
                            if (count($split) === 2):
                        ?>
                        <span style="display:inline-block;background:#F3F4F6;padding:2px 6px;border-radius:4px;margin:1px 2px;font-size:11px;"><?= e($dayNames[(int)$split[0]] ?? '') ?> <?= e($split[1]) ?></span>
                        <?php endif; endforeach; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:#9CA3AF;text-align:center;padding:20px 0;margin:0;font-size:13px;">لا توجد مجموعات تتدرب في هذه المنشأة حالياً</p>
    <?php endif; ?>
</div>

<!-- Blackout Date Form -->
<div class="card" style="padding:20px;">
    <h4 style="margin:0 0 15px;color:#1F2937;">إضافة تاريخ إيقاف</h4>
    <form method="POST" action="/sa/facilities/<?= (int) $facility->id ?>/blackout" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <?= csrf_field() ?>
        <div>
            <label class="form-label" style="font-size:12px;">التاريخ</label>
            <input type="date" name="blackout_date" class="form-input" required min="<?= e(date('Y-m-d')) ?>">
        </div>
        <div style="flex:1;">
            <label class="form-label" style="font-size:12px;">السبب</label>
            <input type="text" name="reason" class="form-input" placeholder="سبب الإيقاف (اختياري)">
        </div>
        <button type="submit" class="btn btn-outline">إضافة إيقاف</button>
    </form>
</div>

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