<?php
function etp_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$templates = $templates ?? [];
$conference = $conference ?? [];
$success = $_SESSION['flash_success'] ?? '';
$error = $_SESSION['flash_error'] ?? '';
unset($_SESSION['flash_success'], $_SESSION['flash_error']);
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1"><i class="fa fa-envelope-open-text"></i> Conference <?php echo ec_h(ec_t('admin.email_templates.title','Email Templates')); ?></h3>
            <div class="opacity-75">
                Add, update, duplicate, delete, and activate conference-specific email templates.
            </div>
            <div class="mt-2">
                <span class="badge bg-light text-dark">
                    <?= etp_h(($conference['short_name'] ?? '') . ' / Conference #' . ($conference['id'] ?? '')) ?>
                </span>
                <span class="badge bg-warning text-dark">
                    Org #<?= (int)($conference['org_id'] ?? 0) ?>
                </span>
            </div>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= etp_h($success) ?></div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= etp_h($error) ?></div>
    <?php endif; ?>

    <?php if (!empty($_GET['saved'])): ?>
        <div class="alert alert-success">Email templates saved successfully.</div>
    <?php endif; ?>

    <div class="alert alert-info">
        Available placeholders:
        <code>{author_name}</code>,
        <code>{reviewer_name}</code>,
        <code>{submission_no}</code>,
        <code>{submission_title}</code>,
        <code>{decision_notes}</code>,
        <code>{conference_title}</code>,
        <code>{conference_short_name}</code>,
        <code>{login_link}</code>,
        <code>{reset_link}</code>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">
                <i class="fa fa-plus-circle"></i>
                Add New Template
            </h5>

            <form method="post" action="/easychair/admin/email-templates/store" class="row g-3">

                <div class="col-md-3">
                    <label class="form-label fw-bold"><?php echo ec_h(ec_t('admin.email_templates.template_key','Template Key')); ?></label>
                    <input name="template_key" class="form-control" required placeholder="e.g. author_welcoming">
                    <div class="text-muted small">Lowercase key, auto-normalized.</div>
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold"><?php echo ec_h(ec_t('admin.email_templates.event_key','Event Key')); ?></label>
                    <input name="event_key" class="form-control" placeholder="e.g. author_welcoming">
                    <div class="text-muted small">Leave blank to use template key.</div>
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold">Subject EN</label>
                    <input name="subject_en" class="form-control" placeholder="Welcome to {conference_short_name}">
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold">Subject AR</label>
                    <input name="subject_ar" class="form-control" placeholder="مرحباً بكم">
                </div>

                <div class="col-md-6">
                    <label class="form-label fw-bold">Body EN</label>
                    <textarea name="body_en" rows="6" class="form-control" dir="ltr" placeholder="Dear {author_name}, ..."></textarea>
                </div>

                <div class="col-md-6">
                    <label class="form-label fw-bold">Body AR</label>
                    <textarea name="body_ar" rows="6" class="form-control" dir="rtl" placeholder="عزيزي/عزيزتي {author_name}، ..."></textarea>
                </div>

                <div class="col-md-3">
                    <label class="fw-bold">
                        <input type="checkbox" name="is_active" value="1" checked>
                        Active
                    </label>
                </div>

                <div class="col-md-9 text-end">
                    <button class="btn btn-success rounded-pill px-5">
                        <i class="fa fa-plus"></i>
                        Add Template
                    </button>
                </div>
            </form>
        </div>
    </div>

    <form method="post" action="/easychair/admin/email-templates/save">

        <?php foreach ($templates as $t): ?>
            <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
                <div class="card-body p-4">

                    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                        <h5 class="fw-bold mb-0">
                            <span class="badge bg-primary"><?= etp_h($t['template_key'] ?: $t['event_key']) ?></span>
                            <span class="badge bg-secondary"><?= etp_h($t['event_key']) ?></span>
                            <?php if (empty($t['is_active'])): ?>
                                <span class="badge bg-warning text-dark">Inactive</span>
                            <?php endif; ?>
                        </h5>

                        <div class="d-flex flex-wrap gap-2">
                            <label class="fw-bold pt-1">
                                <input type="checkbox"
                                       name="templates[<?= (int)$t['id'] ?>][is_active]"
                                       value="1"
                                       <?= !empty($t['is_active']) ? 'checked' : '' ?>>
                                Active
                            </label>

                            <a class="btn btn-sm btn-outline-primary rounded-pill"
                               href="/easychair/admin/email-templates/duplicate?id=<?= (int)$t['id'] ?>">
                                Duplicate
                            </a>

                            <a class="btn btn-sm btn-outline-danger rounded-pill"
                               onclick="return confirm('Delete this template?')"
                               href="/easychair/admin/email-templates/delete?id=<?= (int)$t['id'] ?>">
                                Delete
                            </a>
                        </div>
                    </div>

                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="form-label fw-bold"><?php echo ec_h(ec_t('admin.email_templates.template_key','Template Key')); ?></label>
                            <input name="templates[<?= (int)$t['id'] ?>][template_key]"
                                   class="form-control"
                                   value="<?= etp_h($t['template_key']) ?>">
                        </div>

                        <div class="col-md-3">
                            <label class="form-label fw-bold"><?php echo ec_h(ec_t('admin.email_templates.event_key','Event Key')); ?></label>
                            <input name="templates[<?= (int)$t['id'] ?>][event_key]"
                                   class="form-control"
                                   value="<?= etp_h($t['event_key'] ?: $t['template_key']) ?>">
                        </div>

                        <div class="col-md-3">
                            <label class="form-label fw-bold">Subject EN</label>
                            <input name="templates[<?= (int)$t['id'] ?>][subject_en]"
                                   class="form-control"
                                   value="<?= etp_h($t['subject_en']) ?>">
                        </div>

                        <div class="col-md-3">
                            <label class="form-label fw-bold">Subject AR</label>
                            <input name="templates[<?= (int)$t['id'] ?>][subject_ar]"
                                   class="form-control"
                                   value="<?= etp_h($t['subject_ar']) ?>">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Body EN</label>
                            <textarea name="templates[<?= (int)$t['id'] ?>][body_en]"
                                      rows="8"
                                      class="form-control"
                                      dir="ltr"><?= etp_h($t['body_en']) ?></textarea>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Body AR</label>
                            <textarea name="templates[<?= (int)$t['id'] ?>][body_ar]"
                                      rows="8"
                                      class="form-control"
                                      dir="rtl"><?= etp_h($t['body_ar']) ?></textarea>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if (!$templates): ?>
            <div class="alert alert-warning">
                No templates found. Add the first template above.
            </div>
        <?php endif; ?>

        <button class="btn btn-success btn-lg rounded-pill px-5">
            <i class="fa fa-save"></i>
            Save All Existing Templates
        </button>

    </form>
</div>
