<?php
use App\Modules\Tournaments\Models\Tournament;
$__template->layout('Layout.main');
$isEdit = $isEdit ?? false;
$t = $tournament ?? [];
?>
<?php $__template->section('title'); ?><?= $isEdit ? 'تعديل البطولة' : 'إنشاء بطولة جديدة' ?><?php $__template->endSection(); ?>

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

<?php
$types = Tournament::getTypes();
$formats = Tournament::getFormats();
$genders = ['mixed' => 'مختلط', 'male' => 'ذكور', 'female' => 'إناث'];
?>

<div class="card" style="padding:25px;max-width:900px;">
    <h2 style="margin:0 0 20px;font-size:18px;color:#1A1A2E;">
        <i data-lucide="trophy" style="width:20px;height:20px;vertical-align:middle;margin-left:6px;color:#0D7377;"></i>
        <?= $isEdit ? 'تعديل البطولة' : 'إنشاء بطولة جديدة' ?>
    </h2>

    <form method="POST" action="<?= $isEdit ? '/tournaments/' . (int)($t['id'] ?? 0) : '/tournaments' ?>">
        <?= csrf_field() ?>

        <!-- Basic Info -->
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div style="grid-column:span 2;">
                <label class="form-label">اسم البطولة <span style="color:red;">*</span></label>
                <input type="text" name="name_ar" value="<?= e(old('name_ar') ?? $t['name_ar'] ?? '') ?>" class="form-input" required placeholder="مثال: بطولة النادي لكرة القدم">
            </div>

            <div>
                <label class="form-label">الرياضة <span style="color:red;">*</span></label>
                <select name="discipline_id" class="form-input" required>
                    <option value="">اختر...</option>
                    <?php foreach ($disciplines as $d): ?>
                    <option value="<?= (int) $d['id'] ?>" <?= (old('discipline_id') ?? $t['discipline_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label class="form-label">نوع البطولة <span style="color:red;">*</span></label>
                <select name="tournament_type" class="form-input" required>
                    <option value="">اختر...</option>
                    <?php foreach ($types as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= (old('tournament_type') ?? $t['tournament_type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label class="form-label">نظام البطولة <span style="color:red;">*</span></label>
                <select name="format" class="form-input" required>
                    <option value="">اختر...</option>
                    <?php foreach ($formats as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= (old('format') ?? $t['format'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label class="form-label">الجنس</label>
                <select name="gender" class="form-input">
                    <?php foreach ($genders as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= (old('gender') ?? $t['gender'] ?? 'mixed') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <!-- Dates -->
        <h3 style="font-size:15px;color:#374151;margin:0 0 12px;padding-top:10px;border-top:1px solid #E5E7EB;">المواعيد</h3>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div>
                <label class="form-label">تاريخ البدء <span style="color:red;">*</span></label>
                <input type="date" name="start_date" value="<?= e(old('start_date') ?? $t['start_date'] ?? '') ?>" class="form-input" required>
            </div>
            <div>
                <label class="form-label">تاريخ الانتهاء</label>
                <input type="date" name="end_date" value="<?= e(old('end_date') ?? $t['end_date'] ?? '') ?>" class="form-input">
            </div>
            <div>
                <label class="form-label">بداية التسجيل</label>
                <input type="date" name="registration_start" value="<?= e(old('registration_start') ?? $t['registration_start'] ?? '') ?>" class="form-input">
            </div>
            <div>
                <label class="form-label">نهاية التسجيل</label>
                <input type="date" name="registration_end" value="<?= e(old('registration_end') ?? $t['registration_end'] ?? '') ?>" class="form-input">
            </div>
        </div>

        <!-- Participants & Eligibility -->
        <h3 style="font-size:15px;color:#374151;margin:0 0 12px;padding-top:10px;border-top:1px solid #E5E7EB;">المشاركون والأهلية</h3>
        <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
            <div>
                <label class="form-label">الفئة العمرية</label>
                <input type="text" name="age_group" value="<?= e(old('age_group') ?? $t['age_group'] ?? '') ?>" class="form-input" placeholder="مثال: 12-16 أو under_18">
            </div>
            <div>
                <label class="form-label">الحد الأقصى للمشاركين</label>
                <input type="number" name="max_participants" value="<?= e(old('max_participants') ?? $t['max_participants'] ?? '') ?>" class="form-input" min="2">
            </div>
            <div>
                <label class="form-label">الحد الأدنى للمشاركين</label>
                <input type="number" name="min_participants" value="<?= e(old('min_participants') ?? $t['min_participants'] ?? '2') ?>" class="form-input" min="2">
            </div>
        </div>

        <!-- Venue & Fees -->
        <h3 style="font-size:15px;color:#374151;margin:0 0 12px;padding-top:10px;border-top:1px solid #E5E7EB;">المكان والرسوم</h3>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div>
                <label class="form-label">المنشأة / الملعب</label>
                <select name="facility_id" class="form-input">
                    <option value="">اختر...</option>
                    <?php foreach ($facilities as $f): ?>
                    <option value="<?= (int) $f['id'] ?>" <?= (old('facility_id') ?? $t['facility_id'] ?? '') == $f['id'] ? 'selected' : '' ?>><?= e($f['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label">رسوم الاشتراك (ج.م)</label>
                <input type="number" name="entry_fee" value="<?= e(old('entry_fee') ?? $t['entry_fee'] ?? '0') ?>" class="form-input" min="0" step="0.01">
            </div>
        </div>

        <!-- Additional Info -->
        <h3 style="font-size:15px;color:#374151;margin:0 0 12px;padding-top:10px;border-top:1px solid #E5E7EB;">تفاصيل إضافية</h3>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div>
                <label class="form-label">وصف الجوائز</label>
                <textarea name="prize_description_ar" class="form-input" rows="3" placeholder="مثال: كأس + ميداليات ذهبية للمراكز الثلاثة الأولى"><?= e(old('prize_description_ar') ?? $t['prize_description_ar'] ?? '') ?></textarea>
            </div>
            <div>
                <label class="form-label">القواعد واللوائح</label>
                <textarea name="rules_ar" class="form-input" rows="3" placeholder="قواعد البطولة..."><?= e(old('rules_ar') ?? $t['rules_ar'] ?? '') ?></textarea>
            </div>
        </div>
        <div style="margin-bottom:20px;">
            <label class="form-label">ملاحظات</label>
            <textarea name="notes" class="form-input" rows="2"><?= e(old('notes') ?? $t['notes'] ?? '') ?></textarea>
        </div>

        <!-- Submit -->
        <div style="display:flex;gap:10px;padding-top:15px;border-top:1px solid #E5E7EB;">
            <button type="submit" class="btn btn-primary">
                <i data-lucide="check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i>
                <?= $isEdit ? 'حفظ التعديلات' : 'إنشاء البطولة' ?>
            </button>
            <a href="/tournaments" class="btn btn-outline">إلغاء</a>
        </div>
    </form>
</div>

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