<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الخطط الشهرية — <?= e($grid->name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<?php if (can('facility.manage')): ?>
<a href="/facility-grids/<?= (int) $grid->id ?>/plans/create" class="btn btn-primary">+ خطة جديدة</a>
<?php endif; ?>
<a href="/facility-grids/<?= (int) $grid->id ?>" 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'];
$monthNames = ['01'=>'يناير','02'=>'فبراير','03'=>'مارس','04'=>'أبريل','05'=>'مايو','06'=>'يونيو','07'=>'يوليو','08'=>'أغسطس','09'=>'سبتمبر','10'=>'أكتوبر','11'=>'نوفمبر','12'=>'ديسمبر'];
?>

<?php if (empty($plans)): ?>
<div class="card" style="text-align:center;padding:60px;">
    <p style="color:#6B7280;font-size:16px;margin-bottom:15px;">لا توجد خطط شهرية بعد</p>
    <a href="/facility-grids/<?= (int) $grid->id ?>/plans/create" class="btn btn-primary">إنشاء أول خطة</a>
</div>
<?php else: ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(320px, 1fr));gap:16px;">
<?php foreach ($plans as $p):
    $parts = explode('-', $p['plan_month'] ?? '');
    $monthLabel = ($monthNames[$parts[1] ?? ''] ?? '') . ' ' . ($parts[0] ?? '');
    $color = $statusColors[$p['status'] ?? 'draft'] ?? '#6B7280';
?>
<div class="card" style="padding:20px;border-right:4px solid <?= $color ?>;">
    <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:12px;">
        <div>
            <h4 style="margin:0;color:#1F2937;"><?= e($p['plan_name_ar']) ?></h4>
            <span style="font-size:13px;color:#6B7280;"><?= e($monthLabel) ?></span>
        </div>
        <span style="background:<?= $color ?>20;color:<?= $color ?>;padding:3px 10px;border-radius:12px;font-size:12px;font-weight:600;">
            <?= e($statuses[$p['status'] ?? 'draft'] ?? '') ?>
        </span>
    </div>

    <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:8px;margin-bottom:15px;font-size:13px;">
        <div style="text-align:center;background:#F9FAFB;padding:8px;border-radius:6px;">
            <div style="font-weight:700;color:#0D7377;"><?= (int) ($p['total_sessions'] ?? 0) ?></div>
            <div style="color:#6B7280;">جلسة</div>
        </div>
        <div style="text-align:center;background:#F9FAFB;padding:8px;border-radius:6px;">
            <div style="font-weight:700;color:#0D7377;"><?= (int) ($p['total_coaches'] ?? 0) ?></div>
            <div style="color:#6B7280;">مدرب</div>
        </div>
        <div style="text-align:center;background:#F9FAFB;padding:8px;border-radius:6px;">
            <div style="font-weight:700;color:#0D7377;"><?= (int) ($p['total_trainees'] ?? 0) ?></div>
            <div style="color:#6B7280;">متدرب</div>
        </div>
    </div>

    <div style="font-size:12px;color:#6B7280;margin-bottom:12px;">
        ساعات العمل: <?= e(substr($p['day_hours_start'] ?? '06:00', 0, 5)) ?> — <?= e(substr($p['night_hours_end'] ?? '22:00', 0, 5)) ?>
    </div>

    <div style="display:flex;gap:6px;flex-wrap:wrap;">
        <a href="/facility-grids/<?= (int) $grid->id ?>/plans/<?= (int) $p['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
        <?php if ($p['status'] === 'draft'): ?>
        <form method="POST" action="/facility-grids/<?= (int) $grid->id ?>/plans/<?= (int) $p['id'] ?>/activate" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-primary" onclick="return confirm('تفعيل هذه الخطة؟ سيتم أرشفة الخطة النشطة الحالية.')">تفعيل</button></form>
        <?php endif; ?>
        <?php if ($p['status'] === 'active'): ?>
        <form method="POST" action="/facility-grids/<?= (int) $grid->id ?>/plans/<?= (int) $p['id'] ?>/archive" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;" onclick="return confirm('أرشفة الخطة؟')">أرشفة</button></form>
        <?php endif; ?>
    </div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php $__template->endSection(); ?>
