<?php
use App\Modules\FacilityGrids\Models\FacilityZoneSchedule;
$__template->layout('Layout.main');
$activityTypes = FacilityZoneSchedule::getActivityTypes();
?>
<?php $__template->section('title'); ?>جداول — <?= e($grid->name_ar) ?><?php $__template->endSection(); ?>

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

<div style="display:flex;align-items:center;gap:12px;margin-bottom:20px;">
    <a href="/facility-grids/<?= (int) $grid->id ?>" style="color:#6B7280;text-decoration:none;font-size:13px;"><i data-lucide="arrow-right" style="width:14px;height:14px;vertical-align:middle;"></i> <?= e($grid->name_ar) ?></a>
    <span style="color:#D1D5DB;">|</span>
    <h2 style="margin:0;font-size:18px;font-weight:800;">كل الجداول</h2>
    <span style="background:#E5E7EB;padding:2px 10px;border-radius:10px;font-size:12px;font-weight:700;"><?= count($schedules) ?></span>
</div>

<?php if (empty($schedules)): ?>
<div class="card" style="padding:40px;text-align:center;">
    <i data-lucide="calendar-off" style="width:40px;height:40px;color:#9CA3AF;margin-bottom:12px;"></i>
    <p style="color:#6B7280;font-size:14px;">لا توجد جداول بعد. افتح <a href="/facility-grids/<?= (int) $grid->id ?>" style="color:#0D7377;font-weight:600;">الشبكة</a> واستخدم منشئ الجداول.</p>
</div>
<?php else: ?>

<!-- Group by day -->
<?php
$byDay = [];
foreach ($schedules as $s) $byDay[(int) $s['day_of_week']][] = $s;
?>

<?php foreach ($byDay as $day => $daySchedules): ?>
<div class="card" style="padding:16px;margin-bottom:12px;">
    <h3 style="margin:0 0 12px;font-size:15px;font-weight:700;color:#374151;">
        <i data-lucide="calendar" style="width:16px;height:16px;vertical-align:middle;margin-left:6px;color:#6B7280;"></i>
        <?= e($dayNames[$day] ?? '') ?>
        <span style="font-size:12px;font-weight:400;color:#9CA3AF;margin-right:8px;">(<?= count($daySchedules) ?> جدول)</span>
    </h3>

    <div style="display:grid;gap:8px;">
        <?php foreach ($daySchedules as $sch): ?>
        <div style="display:flex;align-items:center;gap:12px;padding:12px;background:#F9FAFB;border-radius:8px;border-right:4px solid <?= e($sch['color'] ?? '#3B82F6') ?>;">
            <div style="flex:1;">
                <div style="font-size:14px;font-weight:700;color:#111;"><?= e($sch['schedule_name']) ?></div>
                <div style="font-size:12px;color:#6B7280;margin-top:2px;">
                    <?= substr($sch['start_time'], 0, 5) ?>–<?= substr($sch['end_time'], 0, 5) ?>
                    <?php if (!empty($sch['coach_name'])): ?> • <?= e($sch['coach_name']) ?><?php endif; ?>
                    <?php if (!empty($sch['activity_type'])): ?> • <?= e($activityTypes[$sch['activity_type']] ?? $sch['activity_type']) ?><?php endif; ?>
                </div>
                <div style="font-size:11px;color:#9CA3AF;margin-top:2px;">
                    نوع التحديد: <?= e($sch['zone_selection_type']) ?>
                    • ساري من <?= e($sch['effective_from']) ?><?= $sch['effective_to'] ? ' حتى ' . e($sch['effective_to']) : ' (مفتوح)' ?>
                </div>
            </div>
            <form method="POST" action="/facility-grids/<?= (int) $grid->id ?>/schedules/<?= (int) $sch['id'] ?>/delete" onsubmit="return confirm('حذف هذا الجدول؟');" style="margin:0;">
                <?= csrf_field() ?>
                <button type="submit" style="background:none;border:none;cursor:pointer;color:#DC2626;padding:6px;" title="حذف">
                    <i data-lucide="trash-2" style="width:16px;height:16px;"></i>
                </button>
            </form>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endforeach; ?>

<?php endif; ?>

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