<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= $mode === 'create' ? 'إنشاء جدول جديد' : 'تعديل الجدول' ?> - <?= e($playground['playground_name_ar']) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php
$action = $mode === 'create'
    ? '/playgrounds/' . (int) $playground['id'] . '/schedules'
    : '/playgrounds/' . (int) $playground['id'] . '/schedules/' . (int) $schedule['id'];
$workingDays = $schedule ? json_decode($schedule['working_days_json'] ?? '[]', true) : [];
$dayNames = ['السبت', 'الأحد', 'الإثنين', 'الثلاثاء', 'الأربعاء', 'الخميس', 'الجمعة'];
?>
<div class="container-fluid" style="max-width:700px;">
    <form method="post" action="<?= $action ?>">
        <?= csrf_field() ?>

        <div class="card mb-3">
            <div class="card-header">بيانات الجدول</div>
            <div class="card-body">
                <div class="mb-3">
                    <label class="form-label">اسم الجدول *</label>
                    <input type="text" name="plan_name_ar" class="form-control" value="<?= e($schedule['plan_name_ar'] ?? '') ?>" required>
                </div>

                <?php if ($mode === 'create'): ?>
                <div class="mb-3">
                    <label class="form-label">شهر الخطة *</label>
                    <input type="date" name="plan_month" class="form-control" value="<?= date('Y-m-01') ?>" required>
                </div>
                <?php endif; ?>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">ساعة البدء</label>
                        <input type="time" name="day_start_time" class="form-control" value="<?= e($schedule['day_start_time'] ?? '07:00:00') ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">ساعة الانتهاء</label>
                        <input type="time" name="day_end_time" class="form-control" value="<?= e($schedule['day_end_time'] ?? '22:00:00') ?>">
                    </div>
                </div>

                <div class="mb-3">
                    <label class="form-label">أيام العمل</label>
                    <div class="d-flex flex-wrap gap-3">
                        <?php for ($d = 0; $d < 7; $d++): ?>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="working_days[]" value="<?= $d ?>" id="day<?= $d ?>"
                                <?= in_array($d, $workingDays) ? 'checked' : '' ?>>
                            <label class="form-check-label" for="day<?= $d ?>"><?= $dayNames[$d] ?></label>
                        </div>
                        <?php endfor; ?>
                    </div>
                </div>

                <?php if ($mode === 'edit' && $schedule['status'] === 'draft'): ?>
                <div class="mb-3">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="status" value="active" id="activateCheck">
                        <label class="form-check-label" for="activateCheck">تفعيل الجدول</label>
                    </div>
                </div>
                <?php endif; ?>

                <div class="mb-3">
                    <label class="form-label">ملاحظات</label>
                    <textarea name="notes" class="form-control" rows="3"><?= e($schedule['notes'] ?? '') ?></textarea>
                </div>
            </div>
        </div>

        <button type="submit" class="btn btn-success btn-lg">حفظ</button>
        <a href="/playgrounds/<?= (int) $playground['id'] ?>/schedules" class="btn btn-secondary btn-lg">إلغاء</a>
    </form>
</div>
<?php $__template->endSection(); ?>
