<?php
$isEdit = !empty($period);
$p = $period ?? [];
$formAction = $isEdit ? '/application-periods/' . $p['id'] : '/application-periods';
?>
<div class="container-fluid fade-in">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header"><h5 class="mb-0"><?= $isEdit ? lang('applications.edit_period') : lang('applications.create_period') ?></h5></div>
                <div class="card-body">
                    <form method="POST" action="<?= $formAction ?>" data-ajax="true">
                        <?= csrf_field() ?>
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">Name (EN) <span class="text-danger">*</span></label>
                                <input type="text" name="period_name_en" class="form-control" value="<?= e($p['period_name_en'] ?? '') ?>" required>
                                <div class="invalid-feedback" data-field="period_name_en"></div>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Name (AR) <span class="text-danger">*</span></label>
                                <input type="text" name="period_name_ar" class="form-control" dir="rtl" value="<?= e($p['period_name_ar'] ?? '') ?>" required>
                                <div class="invalid-feedback" data-field="period_name_ar"></div>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Start Date <span class="text-danger">*</span></label>
                                <input type="text" name="start_date" class="form-control datepicker" value="<?= e($p['start_date'] ?? '') ?>" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">End Date <span class="text-danger">*</span></label>
                                <input type="text" name="end_date" class="form-control datepicker" value="<?= e($p['end_date'] ?? '') ?>" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Max Applications</label>
                                <input type="number" name="max_applications" class="form-control" value="<?= e($p['max_applications'] ?? '') ?>">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Applicable Membership Types</label>
                                <select name="membership_type_ids[]" class="form-select select2" multiple>
                                    <?php
                                    $selectedTypes = [];
                                    if ($isEdit && $p['membership_type_ids']) {
                                        $selectedTypes = json_decode($p['membership_type_ids'], true) ?: [];
                                    }
                                    ?>
                                    <?php foreach ($membershipTypes as $t): ?>
                                    <option value="<?= $t['id'] ?>" <?= in_array($t['id'], $selectedTypes) ? 'selected' : '' ?>><?= e(current_lang() === 'ar' ? $t['name_ar'] : $t['name_en']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Active</label>
                                <select name="is_active" class="form-select">
                                    <option value="1" <?= selected(1, $p['is_active'] ?? 1) ?>>Yes</option>
                                    <option value="0" <?= selected(0, $p['is_active'] ?? 1) ?>>No</option>
                                </select>
                            </div>
                        </div>
                        <div class="mt-4 d-flex gap-2">
                            <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button>
                            <a href="/application-periods" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>