<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= e($plan->plan_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/facility-grids/<?= (int) $grid->id ?>/plans" class="btn btn-outline">العودة للخطط</a>
<a href="/facility-grids/<?= (int) $grid->id ?>/schedules?plan_month=<?= e($plan->plan_month) ?>" class="btn btn-outline">إدارة الجداول</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php
$statuses = \App\Modules\FacilityGrids\Models\FacilityMonthlyPlan::getStatuses();
$statusColors = ['draft' => '#F59E0B', 'active' => '#059669', 'archived' => '#6B7280'];
$color = $statusColors[$plan->status ?? 'draft'] ?? '#6B7280';
?>

<div style="display:grid;grid-template-columns:1fr 300px;gap:20px;">
    <div>
        <div class="card" style="padding:20px;margin-bottom:20px;">
            <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
                <h3 style="margin:0;"><?= e($plan->plan_name_ar) ?></h3>
                <span style="background:<?= $color ?>20;color:<?= $color ?>;padding:4px 12px;border-radius:12px;font-size:13px;font-weight:600;">
                    <?= e($statuses[$plan->status] ?? '') ?>
                </span>
            </div>

            <div style="display:grid;grid-template-columns:repeat(4, 1fr);gap:12px;margin-bottom:20px;">
                <div style="text-align:center;background:#F0F9FF;padding:12px;border-radius:8px;">
                    <div style="font-size:24px;font-weight:700;color:#0D7377;"><?= count($schedules) ?></div>
                    <div style="font-size:12px;color:#6B7280;">جلسة مجدولة</div>
                </div>
                <div style="text-align:center;background:#F0FDF4;padding:12px;border-radius:8px;">
                    <div style="font-size:24px;font-weight:700;color:#059669;"><?= count(array_unique(array_filter(array_column($schedules, 'coach_id')))) ?></div>
                    <div style="font-size:12px;color:#6B7280;">مدرب</div>
                </div>
                <div style="text-align:center;background:#FEF3C7;padding:12px;border-radius:8px;">
                    <div style="font-size:24px;font-weight:700;color:#D97706;"><?= count(array_unique(array_filter(array_column($schedules, 'academy_id')))) ?></div>
                    <div style="font-size:12px;color:#6B7280;">أكاديمية</div>
                </div>
                <div style="text-align:center;background:#F5F3FF;padding:12px;border-radius:8px;">
                    <div style="font-size:24px;font-weight:700;color:#7C3AED;"><?= array_sum(array_column($schedules, 'max_occupants')) ?></div>
                    <div style="font-size:12px;color:#6B7280;">طاقة استيعابية</div>
                </div>
            </div>
        </div>

        <!-- Schedule by Day -->
        <div class="card" style="padding:20px;">
            <h4 style="margin-bottom:15px;">الجدول اليومي</h4>
            <?php
            $byDay = [];
            foreach ($schedules as $s) { $byDay[(int) $s['day_of_week']][] = $s; }
            ?>
            <?php foreach ($dayNames as $idx => $name): ?>
            <?php if (isset($byDay[$idx])): ?>
            <div style="margin-bottom:20px;">
                <h5 style="color:#0D7377;margin-bottom:8px;padding-bottom:5px;border-bottom:1px solid #E5E7EB;"><?= $name ?></h5>
                <div style="display:grid;gap:6px;">
                    <?php foreach ($byDay[$idx] as $s): ?>
                    <div style="display:flex;align-items:center;gap:10px;padding:8px 12px;background:#F9FAFB;border-radius:6px;border-right:3px solid <?= e($s['color'] ?? '#3B82F6') ?>;">
                        <span style="font-weight:600;min-width:90px;direction:ltr;text-align:center;font-size:13px;">
                            <?= e(substr($s['start_time'], 0, 5)) ?> — <?= e(substr($s['end_time'], 0, 5)) ?>
                        </span>
                        <span style="flex:1;"><?= e($s['schedule_name']) ?></span>
                        <?php if ($s['coach_name']): ?><span style="font-size:12px;color:#6B7280;"><?= e($s['coach_name']) ?></span><?php endif; ?>
                        <?php if ($s['academy_name']): ?><span style="font-size:11px;background:#EDE9FE;color:#7C3AED;padding:2px 6px;border-radius:4px;"><?= e($s['academy_name']) ?></span><?php endif; ?>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>
            <?php endforeach; ?>
        </div>
    </div>

    <div>
        <div class="card" style="padding:15px;">
            <h5 style="margin-bottom:12px;">معلومات الخطة</h5>
            <table style="width:100%;font-size:13px;">
                <tr><td style="color:#6B7280;padding:4px 0;">الشهر</td><td style="font-weight:600;"><?= e($plan->plan_month) ?></td></tr>
                <tr><td style="color:#6B7280;padding:4px 0;">ساعات النهار</td><td><?= e(substr($plan->day_hours_start, 0, 5)) ?> — <?= e(substr($plan->day_hours_end, 0, 5)) ?></td></tr>
                <tr><td style="color:#6B7280;padding:4px 0;">ساعات الليل</td><td><?= e(substr($plan->night_hours_start, 0, 5)) ?> — <?= e(substr($plan->night_hours_end, 0, 5)) ?></td></tr>
                <?php if ($plan->activated_at): ?><tr><td style="color:#6B7280;padding:4px 0;">تاريخ التفعيل</td><td><?= e(substr($plan->activated_at, 0, 10)) ?></td></tr><?php endif; ?>
                <?php if ($plan->cloned_from_id): ?><tr><td style="color:#6B7280;padding:4px 0;">منسوخة من</td><td>خطة #<?= (int) $plan->cloned_from_id ?></td></tr><?php endif; ?>
            </table>
            <?php if ($plan->notes): ?>
            <div style="margin-top:10px;padding:8px;background:#F9FAFB;border-radius:4px;font-size:12px;color:#6B7280;"><?= e($plan->notes) ?></div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>
