<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الفترات الزمنية: <?= e($facility->name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/sa/facilities/<?= (int) $facility->id ?>" class="btn btn-outline">رجوع للمنشأة</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<?php
$dayNames = [
    0 => 'الأحد',
    1 => 'الإثنين',
    2 => 'الثلاثاء',
    3 => 'الأربعاء',
    4 => 'الخميس',
    5 => 'الجمعة',
    6 => 'السبت',
];
?>

<div class="card" style="padding:10px 15px;margin-bottom:15px;background:#F9FAFB;">
    <span style="font-size:13px;color:#6B7280;">المنشأة:</span>
    <a href="/sa/facilities/<?= (int) $facility->id ?>" style="color:#1e3a5f;font-weight:600;margin-right:5px;"><?= e($facility->name_ar) ?></a>
    <span style="font-size:12px;color:#9CA3AF;">(<?= e($facility->code) ?>)</span>
</div>

<!-- Add Bracket Form -->
<div class="card" style="padding:20px;margin-bottom:20px;">
    <h4 style="margin:0 0 15px;color:#1F2937;">إضافة فترة زمنية جديدة</h4>

    <form method="POST" action="/sa/facilities/<?= (int) $facility->id ?>/brackets">
        <?= csrf_field() ?>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">اسم الفترة <span style="color:#DC2626;">*</span></label>
                <input type="text" name="name_ar" value="<?= e(old('name_ar', '')) ?>" class="form-input" required maxlength="100" placeholder="مثال: فترة الذروة الصباحية">
            </div>
            <div>
                <label class="form-label">نوع الفترة <span style="color:#DC2626;">*</span></label>
                <select name="bracket_type" class="form-select" required>
                    <option value="">اختر النوع</option>
                    <?php foreach ($bracketTypes as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= old('bracket_type', '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">وقت البدء <span style="color:#DC2626;">*</span></label>
                <input type="time" name="start_time" value="<?= e(old('start_time', '')) ?>" class="form-input" required>
            </div>
            <div>
                <label class="form-label">وقت الانتهاء <span style="color:#DC2626;">*</span></label>
                <input type="time" name="end_time" value="<?= e(old('end_time', '')) ?>" class="form-input" required>
            </div>
        </div>

        <div style="margin-bottom:15px;">
            <label class="form-label">أيام الأسبوع <span style="color:#DC2626;">*</span></label>
            <div style="display:flex;gap:12px;flex-wrap:wrap;padding:10px;background:#F9FAFB;border:1px solid #E5E7EB;border-radius:6px;">
                <?php foreach ($dayNames as $dayNum => $dayLabel): ?>
                <label style="display:flex;align-items:center;gap:5px;cursor:pointer;font-size:13px;">
                    <input type="checkbox" name="days_of_week[]" value="<?= $dayNum ?>" style="width:16px;height:16px;">
                    <?= e($dayLabel) ?>
                </label>
                <?php endforeach; ?>
            </div>
        </div>

        <button type="submit" class="btn btn-primary">إضافة الفترة</button>
    </form>
</div>

<!-- Existing Brackets -->
<div class="card" style="padding:20px;">
    <h4 style="margin:0 0 15px;color:#1F2937;">الفترات الزمنية الحالية (<?= count($brackets) ?>)</h4>

    <?php if (!empty($brackets)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الاسم</th>
                    <th>النوع</th>
                    <th>من</th>
                    <th>إلى</th>
                    <th>الأيام</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($brackets as $bracket): ?>
                <?php
                    $b = is_object($bracket) ? $bracket : (object) $bracket;
                    $days = is_string($b->days_of_week_json) ? json_decode($b->days_of_week_json, true) : ($b->days_of_week_json ?: []);
                    $dayLabels = array_map(fn($d) => $dayNames[(int) $d] ?? '', $days ?: []);
                    $typeColor = match($b->bracket_type) {
                        'peak'     => '#DC2626',
                        'off_peak' => '#059669',
                        'weekend'  => '#7C3AED',
                        'holiday'  => '#D97706',
                        default    => '#6B7280',
                    };
                ?>
                <tr>
                    <td style="font-weight:600;"><?= e($b->name_ar) ?></td>
                    <td>
                        <span style="color:<?= $typeColor ?>;font-weight:600;"><?= e($bracketTypes[$b->bracket_type] ?? $b->bracket_type) ?></span>
                    </td>
                    <td style="font-family:monospace;font-size:13px;"><?= e($b->start_time) ?></td>
                    <td style="font-family:monospace;font-size:13px;"><?= e($b->end_time) ?></td>
                    <td style="font-size:12px;"><?= e(implode('، ', $dayLabels)) ?></td>
                    <td>
                        <form method="POST" action="/sa/facilities/<?= (int) $facility->id ?>/brackets/<?= (int) $b->id ?>/delete" style="display:inline;" onsubmit="return confirm('هل أنت متأكد من حذف هذه الفترة؟');">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;border-color:#DC2626;">حذف</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:#6B7280;text-align:center;padding:20px 0;margin:0;">لا توجد فترات زمنية مسجلة. أضف فترة جديدة من النموذج أعلاه.</p>
    <?php endif; ?>
</div>

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