<?php
$__template->layout('Layout.main');
$isEdit = $discount !== null;
$__template->section('title');
echo $isEdit ? 'تعديل خصم لائحي' : 'إضافة خصم لائحي';
$__template->endSection();
?>

<?php $__template->section('page_actions'); ?>
<a href="/pricing/regulatory-discounts" class="btn btn-outline">
    <i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة
</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card" style="max-width:900px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0D7377;font-size:15px;">
            <i data-lucide="scale" style="width:16px;height:16px;vertical-align:middle;margin-left:6px;"></i>
            <?= $isEdit ? 'تعديل الخصم اللائحي' : 'إضافة خصم لائحي جديد' ?>
        </h3>
    </div>
    <form method="POST" action="<?= $isEdit ? '/pricing/regulatory-discounts/' . (int) $discount['id'] : '/pricing/regulatory-discounts' ?>" style="padding:20px;">
        <?= csrf_field() ?>

        <!-- Basic Info -->
        <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
            <div class="form-group">
                <label class="form-label">رقم المادة <span style="color:red;">*</span></label>
                <input type="text" name="article_number" value="<?= e($discount['article_number'] ?? '') ?>" class="form-input" placeholder="مثال: 97" required>
            </div>
            <div class="form-group" style="grid-column:span 2;">
                <label class="form-label">اسم الخصم بالعربية <span style="color:red;">*</span></label>
                <input type="text" name="name_ar" value="<?= e($discount['name_ar'] ?? '') ?>" class="form-input" placeholder="مثال: خصم أعضاء فرع السادس لفرع شيراتون" required>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div class="form-group">
                <label class="form-label">اسم الخصم بالإنجليزية</label>
                <input type="text" name="name_en" value="<?= e($discount['name_en'] ?? '') ?>" class="form-input" placeholder="Optional">
            </div>
            <div class="form-group">
                <label class="form-label">نوع الاستحقاق <span style="color:red;">*</span></label>
                <select name="eligibility_type" id="eligibility_type" class="form-input" required>
                    <option value="cross_branch_member" <?= ($discount['eligibility_type'] ?? '') === 'cross_branch_member' ? 'selected' : '' ?>>عضو فرع آخر (مادة 97)</option>
                    <option value="government_employee" <?= ($discount['eligibility_type'] ?? '') === 'government_employee' ? 'selected' : '' ?>>موظف حكومي (مادة 98)</option>
                    <option value="ministry_youth_employee" <?= ($discount['eligibility_type'] ?? '') === 'ministry_youth_employee' ? 'selected' : '' ?>>موظف وزارة الشباب (مادة 98/99)</option>
                    <option value="board_of_trustees" <?= ($discount['eligibility_type'] ?? '') === 'board_of_trustees' ? 'selected' : '' ?>>عضو مجلس أمناء (مادة 100)</option>
                    <option value="club_employee" <?= ($discount['eligibility_type'] ?? '') === 'club_employee' ? 'selected' : '' ?>>موظف النادي (مادة 102)</option>
                    <option value="group_membership" <?= ($discount['eligibility_type'] ?? '') === 'group_membership' ? 'selected' : '' ?>>عضوية مجمعة (مادة 110)</option>
                </select>
            </div>
        </div>

        <!-- Discount Value -->
        <div id="section-discount-value" style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
            <div class="form-group">
                <label class="form-label">نسبة الخصم (%) <span style="color:red;">*</span></label>
                <input type="number" name="discount_percentage" step="0.01" min="0" max="100" value="<?= e($discount['discount_percentage'] ?? '0') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">الحد الأقصى للخصم (%)</label>
                <input type="number" name="max_discount_percentage" step="0.01" min="0" max="100" value="<?= e($discount['max_discount_percentage'] ?? '') ?>" class="form-input" placeholder="اختياري — لمادة 102">
            </div>
            <div class="form-group">
                <label class="form-label">يُطبَّق على</label>
                <select name="applies_to" class="form-input">
                    <option value="membership_fee" <?= ($discount['applies_to'] ?? 'membership_fee') === 'membership_fee' ? 'selected' : '' ?>>رسوم العضوية</option>
                    <option value="subscription" <?= ($discount['applies_to'] ?? '') === 'subscription' ? 'selected' : '' ?>>الاشتراك</option>
                    <option value="all" <?= ($discount['applies_to'] ?? '') === 'all' ? 'selected' : '' ?>>الكل</option>
                </select>
            </div>
        </div>

        <!-- Branch Selection -->
        <div id="section-branches" style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div class="form-group">
                <label class="form-label">فرع المصدر (عضوية العضو الحالية)</label>
                <select name="source_branch_id" class="form-input">
                    <option value="">— غير محدد —</option>
                    <?php foreach ($branches as $b): ?>
                    <option value="<?= (int) $b['id'] ?>" <?= ((int) ($discount['source_branch_id'] ?? 0)) === (int) $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">الفرع المستهدف (المراد الانضمام إليه)</label>
                <select name="target_branch_id" class="form-input">
                    <option value="">— الكل —</option>
                    <?php foreach ($branches as $b): ?>
                    <option value="<?= (int) $b['id'] ?>" <?= ((int) ($discount['target_branch_id'] ?? 0)) === (int) $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <!-- Employee-specific -->
        <div id="section-employee" style="display:grid;grid-template-columns:1fr;gap:15px;margin-bottom:20px;">
            <div class="form-group">
                <label class="form-label">الحد الأدنى لسنوات الخدمة</label>
                <input type="number" name="min_service_years" min="0" value="<?= e($discount['min_service_years'] ?? '0') ?>" class="form-input" placeholder="مثال: 5 (لمادة 102)">
            </div>
        </div>

        <!-- Installment -->
        <div style="padding:15px;background:#F9FAFB;border-radius:8px;margin-bottom:20px;">
            <label style="display:flex;align-items:center;gap:8px;cursor:pointer;margin-bottom:12px;">
                <input type="checkbox" name="allows_installment" value="1" id="allows_installment" <?= ($discount['allows_installment'] ?? 0) ? 'checked' : '' ?>>
                <span style="font-weight:600;color:#374151;">يسمح بالتقسيط</span>
            </label>
            <div id="section-installment" style="display:grid;grid-template-columns:1fr 1fr;gap:15px;<?= ($discount['allows_installment'] ?? 0) ? '' : 'display:none;' ?>">
                <div class="form-group">
                    <label class="form-label">أقصى مدة تقسيط (سنوات)</label>
                    <input type="number" name="installment_max_years" min="1" max="10" value="<?= e($discount['installment_max_years'] ?? '2') ?>" class="form-input">
                </div>
                <div class="form-group">
                    <label class="form-label">نسبة الفائدة على التقسيط (%)</label>
                    <input type="number" name="installment_interest_rate" step="0.01" min="0" value="<?= e($discount['installment_interest_rate'] ?? '0') ?>" class="form-input" placeholder="0 = بدون فائدة">
                </div>
            </div>
        </div>

        <!-- Group Tiers (Article 110) -->
        <div id="section-group-tiers" style="padding:15px;background:#EEF2FF;border-radius:8px;margin-bottom:20px;<?= ($discount['eligibility_type'] ?? '') === 'group_membership' ? '' : 'display:none;' ?>">
            <h4 style="color:#4F46E5;margin:0 0 12px;font-size:14px;">
                <i data-lucide="layers" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i>
                شرائح الخصم حسب العدد (مادة 110)
            </h4>
            <div id="tiers-container">
                <?php if (!empty($tiers)): ?>
                    <?php foreach ($tiers as $idx => $tier): ?>
                    <div class="tier-row" style="display:grid;grid-template-columns:1fr 1fr 1fr 40px;gap:10px;margin-bottom:8px;align-items:end;">
                        <div class="form-group" style="margin:0;">
                            <label class="form-label" style="font-size:11px;">من (عدد)</label>
                            <input type="number" name="tier_min[]" value="<?= (int) $tier['min_quantity'] ?>" class="form-input" min="1">
                        </div>
                        <div class="form-group" style="margin:0;">
                            <label class="form-label" style="font-size:11px;">إلى (عدد)</label>
                            <input type="number" name="tier_max[]" value="<?= $tier['max_quantity'] ? (int) $tier['max_quantity'] : '' ?>" class="form-input" placeholder="مفتوح">
                        </div>
                        <div class="form-group" style="margin:0;">
                            <label class="form-label" style="font-size:11px;">نسبة الخصم %</label>
                            <input type="number" name="tier_pct[]" value="<?= e($tier['discount_percentage']) ?>" step="0.01" class="form-input">
                        </div>
                        <button type="button" class="btn btn-sm" style="color:#DC2626;padding:6px;" onclick="this.closest('.tier-row').remove();">✕</button>
                    </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <div class="tier-row" style="display:grid;grid-template-columns:1fr 1fr 1fr 40px;gap:10px;margin-bottom:8px;align-items:end;">
                        <div class="form-group" style="margin:0;">
                            <label class="form-label" style="font-size:11px;">من (عدد)</label>
                            <input type="number" name="tier_min[]" value="5" class="form-input" min="1">
                        </div>
                        <div class="form-group" style="margin:0;">
                            <label class="form-label" style="font-size:11px;">إلى (عدد)</label>
                            <input type="number" name="tier_max[]" value="10" class="form-input" placeholder="مفتوح">
                        </div>
                        <div class="form-group" style="margin:0;">
                            <label class="form-label" style="font-size:11px;">نسبة الخصم %</label>
                            <input type="number" name="tier_pct[]" value="3" step="0.01" class="form-input">
                        </div>
                        <button type="button" class="btn btn-sm" style="color:#DC2626;padding:6px;" onclick="this.closest('.tier-row').remove();">✕</button>
                    </div>
                    <div class="tier-row" style="display:grid;grid-template-columns:1fr 1fr 1fr 40px;gap:10px;margin-bottom:8px;align-items:end;">
                        <div class="form-group" style="margin:0;"><input type="number" name="tier_min[]" value="11" class="form-input" min="1"></div>
                        <div class="form-group" style="margin:0;"><input type="number" name="tier_max[]" value="20" class="form-input"></div>
                        <div class="form-group" style="margin:0;"><input type="number" name="tier_pct[]" value="7" step="0.01" class="form-input"></div>
                        <button type="button" class="btn btn-sm" style="color:#DC2626;padding:6px;" onclick="this.closest('.tier-row').remove();">✕</button>
                    </div>
                    <div class="tier-row" style="display:grid;grid-template-columns:1fr 1fr 1fr 40px;gap:10px;margin-bottom:8px;align-items:end;">
                        <div class="form-group" style="margin:0;"><input type="number" name="tier_min[]" value="21" class="form-input" min="1"></div>
                        <div class="form-group" style="margin:0;"><input type="number" name="tier_max[]" value="" class="form-input" placeholder="مفتوح"></div>
                        <div class="form-group" style="margin:0;"><input type="number" name="tier_pct[]" value="10" step="0.01" class="form-input"></div>
                        <button type="button" class="btn btn-sm" style="color:#DC2626;padding:6px;" onclick="this.closest('.tier-row').remove();">✕</button>
                    </div>
                <?php endif; ?>
            </div>
            <button type="button" class="btn btn-sm btn-outline" style="margin-top:8px;" onclick="addTierRow()">
                <i data-lucide="plus" style="width:12px;height:12px;vertical-align:middle;"></i> إضافة شريحة
            </button>
        </div>

        <!-- Dates and Board Decision -->
        <div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
            <div class="form-group">
                <label class="form-label">سارٍ من</label>
                <input type="date" name="effective_from" value="<?= e($discount['effective_from'] ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">سارٍ حتى</label>
                <input type="date" name="effective_to" value="<?= e($discount['effective_to'] ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">رقم قرار مجلس الإدارة</label>
                <input type="text" name="board_decision_number" value="<?= e($discount['board_decision_number'] ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">تاريخ القرار</label>
                <input type="date" name="board_decision_date" value="<?= e($discount['board_decision_date'] ?? '') ?>" class="form-input">
            </div>
        </div>

        <!-- Notes -->
        <div class="form-group" style="margin-bottom:20px;">
            <label class="form-label">ملاحظات</label>
            <textarea name="notes" class="form-input" rows="3" placeholder="وصف أو ملاحظات إضافية..."><?= e($discount['notes'] ?? '') ?></textarea>
        </div>

        <!-- Description -->
        <div class="form-group" style="margin-bottom:20px;">
            <label class="form-label">وصف تفصيلي (يظهر عند التطبيق)</label>
            <textarea name="description" class="form-input" rows="2"><?= e($discount['description'] ?? '') ?></textarea>
        </div>

        <?php if ($isEdit): ?>
        <div class="form-group" style="margin-bottom:20px;">
            <label class="form-label">الحالة</label>
            <select name="is_active" class="form-input" style="max-width:200px;">
                <option value="1" <?= ($discount['is_active'] ?? 1) ? 'selected' : '' ?>>فعال</option>
                <option value="0" <?= !($discount['is_active'] ?? 1) ? 'selected' : '' ?>>معطل</option>
            </select>
        </div>
        <?php endif; ?>

        <div style="border-top:1px solid #E5E7EB;padding-top:15px;display:flex;gap:10px;">
            <button type="submit" class="btn btn-primary">
                <i data-lucide="check" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i>
                <?= $isEdit ? 'حفظ التعديلات' : 'إضافة الخصم' ?>
            </button>
            <a href="/pricing/regulatory-discounts" class="btn btn-outline">إلغاء</a>
        </div>
    </form>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();

    const typeSelect = document.getElementById('eligibility_type');
    const sectionBranches = document.getElementById('section-branches');
    const sectionEmployee = document.getElementById('section-employee');
    const sectionGroupTiers = document.getElementById('section-group-tiers');
    const sectionDiscountValue = document.getElementById('section-discount-value');
    const installmentCheckbox = document.getElementById('allows_installment');
    const sectionInstallment = document.getElementById('section-installment');

    function updateVisibility() {
        const type = typeSelect.value;
        sectionBranches.style.display = (type === 'cross_branch_member') ? 'grid' : 'none';
        sectionEmployee.style.display = (type === 'club_employee') ? 'grid' : 'none';
        sectionGroupTiers.style.display = (type === 'group_membership') ? 'block' : 'none';
        sectionDiscountValue.style.display = (type === 'group_membership') ? 'none' : 'grid';
    }

    typeSelect.addEventListener('change', updateVisibility);
    updateVisibility();

    installmentCheckbox.addEventListener('change', function() {
        sectionInstallment.style.display = this.checked ? 'grid' : 'none';
    });
});

function addTierRow() {
    const container = document.getElementById('tiers-container');
    const row = document.createElement('div');
    row.className = 'tier-row';
    row.style.cssText = 'display:grid;grid-template-columns:1fr 1fr 1fr 40px;gap:10px;margin-bottom:8px;align-items:end;';
    row.innerHTML = `
        <div class="form-group" style="margin:0;"><input type="number" name="tier_min[]" class="form-input" min="1" placeholder="من"></div>
        <div class="form-group" style="margin:0;"><input type="number" name="tier_max[]" class="form-input" placeholder="إلى (مفتوح)"></div>
        <div class="form-group" style="margin:0;"><input type="number" name="tier_pct[]" step="0.01" class="form-input" placeholder="نسبة %"></div>
        <button type="button" class="btn btn-sm" style="color:#DC2626;padding:6px;" onclick="this.closest('.tier-row').remove();">✕</button>
    `;
    container.appendChild(row);
}
</script>
<?php $__template->endSection(); ?>
