<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إضافة أكاديمية جديدة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/academies" 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'); ?>

<form method="POST" action="/academies" id="academyForm">
    <?= csrf_field() ?>

    <!-- Basic Information -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="file-text" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">البيانات الأساسية</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
                <div class="form-group">
                    <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 placeholder="مثال: أكاديمية كرة القدم">
                </div>
                <div class="form-group">
                    <label class="form-label">الاسم بالإنجليزي</label>
                    <input type="text" name="name_en" value="<?= e(old('name_en')) ?>" class="form-input" id="nameEn" placeholder="e.g. Football Academy" style="direction:ltr;text-align:left;">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">كود الأكاديمية</label>
                    <input type="text" name="code" value="<?= e(old('code')) ?>" class="form-input" id="codeInput" placeholder="يتم توليده تلقائياً" style="direction:ltr;text-align:left;text-transform:uppercase;">
                    <small style="color:#9CA3AF;font-size:11px;">اختياري — يتم توليده تلقائياً إن ترك فارغاً</small>
                </div>
                <div class="form-group">
                    <label class="form-label">النشاط الرياضي <span style="color:#DC2626;">*</span></label>
                    <select name="discipline_id" class="form-select" required>
                        <option value="">-- اختر النشاط --</option>
                        <?php foreach ($disciplines as $disc): ?>
                        <option value="<?= (int) $disc['id'] ?>" <?= old('discipline_id') == $disc['id'] ? 'selected' : '' ?>><?= e($disc['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">نوع الأكاديمية <span style="color:#DC2626;">*</span></label>
                    <select name="academy_type" class="form-select" required>
                        <option value="">-- اختر النوع --</option>
                        <?php foreach ($academyTypes as $key => $label): ?>
                        <option value="<?= e($key) ?>" <?= old('academy_type') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div style="display:grid;grid-template-columns:3fr 1fr;gap:20px;margin-top:15px;">
                <div class="form-group">
                    <label class="form-label">وصف الأكاديمية</label>
                    <textarea name="description_ar" class="form-input" rows="3" placeholder="وصف مختصر للأكاديمية..."><?= e(old('description_ar')) ?></textarea>
                </div>
                <div class="form-group">
                    <label class="form-label">ترتيب العرض</label>
                    <input type="number" name="sort_order" value="<?= e(old('sort_order', '0')) ?>" class="form-input" min="0" style="direction:ltr;text-align:left;">
                </div>
            </div>
        </div>
    </div>

    <!-- Config Card -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="settings" style="width:18px;height:18px;color:#D97706;"></i>
            <h3 style="margin:0;color:#D97706;font-size:15px;">الإعدادات</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px;">
                <div class="form-group">
                    <label class="form-label">الحد الأدنى للسن</label>
                    <input type="number" name="age_min" value="<?= e(old('age_min')) ?>" class="form-input" min="0" max="99" placeholder="مثال: 5" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">الحد الأقصى للسن</label>
                    <input type="number" name="age_max" value="<?= e(old('age_max')) ?>" class="form-input" min="0" max="99" placeholder="مثال: 18" style="direction:ltr;text-align:left;">
                </div>
                <div class="form-group">
                    <label class="form-label">السعة الاستيعابية</label>
                    <input type="number" name="capacity" value="<?= e(old('capacity')) ?>" class="form-input" min="1" placeholder="مثال: 100" style="direction:ltr;text-align:left;">
                </div>
            </div>
        </div>
    </div>

    <!-- Submit Buttons -->
    <div style="display:flex;gap:10px;justify-content:flex-start;">
        <button type="submit" class="btn btn-primary" style="padding:12px 30px;font-size:15px;">
            <i data-lucide="check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حفظ الأكاديمية
        </button>
        <a href="/academies" class="btn btn-outline" style="padding:12px 30px;font-size:15px;">إلغاء</a>
    </div>
</form>

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

    // Auto-generate code from English name
    var nameEnInput = document.getElementById('nameEn');
    var codeInput = document.getElementById('codeInput');
    if (nameEnInput && codeInput) {
        nameEnInput.addEventListener('input', function() {
            if (codeInput.dataset.manuallyEdited !== 'true') {
                codeInput.value = this.value
                    .toUpperCase()
                    .replace(/[^A-Z0-9\s]/g, '')
                    .replace(/\s+/g, '_')
                    .substring(0, 30);
            }
        });
        codeInput.addEventListener('input', function() {
            this.dataset.manuallyEdited = 'true';
        });
    }
});
</script>

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