<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>قواعد التسعير<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/sa/pricing" class="btn btn-outline">← العودة</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/sa/pricing/rules" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div>
            <label class="form-label" style="font-size:12px;">المرفق</label>
            <select name="facility_id" class="form-select" style="min-width:180px;" onchange="this.form.submit()">
                <option value="">اختر المرفق</option>
                <?php foreach ($facilities as $f): ?>
                    <option value="<?= (int) $f['id'] ?>" <?= $selectedFacility == $f['id'] ? 'selected' : '' ?>><?= e($f['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php if (!empty($units)): ?>
        <div>
            <label class="form-label" style="font-size:12px;">الوحدة</label>
            <select name="unit_id" class="form-select" style="min-width:150px;" onchange="this.form.submit()">
                <option value="">اختر الوحدة</option>
                <?php foreach ($units as $u): ?>
                    <option value="<?= (int) $u['id'] ?>" <?= $selectedUnit == $u['id'] ? 'selected' : '' ?>><?= e($u['name_ar']) ?> (<?= e($u['booking_mode']) ?>)</option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php endif; ?>
    </form>
</div>

<?php if ($selectedUnit > 0): ?>
<div class="card" style="margin-bottom:20px;">
    <h4 style="margin-bottom:15px;">القواعد الحالية</h4>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>الفترة</th>
                    <th>النوع</th>
                    <th>الحجم (من-إلى)</th>
                    <th>سعر العضو</th>
                    <th>سعر غير العضو</th>
                    <th>من تاريخ</th>
                    <th>إلى تاريخ</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($rules)): ?>
                    <tr><td colspan="8" style="text-align:center;color:#9CA3AF;">لا توجد قواعد تسعير لهذه الوحدة</td></tr>
                <?php else: ?>
                    <?php foreach ($rules as $r): ?>
                        <tr>
                            <td><?= e($r['bracket_name']) ?><br><small><?= e($r['bracket_start']) ?> - <?= e($r['bracket_end']) ?></small></td>
                            <td><?= e($r['bracket_type']) ?></td>
                            <td><?= (int) $r['group_size_min'] ?> - <?= (int) $r['group_size_max'] ?></td>
                            <td><strong><?= number_format((float) $r['price_per_person_member'], 2) ?></strong></td>
                            <td><?= number_format((float) $r['price_per_person_nonmember'], 2) ?></td>
                            <td><?= e($r['effective_from']) ?></td>
                            <td><?= $r['effective_to'] ? e($r['effective_to']) : '—' ?></td>
                            <td>
                                <?php if (can('sa.pricing.manage')): ?>
                                <form method="POST" action="/sa/pricing/rules/<?= (int) $r['id'] ?>/deactivate" style="display:inline;">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('تعطيل هذه القاعدة؟')">تعطيل</button>
                                </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (can('sa.pricing.manage')): ?>
<div class="card">
    <h4 style="margin-bottom:15px;">إضافة قاعدة تسعير جديدة</h4>
    <form method="POST" action="/sa/pricing/rules">
        <?= csrf_field() ?>
        <input type="hidden" name="facility_unit_id" value="<?= (int) $selectedUnit ?>">

        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:15px;">
            <div>
                <label class="form-label">الفترة الزمنية</label>
                <select name="time_bracket_id" class="form-select" required>
                    <option value="">اختر</option>
                    <?php foreach ($brackets as $b): ?>
                        <option value="<?= (int) $b['id'] ?>"><?= e($b['name_ar']) ?> (<?= e($b['start_time']) ?> - <?= e($b['end_time']) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label">حجم المجموعة (من)</label>
                <input type="number" name="group_size_min" class="form-input" min="1" step="1" value="<?= e(old('group_size_min', '1')) ?>" required>
            </div>
            <div>
                <label class="form-label">حجم المجموعة (إلى)</label>
                <input type="number" name="group_size_max" class="form-input" min="1" step="1" value="<?= e(old('group_size_max', '1')) ?>" required>
            </div>
            <div>
                <label class="form-label">سعر العضو (للفرد/ساعة)</label>
                <input type="number" name="price_per_person_member" class="form-input" step="0.01" min="0" value="<?= e(old('price_per_person_member', '')) ?>" required>
            </div>
            <div>
                <label class="form-label">سعر غير العضو (للفرد/ساعة)</label>
                <input type="number" name="price_per_person_nonmember" class="form-input" step="0.01" min="0" value="<?= e(old('price_per_person_nonmember', '')) ?>" required>
            </div>
            <div>
                <label class="form-label">تاريخ البداية</label>
                <input type="date" name="effective_from" class="form-input" value="<?= e(old('effective_from', date('Y-m-d'))) ?>" required>
            </div>
            <div>
                <label class="form-label">تاريخ الانتهاء (اختياري)</label>
                <input type="date" name="effective_to" class="form-input" value="<?= e(old('effective_to', '')) ?>">
            </div>
        </div>

        <div style="margin-top:20px;">
            <button type="submit" class="btn btn-primary">إضافة القاعدة</button>
        </div>
    </form>
</div>
<?php endif; ?>
<?php endif; ?>

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