<?php $__template->layout('Layout.main'); ?>
<?php $isEdit = $schedule !== null; ?>
<?php $__template->section('title'); ?><?= $isEdit ? 'تعديل جدول العمل' : 'إضافة جدول عمل' ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/schedules" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php
$daysConfig = [];
if ($isEdit && $schedule->days_config_json) {
    $daysConfig = json_decode($schedule->days_config_json, true) ?: [];
}
$dayLabels = ['saturday'=>'السبت','sunday'=>'الأحد','monday'=>'الاثنين','tuesday'=>'الثلاثاء','wednesday'=>'الأربعاء','thursday'=>'الخميس','friday'=>'الجمعة'];
?>
<form method="POST" action="<?= $isEdit ? '/hr/schedules/' . (int) $schedule->id : '/hr/schedules' ?>">
    <?= csrf_field() ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">البيانات الأساسية</h3></div>
        <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:16px;">
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">الاسم بالعربي <span style="color:#DC2626;">*</span></label><input type="text" name="name_ar" value="<?= e(old('name_ar') ?? ($isEdit ? $schedule->name_ar : '')) ?>" class="form-control" required></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">الاسم بالإنجليزي</label><input type="text" name="name_en" value="<?= e(old('name_en') ?? ($isEdit ? $schedule->name_en : '')) ?>" class="form-control"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">الكود <span style="color:#DC2626;">*</span></label><input type="text" name="code" value="<?= e(old('code') ?? ($isEdit ? $schedule->code : '')) ?>" class="form-control" required></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">النوع</label>
                <select name="schedule_type" class="form-control">
                    <option value="fixed" <?= (old('schedule_type') ?? ($isEdit ? $schedule->schedule_type : '')) === 'fixed' ? 'selected' : '' ?>>ثابت</option>
                    <option value="rotating" <?= (old('schedule_type') ?? ($isEdit ? $schedule->schedule_type : '')) === 'rotating' ? 'selected' : '' ?>>دوري</option>
                    <option value="flexible" <?= (old('schedule_type') ?? ($isEdit ? $schedule->schedule_type : '')) === 'flexible' ? 'selected' : '' ?>>مرن</option>
                </select>
            </div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">أيام العمل/أسبوع</label><input type="number" name="working_days_per_week" value="<?= e(old('working_days_per_week') ?? ($isEdit ? $schedule->working_days_per_week : '6')) ?>" class="form-control" min="1" max="7"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">ساعات/يوم</label><input type="text" name="hours_per_day" value="<?= e(old('hours_per_day') ?? ($isEdit ? $schedule->hours_per_day : '8.00')) ?>" class="form-control"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">ساعات/أسبوع</label><input type="text" name="hours_per_week" value="<?= e(old('hours_per_week') ?? ($isEdit ? $schedule->hours_per_week : '48.00')) ?>" class="form-control"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">ساعات رمضان/يوم</label><input type="text" name="ramadan_hours_per_day" value="<?= e(old('ramadan_hours_per_day') ?? ($isEdit ? $schedule->ramadan_hours_per_day : '')) ?>" class="form-control" placeholder="7.00"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;"><input type="checkbox" name="is_night_shift" value="1" <?= (old('is_night_shift') ?? ($isEdit ? $schedule->is_night_shift : 0)) ? 'checked' : '' ?>> وردية ليلية</label></div>
        </div>
    </div>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">أيام العمل</h3></div>
        <div style="padding:16px;">
            <?php foreach ($dayLabels as $dayKey => $dayName): ?>
            <?php $dc = $daysConfig[$dayKey] ?? ['is_working' => false, 'start_time' => '09:00', 'end_time' => '17:00']; ?>
            <div style="display:flex;gap:12px;align-items:center;margin-bottom:8px;">
                <label style="min-width:80px;"><input type="checkbox" name="day_<?= $dayKey ?>_working" value="1" <?= ($dc['is_working'] ?? false) ? 'checked' : '' ?>> <?= $dayName ?></label>
                <input type="time" name="day_<?= $dayKey ?>_start" value="<?= e($dc['start_time'] ?? '09:00') ?>" class="form-control" style="max-width:120px;">
                <span>-</span>
                <input type="time" name="day_<?= $dayKey ?>_end" value="<?= e($dc['end_time'] ?? '17:00') ?>" class="form-control" style="max-width:120px;">
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <div style="display:flex;gap:10px;justify-content:flex-end;">
        <a href="/hr/schedules" class="btn btn-secondary">إلغاء</a>
        <button type="submit" class="btn btn-primary"><?= $isEdit ? 'تحديث' : 'حفظ' ?></button>
    </div>
</form>
<?php $__template->endSection(); ?>
