<div class="row justify-content-center">
    <div class="col-lg-10">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><?= e($title) ?></h5></div>
            <div class="card-body">
                <form method="POST" action="<?= e($formAction) ?>" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="form-label">Offer Code <span class="text-danger">*</span></label>
                            <input type="text" name="offer_code" class="form-control" value="<?= e($offer['offer_code'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="offer_code"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Name (EN) <span class="text-danger">*</span></label>
                            <input type="text" name="name_en" class="form-control" value="<?= e($offer['name_en'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="name_en"></div>
                        </div>
                        <div class="col-md-5">
                            <label class="form-label">Name (AR) <span class="text-danger">*</span></label>
                            <input type="text" name="name_ar" class="form-control" value="<?= e($offer['name_ar'] ?? '') ?>" required dir="rtl">
                            <div class="invalid-feedback" data-field="name_ar"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Offer Type <span class="text-danger">*</span></label>
                            <select name="offer_type" class="form-select" required>
                                <?php foreach (['discount','fee_waiver','payment_plan','upgrade','bundle','custom'] as $t): ?>
                                <option value="<?= $t ?>" <?= selected($t, $offer['offer_type'] ?? 'discount') ?>><?= ucfirst(str_replace('_', ' ', $t)) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Discount Method</label>
                            <select name="discount_method" class="form-select">
                                <option value="">-- N/A --</option>
                                <option value="percentage" <?= selected('percentage', $offer['discount_method'] ?? '') ?>>Percentage</option>
                                <option value="fixed_amount" <?= selected('fixed_amount', $offer['discount_method'] ?? '') ?>>Fixed Amount</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Discount Value</label>
                            <input type="number" name="discount_value" class="form-control" value="<?= e($offer['discount_value'] ?? '') ?>" step="0.01" min="0">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Start Date <span class="text-danger">*</span></label>
                            <input type="date" name="start_date" class="form-control" value="<?= e($offer['start_date'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="start_date"></div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">End Date <span class="text-danger">*</span></label>
                            <input type="date" name="end_date" class="form-control" value="<?= e($offer['end_date'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="end_date"></div>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Max Uses</label>
                            <input type="number" name="max_uses" class="form-control" value="<?= e($offer['max_uses'] ?? '') ?>" min="0" placeholder="Unlimited">
                        </div>
                        <?php if ($offer): ?>
                        <div class="col-md-3">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-select">
                                <?php foreach (['draft','active','paused','expired','cancelled'] as $s): ?>
                                <option value="<?= $s ?>" <?= selected($s, $offer['status'] ?? 'draft') ?>><?= ucfirst($s) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <?php endif; ?>
                        <div class="col-12">
                            <label class="form-label">Target Membership Types</label>
                            <div class="row g-2">
                                <?php foreach ($membershipTypes as $mt): ?>
                                <div class="col-auto">
                                    <div class="form-check">
                                        <input type="checkbox" name="membership_type_ids[]" value="<?= $mt['id'] ?>" class="form-check-input" <?= in_array($mt['id'], $selectedTypes) ? 'checked' : '' ?>>
                                        <label class="form-check-label"><?= e($mt['name_en']) ?></label>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Description (EN)</label>
                            <textarea name="description_en" class="form-control" rows="3"><?= e($offer['description_en'] ?? '') ?></textarea>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Description (AR)</label>
                            <textarea name="description_ar" class="form-control" rows="3" dir="rtl"><?= e($offer['description_ar'] ?? '') ?></textarea>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Terms (EN)</label>
                            <textarea name="terms_en" class="form-control" rows="3"><?= e($offer['terms_en'] ?? '') ?></textarea>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Terms (AR)</label>
                            <textarea name="terms_ar" class="form-control" rows="3" dir="rtl"><?= e($offer['terms_ar'] ?? '') ?></textarea>
                        </div>
                        <div class="col-12">
                            <div class="form-check"><input type="checkbox" name="requires_approval" class="form-check-input" value="1" <?= checked(1, $offer['requires_approval'] ?? 0) ?>><label class="form-check-label">Requires Approval to Apply</label></div>
                        </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="/offers" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>