<?php $isEdit = !empty($template); $t = $template ?? []; ?>
<div class="row justify-content-center"><div class="col-lg-10"><div class="card">
    <div class="card-header"><h5 class="mb-0"><?= $isEdit ? lang('comm.edit_doc_template') : lang('comm.create_doc_template') ?></h5></div>
    <div class="card-body">
        <form method="POST" action="<?= $isEdit ? '/communications/document-templates/' . $t['id'] : '/communications/document-templates' ?>" data-ajax="true">
            <?= csrf_field() ?>
            <div class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">Code <span class="text-danger">*</span></label>
                    <input type="text" name="template_code" class="form-control" value="<?= e($t['template_code'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="template_code"></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Name (EN) <span class="text-danger">*</span></label>
                    <input type="text" name="name_en" class="form-control" value="<?= e($t['name_en'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="name_en"></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Name (AR) <span class="text-danger">*</span></label>
                    <input type="text" name="name_ar" class="form-control" value="<?= e($t['name_ar'] ?? '') ?>" required dir="rtl">
                    <div class="invalid-feedback" data-field="name_ar"></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Category <span class="text-danger">*</span></label>
                    <input type="text" name="category" class="form-control" value="<?= e($t['category'] ?? '') ?>" required placeholder="e.g. membership, finance">
                    <div class="invalid-feedback" data-field="category"></div>
                </div>
            </div>
            <div class="row g-3 mt-1">
                <div class="col-md-3">
                    <label class="form-label">Type <span class="text-danger">*</span></label>
                    <select name="template_type" class="form-select" required>
                        <?php foreach (['letter','certificate','receipt','invoice','contract','form','report','badge','other'] as $tt): ?>
                        <option value="<?= $tt ?>" <?= selected($tt, $t['template_type'] ?? '') ?>><?= ucfirst($tt) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Paper Size <span class="text-danger">*</span></label>
                    <select name="paper_size" class="form-select" required>
                        <?php foreach (['a4','a3','letter','legal','custom'] as $ps): ?>
                        <option value="<?= $ps ?>" <?= selected($ps, $t['paper_size'] ?? 'a4') ?>><?= strtoupper($ps) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Orientation <span class="text-danger">*</span></label>
                    <select name="orientation" class="form-select" required>
                        <option value="portrait" <?= selected('portrait', $t['orientation'] ?? 'portrait') ?>>Portrait</option>
                        <option value="landscape" <?= selected('landscape', $t['orientation'] ?? '') ?>>Landscape</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <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="dt_active" <?= checked(1, $t['is_active'] ?? 1) ?>>
                        <label class="form-check-label" for="dt_active">Active</label>
                    </div>
                </div>
            </div>
            <div class="mt-3">
                <label class="form-label">Header Template (HTML)</label>
                <textarea name="header_template" class="form-control font-monospace" rows="4"><?= e($t['header_template'] ?? '') ?></textarea>
            </div>
            <div class="mt-3">
                <label class="form-label">Body (EN) — Use <code>{{variable_name}}</code> for placeholders</label>
                <textarea name="template_body_en" class="form-control font-monospace" rows="12"><?= e($t['template_body_en'] ?? '') ?></textarea>
                <small class="text-muted">Variables: {{member_name}}, {{member_number}}, {{national_id}}, {{membership_type}}, {{join_date}}, {{club_name}}, {{current_date}}</small>
            </div>
            <div class="mt-3">
                <label class="form-label">Body (AR)</label>
                <textarea name="template_body_ar" class="form-control font-monospace" rows="12" dir="rtl"><?= e($t['template_body_ar'] ?? '') ?></textarea>
            </div>
            <div class="mt-3">
                <label class="form-label">Footer Template (HTML)</label>
                <textarea name="footer_template" class="form-control font-monospace" rows="3"><?= e($t['footer_template'] ?? '') ?></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/document-templates" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
            </div>
        </form>
    </div>
</div></div></div>