<?php
function os_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function os_t($k, $f) {
    if (function_exists('ec_t')) return ec_t($k, $f);
    if (function_exists('t')) return t($k, $f);
    return $f;
}

$settings = $settings ?? [];
$conference = $conference ?? [];
$success = $_SESSION['flash_success'] ?? '';
$error = $_SESSION['flash_error'] ?? '';
unset($_SESSION['flash_success'], $_SESSION['flash_error']);

$scope = !empty($settings['conference_id']) ? 'conference' : 'org';
?>

<div class="container-fluid py-3">
    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#2563eb);color:white;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-server"></i>
                <?= os_h(os_t('admin.org_smtp.title','Organization SMTP Settings')) ?>
            </h3>
            <div class="opacity-75">
                <?= os_h(($conference['org_name'] ?? '') . ' / ' . ($conference['short_name'] ?? '')) ?>
            </div>
        </div>
    </div>

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

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

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <form method="post" action="/easychair/admin/org-smtp/save" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label fw-bold">Scope</label>
                    <select name="scope" class="form-select">
                        <option value="org" <?= $scope === 'org' ? 'selected' : '' ?>>Organization-wide</option>
                        <option value="conference" <?= $scope === 'conference' ? 'selected' : '' ?>>This conference only</option>
                    </select>
                    <div class="form-text">Conference SMTP overrides organization SMTP.</div>
                </div>

                <div class="col-md-5">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.host','SMTP Host')) ?></label>
                    <input name="smtp_host" class="form-control" required value="<?= os_h($settings['smtp_host'] ?? '') ?>" placeholder="mail.privateemail.com">
                </div>

                <div class="col-md-2">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.port','SMTP Port')) ?></label>
                    <input name="smtp_port" type="number" class="form-control" value="<?= os_h($settings['smtp_port'] ?? 587) ?>">
                </div>

                <div class="col-md-2">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.encryption','Encryption')) ?></label>
                    <select name="smtp_encryption" class="form-select">
                        <?php foreach (['tls','ssl','none'] as $enc): ?>
                            <option value="<?= os_h($enc) ?>" <?= (($settings['smtp_encryption'] ?? 'tls') === $enc) ? 'selected' : '' ?>>
                                <?= strtoupper($enc) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.username','SMTP Username')) ?></label>
                    <input name="smtp_username" class="form-control" value="<?= os_h($settings['smtp_username'] ?? '') ?>" placeholder="info@example.com">
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.password','SMTP Password')) ?></label>
                    <input name="smtp_password" type="password" class="form-control" placeholder="<?= !empty($settings['smtp_password_enc']) ? '********' : '' ?>">
                    <div class="form-text">Leave empty to keep current password.</div>
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.reply_to','Reply To')) ?></label>
                    <input name="reply_to" class="form-control" value="<?= os_h($settings['reply_to'] ?? '') ?>">
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.from_email','From Email')) ?></label>
                    <input name="from_email" class="form-control" value="<?= os_h($settings['from_email'] ?? '') ?>" placeholder="info@example.com">
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold"><?= os_h(os_t('admin.org_smtp.from_name','From Name')) ?></label>
                    <input name="from_name" class="form-control" value="<?= os_h($settings['from_name'] ?? '') ?>" placeholder="Conference Secretariat">
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold">Test Recipient</label>
                    <input name="test_recipient" class="form-control" value="<?= os_h($settings['test_recipient'] ?? '') ?>">
                </div>

                <div class="col-md-3 d-flex align-items-center">
                    <label class="form-check mt-4">
                        <input type="checkbox" name="is_active" value="1" class="form-check-input" <?= !isset($settings['is_active']) || !empty($settings['is_active']) ? 'checked' : '' ?>>
                        <span class="form-check-label fw-bold">Active</span>
                    </label>
                </div>

                <div class="col-12">
                    <button class="btn btn-success rounded-pill px-5">
                        <i class="fa fa-save"></i>
                        <?= os_h(os_t('admin.org_smtp.save','Save SMTP Settings')) ?>
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">
                <i class="fa fa-paper-plane"></i>
                <?= os_h(os_t('admin.org_smtp.test','Test SMTP')) ?>
            </h5>

            <form method="post" action="/easychair/admin/org-smtp/test" class="row g-3 align-items-end">
                <div class="col-md-8">
                    <label class="form-label fw-bold">Recipient</label>
                    <input name="test_recipient" class="form-control" value="<?= os_h($settings['test_recipient'] ?? '') ?>" required>
                </div>
                <div class="col-md-4">
                    <button class="btn btn-primary rounded-pill w-100">
                        <i class="fa fa-paper-plane"></i>
                        Queue Test Email
                    </button>
                </div>
            </form>

            <?php if (!empty($settings['last_test_at'])): ?>
                <hr>
                <div>
                    <strong>Last test:</strong>
                    <?= os_h($settings['last_test_status'] ?? '') ?>
                    —
                    <?= os_h($settings['last_test_at'] ?? '') ?>
                </div>
                <?php if (!empty($settings['last_test_error'])): ?>
                    <pre class="mt-2 p-3 bg-light border rounded"><?= os_h($settings['last_test_error']) ?></pre>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</div>
