<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= $mode === 'edit' ? 'تعديل مؤسسة' : 'إضافة مؤسسة جديدة' ?><?php $__template->endSection(); ?>

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

<?php $__template->section('content'); ?>

<div class="card" style="padding:20px;max-width:700px;">
    <form method="POST" action="<?= $mode === 'edit' ? '/sa/institutions/' . (int)$institution['id'] : '/sa/institutions' ?>">
        <?= 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="code" class="form-control" value="<?= e($institution['code'] ?? old('code', '')) ?>" required maxlength="30">
            </div>
            <div>
                <label class="form-label">النوع <span style="color:#DC2626;">*</span></label>
                <select name="institution_type" class="form-select" required>
                    <?php
                    $types = ['school'=>'مدرسة','university'=>'جامعة','company'=>'شركة','club'=>'نادي','government'=>'حكومي','other'=>'أخرى'];
                    $current = $institution['institution_type'] ?? 'school';
                    foreach ($types as $val => $label): ?>
                    <option value="<?= $val ?>" <?= $current === $val ? 'selected' : '' ?>><?= $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="text" name="name_ar" class="form-control" value="<?= e($institution['name_ar'] ?? old('name_ar', '')) ?>" required>
            </div>
            <div>
                <label class="form-label">الاسم بالإنجليزي</label>
                <input type="text" name="name_en" class="form-control" value="<?= e($institution['name_en'] ?? old('name_en', '')) ?>">
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">المسؤول</label>
                <input type="text" name="contact_person" class="form-control" value="<?= e($institution['contact_person'] ?? '') ?>">
            </div>
            <div>
                <label class="form-label">الهاتف</label>
                <input type="text" name="phone" class="form-control" value="<?= e($institution['phone'] ?? '') ?>">
            </div>
        </div>

        <div style="margin-bottom:15px;">
            <label class="form-label">البريد الإلكتروني</label>
            <input type="email" name="email" class="form-control" value="<?= e($institution['email'] ?? '') ?>">
        </div>

        <div style="margin-bottom:15px;">
            <label class="form-label">العنوان</label>
            <textarea name="address" class="form-control" rows="2"><?= e($institution['address'] ?? '') ?></textarea>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">بداية العقد</label>
                <input type="date" name="contract_start" class="form-control" value="<?= e($institution['contract_start'] ?? '') ?>">
            </div>
            <div>
                <label class="form-label">نهاية العقد</label>
                <input type="date" name="contract_end" class="form-control" value="<?= e($institution['contract_end'] ?? '') ?>">
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">سعر الساعة</label>
                <input type="number" step="0.01" name="hourly_rate" class="form-control" value="<?= e($institution['hourly_rate'] ?? '') ?>">
            </div>
            <div>
                <label class="form-label">نسبة الخصم %</label>
                <input type="number" step="0.01" name="discount_percent" class="form-control" value="<?= e($institution['discount_percent'] ?? '0') ?>">
            </div>
            <div>
                <label class="form-label">الحد الأقصى</label>
                <input type="number" name="max_participants" class="form-control" value="<?= e($institution['max_participants'] ?? '') ?>" placeholder="عدد المشاركين">
            </div>
        </div>

        <div style="margin-bottom:15px;">
            <label class="form-label">ملاحظات</label>
            <textarea name="notes" class="form-control" rows="3"><?= e($institution['notes'] ?? '') ?></textarea>
        </div>

        <div style="margin-bottom:20px;">
            <label style="display:flex;align-items:center;gap:8px;">
                <input type="hidden" name="is_active" value="0">
                <input type="checkbox" name="is_active" value="1" <?= (int)($institution['is_active'] ?? 1) ? 'checked' : '' ?>>
                <span>نشط</span>
            </label>
        </div>

        <button type="submit" class="btn btn-primary" style="padding:10px 30px;">
            <i data-lucide="save" style="width:16px;height:16px;vertical-align:middle;margin-left:6px;"></i>
            <?= $mode === 'edit' ? 'حفظ التعديلات' : 'إضافة المؤسسة' ?>
        </button>
    </form>
</div>

<script>document.addEventListener('DOMContentLoaded', function() { if (typeof lucide !== 'undefined') lucide.createIcons(); });</script>
<?php $__template->endSection(); ?>
