<?php $isEdit = !empty($template); ?>

<div class="row justify-content-center">
    <div class="col-lg-9">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><?= $isEdit ? lang('comm.edit_template') : lang('comm.create_template') ?></h5></div>
            <div class="card-body">
                <form method="POST" action="<?= $isEdit ? '/communications/templates/' . $template['id'] : '/communications/templates' ?>" data-ajax="true">
                    <?= csrf_field() ?>

                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label"><?= lang('comm.template_code') ?> <span class="text-danger">*</span></label>
                            <input type="text" name="template_code" class="form-control" value="<?= e($template['template_code'] ?? '') ?>" required maxlength="50" placeholder="e.g. member_activated">
                            <div class="invalid-feedback" data-field="template_code"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label"><?= lang('comm.name_en') ?> <span class="text-danger">*</span></label>
                            <input type="text" name="name_en" class="form-control" value="<?= e($template['name_en'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="name_en"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label"><?= lang('comm.name_ar') ?> <span class="text-danger">*</span></label>
                            <input type="text" name="name_ar" class="form-control" value="<?= e($template['name_ar'] ?? '') ?>" required dir="rtl">
                            <div class="invalid-feedback" data-field="name_ar"></div>
                        </div>
                    </div>

                    <div class="row g-3 mt-1">
                        <div class="col-md-4">
                            <label class="form-label"><?= lang('comm.channel') ?> <span class="text-danger">*</span></label>
                            <select name="channel_id" class="form-select" required>
                                <option value="">-- Select --</option>
                                <?php foreach ($channels as $ch): ?>
                                <option value="<?= $ch['id'] ?>" <?= selected($ch['id'], $template['channel_id'] ?? '') ?>><?= e($ch['name_en']) ?> (<?= e($ch['channel_code']) ?>)</option>
                                <?php endforeach; ?>
                            </select>
                            <div class="invalid-feedback" data-field="channel_id"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label"><?= lang('comm.event_trigger') ?> <span class="text-danger">*</span></label>
                            <input type="text" name="event_trigger" class="form-control" value="<?= e($template['event_trigger'] ?? '') ?>" required placeholder="e.g. member.activated">
                            <div class="invalid-feedback" data-field="event_trigger"></div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">&nbsp;</label>
                            <div class="form-check mt-2">
                                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="is_active" <?= checked(1, $template['is_active'] ?? 1) ?>>
                                <label class="form-check-label" for="is_active"><?= lang('comm.active') ?></label>
                            </div>
                        </div>
                    </div>

                    <div class="row g-3 mt-1">
                        <div class="col-md-6">
                            <label class="form-label"><?= lang('comm.subject_en') ?></label>
                            <input type="text" name="subject_en" class="form-control" value="<?= e($template['subject_en'] ?? '') ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label"><?= lang('comm.subject_ar') ?></label>
                            <input type="text" name="subject_ar" class="form-control" value="<?= e($template['subject_ar'] ?? '') ?>" dir="rtl">
                        </div>
                    </div>

                    <div class="mt-3">
                        <label class="form-label"><?= lang('comm.body_en') ?> <span class="text-danger">*</span></label>
                        <textarea name="body_en" class="form-control" rows="8" required placeholder="Use {variable_name} for placeholders"><?= e($template['body_en'] ?? '') ?></textarea>
                        <div class="invalid-feedback" data-field="body_en"></div>
                        <small class="text-muted">Available variables: {member_name}, {member_number}, {club_name}, {amount}, {date}, {status}</small>
                    </div>

                    <div class="mt-3">
                        <label class="form-label"><?= lang('comm.body_ar') ?></label>
                        <textarea name="body_ar" class="form-control" rows="8" dir="rtl"><?= e($template['body_ar'] ?? '') ?></textarea>
                    </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="/communications/templates" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>