<?php $isEdit = !empty($trigger); ?>
<div class="row justify-content-center"><div class="col-lg-8"><div class="card">
    <div class="card-header"><h5 class="mb-0"><?= $isEdit ? lang('comm.edit_trigger') : lang('comm.create_trigger') ?></h5></div>
    <div class="card-body">
        <form method="POST" action="<?= $isEdit ? '/communications/triggers/' . $trigger['id'] : '/communications/triggers' ?>" data-ajax="true">
            <?= csrf_field() ?>
            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label">Trigger Code <span class="text-danger">*</span></label>
                    <input type="text" name="trigger_code" class="form-control" value="<?= e($trigger['trigger_code'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="trigger_code"></div>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Module <span class="text-danger">*</span></label>
                    <input type="text" name="module" class="form-control" value="<?= e($trigger['module'] ?? '') ?>" required placeholder="e.g. members, finance">
                    <div class="invalid-feedback" data-field="module"></div>
                </div>
            </div>
            <div class="row g-3 mt-1">
                <div class="col-md-6">
                    <label class="form-label">Name (EN) <span class="text-danger">*</span></label>
                    <input type="text" name="trigger_name_en" class="form-control" value="<?= e($trigger['trigger_name_en'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="trigger_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="trigger_name_ar" class="form-control" value="<?= e($trigger['trigger_name_ar'] ?? '') ?>" required dir="rtl">
                    <div class="invalid-feedback" data-field="trigger_name_ar"></div>
                </div>
            </div>
            <div class="row g-3 mt-1">
                <div class="col-md-4">
                    <label class="form-label">Event Type <span class="text-danger">*</span></label>
                    <input type="text" name="event_type" class="form-control" value="<?= e($trigger['event_type'] ?? '') ?>" required placeholder="e.g. status_change">
                    <div class="invalid-feedback" data-field="event_type"></div>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Template</label>
                    <select name="template_id" class="form-select select2">
                        <option value="">-- None --</option>
                        <?php foreach ($templates as $t): ?>
                        <option value="<?= $t['id'] ?>" <?= selected($t['id'], $trigger['template_id'] ?? '') ?>><?= e($t['name_en']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </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="trg_active" <?= checked(1, $trigger['is_active'] ?? 1) ?>>
                        <label class="form-check-label" for="trg_active">Active</label>
                    </div>
                </div>
            </div>
            <div class="mt-3">
                <label class="form-label">Conditions (JSON)</label>
                <textarea name="conditions" class="form-control font-monospace" rows="4"><?= e($trigger['conditions'] ?? '') ?></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/triggers" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
            </div>
        </form>
    </div>
</div></div></div>