<?php
/**
 * SMM Paket Satış Scripti - Telegram Bildirim Ayarları
 * Demo Mod Destekli - Cache Sorunu Düzeltildi
 * @version 1.0.2
 */

require_once __DIR__ . '/../config.php';

if (!isLoggedIn() || !isAdmin()) {
    redirect(SITE_URL . '/admin/');
    exit;
}

// Demo mod kontrolü
$demoMode = $settings->get('demo_mode', '0') == '1';

// CSRF token
if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrfToken = $_SESSION['csrf_token'];

$errors = [];
$success = false;

// Telegram ayarlarını direkt veritabanından çek (cache bypass)
function getTelegramSettings($db) {
    $telegramKeys = [
        'telegram_bot_enabled',
        'telegram_bot_token', 
        'telegram_chat_id',
        'telegram_notify_new_order',
        'telegram_notify_payment',
        'telegram_notify_completed',
        'telegram_notify_contact'
    ];
    
    $defaults = [
        'telegram_bot_enabled' => '0',
        'telegram_bot_token' => '',
        'telegram_chat_id' => '',
        'telegram_notify_new_order' => '1',
        'telegram_notify_payment' => '1',
        'telegram_notify_completed' => '1',
        'telegram_notify_contact' => '1'
    ];
    
    try {
        $placeholders = implode(',', array_fill(0, count($telegramKeys), '?'));
        $stmt = $db->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ($placeholders)");
        $stmt->execute($telegramKeys);
        $results = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
        
        return array_merge($defaults, $results);
    } catch (PDOException $e) {
        return $defaults;
    }
}

// Test mesajı gönderme fonksiyonu
function sendTestTelegramMessage($botToken, $chatId, $message) {
    $url = "https://api.telegram.org/bot{$botToken}/sendMessage";
    
    $data = [
        'chat_id' => $chatId,
        'text' => $message,
        'parse_mode' => 'HTML',
        'disable_web_page_preview' => true
    ];
    
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query($data),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_SSL_VERIFYPEER => false
    ]);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);
    curl_close($ch);
    
    if ($error) {
        return ['success' => false, 'error' => 'Bağlantı hatası: ' . $error];
    }
    
    $result = json_decode($response, true);
    
    if ($httpCode === 200 && isset($result['ok']) && $result['ok'] === true) {
        return ['success' => true, 'message_id' => $result['result']['message_id'] ?? null];
    }
    
    $errorMsg = $result['description'] ?? 'Bilinmeyen hata';
    return ['success' => false, 'error' => $errorMsg];
}

// Form gönderildi mi?
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    // CSRF kontrolü
    if (!hash_equals($csrfToken, $_POST['csrf_token'] ?? '')) {
        $errors[] = 'Güvenlik doğrulaması başarısız.';
    }
    
    // Demo mod kontrolü
    if ($demoMode) {
        $errors[] = 'Demo modda bu işlem yapılamaz.';
    }
    
    if (empty($errors)) {
        $action = $_POST['action'] ?? 'save';
        
        // Test mesajı gönder
        if ($action === 'test') {
            $testToken = trim($_POST['telegram_bot_token'] ?? '');
            $testChatId = trim($_POST['telegram_chat_id'] ?? '');
            
            if (empty($testToken)) {
                $errors[] = 'Bot Token gerekli.';
            }
            if (empty($testChatId)) {
                $errors[] = 'Chat ID gerekli.';
            }
            
            if (empty($errors)) {
                $testMessage = "🔔 <b>Test Bildirimi</b>\n\n";
                $testMessage .= "✅ Telegram bildirimleri başarıyla yapılandırıldı!\n\n";
                $testMessage .= "📅 Tarih: " . date('d.m.Y H:i:s') . "\n";
                $testMessage .= "🌐 Site: " . SITE_NAME;
                
                $result = sendTestTelegramMessage($testToken, $testChatId, $testMessage);
                
                if ($result['success']) {
                    setFlash('success', 'Test mesajı başarıyla gönderildi! Telegram\'ınızı kontrol edin.');
                    redirect(SITE_URL . '/admin/telegram-ayarlari.php');
                    exit;
                } else {
                    $errors[] = 'Mesaj gönderilemedi: ' . $result['error'];
                }
            }
        }
        // Ayarları kaydet
        elseif ($action === 'save') {
            
            $saveSettings = [
                'telegram_bot_enabled' => isset($_POST['telegram_bot_enabled']) ? '1' : '0',
                'telegram_bot_token' => trim($_POST['telegram_bot_token'] ?? ''),
                'telegram_chat_id' => trim($_POST['telegram_chat_id'] ?? ''),
                'telegram_notify_new_order' => isset($_POST['telegram_notify_new_order']) ? '1' : '0',
                'telegram_notify_payment' => isset($_POST['telegram_notify_payment']) ? '1' : '0',
                'telegram_notify_completed' => isset($_POST['telegram_notify_completed']) ? '1' : '0',
                'telegram_notify_contact' => isset($_POST['telegram_notify_contact']) ? '1' : '0',
            ];
            
            // Bot aktifse token ve chat id zorunlu
            if ($saveSettings['telegram_bot_enabled'] === '1') {
                if (empty($saveSettings['telegram_bot_token'])) {
                    $errors[] = 'Bot Token zorunludur.';
                }
                if (empty($saveSettings['telegram_chat_id'])) {
                    $errors[] = 'Chat ID zorunludur.';
                }
            }
            
            if (empty($errors)) {
                try {
                    $db->beginTransaction();
                    
                    foreach ($saveSettings as $key => $value) {
                        // Önce sil sonra ekle (REPLACE INTO gibi)
                        $stmt = $db->prepare("DELETE FROM settings WHERE setting_key = ?");
                        $stmt->execute([$key]);
                        
                        $stmt = $db->prepare("
                            INSERT INTO settings (setting_key, setting_value, setting_type, setting_group, created_at, updated_at) 
                            VALUES (?, ?, 'string', 'telegram', NOW(), NOW())
                        ");
                        $stmt->execute([$key, $value]);
                    }
                    
                    $db->commit();
                    
                    setFlash('success', 'Telegram ayarları başarıyla kaydedildi.');
                    redirect(SITE_URL . '/admin/telegram-ayarlari.php');
                    exit;
                    
                } catch (PDOException $e) {
                    $db->rollBack();
                    $errors[] = 'Veritabanı hatası: ' . $e->getMessage();
                }
            }
        }
    }
}

// Ayarları çek (her zaman güncel)
$telegramSettings = getTelegramSettings($db);

// Demo modda hassas bilgileri maskele
function maskToken($token, $demoMode) {
    if (!$demoMode || empty($token)) return $token;
    if (strlen($token) < 20) return '••••••••••••••••••••';
    return substr($token, 0, 10) . '••••••••••••••••••••' . substr($token, -5);
}

function maskChatId($chatId, $demoMode) {
    if (!$demoMode || empty($chatId)) return $chatId;
    return '••••••••••';
}

$adminPageTitle = 'Telegram Bildirim Ayarları';
require_once __DIR__ . '/header.php';
?>

<style>
.telegram-header {
    background: linear-gradient(135deg, #0088cc 0%, #00a8e8 100%);
    border-radius: 16px;
    padding: 30px;
    margin-bottom: 25px;
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
}

.telegram-icon {
    width: 70px;
    height: 70px;
    background: rgba(255,255,255,0.2);
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    color: #fff;
}

.telegram-info h2 {
    font-size: 22px;
    font-weight: 700;
    color: #fff;
    margin: 0 0 5px;
}

.telegram-info p {
    color: rgba(255,255,255,0.8);
    margin: 0;
    font-size: 14px;
}

.telegram-status {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255,255,255,0.15);
    padding: 10px 20px;
    border-radius: 30px;
}

.telegram-status .dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ef4444;
}

.telegram-status.active .dot {
    background: #10b981;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.telegram-status span {
    color: #fff;
    font-weight: 600;
    font-size: 13px;
}

.demo-warning {
    background: rgba(245, 158, 11, 0.15);
    border: 1px solid rgba(245, 158, 11, 0.3);
    border-radius: 12px;
    padding: 16px 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    color: #f59e0b;
}

.demo-warning i { font-size: 20px; }
.demo-warning p { margin: 0; font-size: 14px; }

.settings-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 25px;
}

@media (max-width: 991px) {
    .settings-grid { grid-template-columns: 1fr; }
}

.settings-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    overflow: hidden;
}

.settings-card-header {
    padding: 18px 22px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 12px;
}

.settings-card-header i {
    width: 40px;
    height: 40px;
    background: rgba(0, 136, 204, 0.15);
    color: #0088cc;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}

.settings-card-header h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
}

.settings-card-body { padding: 22px; }

.form-group { margin-bottom: 20px; }
.form-group:last-child { margin-bottom: 0; }

.form-label {
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
    font-size: 14px;
}

.form-label .required {
    color: #ef4444;
    margin-left: 3px;
}

.form-hint {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
}

.form-hint i { margin-right: 4px; }
.form-hint a { color: #0088cc; }
.form-hint a:hover { text-decoration: underline; }

/* Toggle Switch */
.toggle-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    background: var(--bg-dark);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    margin-bottom: 12px;
    transition: all 0.2s;
}

.toggle-item:hover { border-color: rgba(0, 136, 204, 0.3); }
.toggle-item:last-child { margin-bottom: 0; }

.toggle-item.main-toggle {
    background: rgba(0, 136, 204, 0.1);
    border-color: rgba(0, 136, 204, 0.2);
}

.toggle-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.toggle-left i {
    width: 36px;
    height: 36px;
    background: var(--bg-hover);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: var(--text-secondary);
}

.toggle-item.main-toggle .toggle-left i {
    background: rgba(0, 136, 204, 0.2);
    color: #0088cc;
}

.toggle-text h6 {
    font-size: 14px;
    font-weight: 600;
    margin: 0 0 2px;
}

.toggle-text span {
    font-size: 12px;
    color: var(--text-muted);
}

/* Custom Checkbox Switch */
.switch-wrapper {
    position: relative;
    width: 50px;
    height: 26px;
    flex-shrink: 0;
}

.switch-wrapper input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
    z-index: 2;
    margin: 0;
}

.switch-slider {
    position: absolute;
    inset: 0;
    background: var(--bg-hover);
    border-radius: 26px;
    transition: 0.3s;
    pointer-events: none;
}

.switch-slider::before {
    content: '';
    position: absolute;
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background: #fff;
    border-radius: 50%;
    transition: 0.3s;
}

.switch-wrapper input:checked + .switch-slider {
    background: #0088cc;
}

.switch-wrapper input:checked + .switch-slider::before {
    transform: translateX(24px);
}

.switch-wrapper input:disabled + .switch-slider {
    opacity: 0.5;
    cursor: not-allowed;
}

.help-box {
    background: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.2);
    border-radius: 12px;
    padding: 18px;
    margin-top: 20px;
}

.help-box h5 {
    font-size: 14px;
    font-weight: 600;
    color: #3b82f6;
    margin: 0 0 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.help-box ol {
    margin: 0;
    padding-left: 18px;
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.8;
}

.help-box code {
    background: rgba(0,0,0,0.2);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12px;
    color: #0088cc;
}

.btn-actions {
    display: flex;
    gap: 12px;
    margin-top: 25px;
    flex-wrap: wrap;
}

.btn-test {
    background: rgba(0, 136, 204, 0.15);
    border: 1px solid rgba(0, 136, 204, 0.3);
    color: #0088cc;
    padding: 12px 24px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 14px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-test:hover { background: #0088cc; color: #fff; }
.btn-test:disabled { opacity: 0.5; cursor: not-allowed; }

.error-box {
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: 12px;
    padding: 16px 20px;
    margin-bottom: 20px;
}

.error-box ul { margin: 0; padding-left: 20px; }
.error-box li { color: #ef4444; font-size: 14px; margin-bottom: 5px; }
.error-box li:last-child { margin-bottom: 0; }

.notification-preview {
    background: var(--bg-dark);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 16px;
    margin-top: 15px;
}

.notification-preview-title {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.notification-preview-content {
    background: #1e3a5f;
    border-radius: 8px;
    padding: 12px;
    font-size: 13px;
    line-height: 1.6;
    color: #fff;
    font-family: monospace;
}

@media (max-width: 576px) {
    .telegram-header {
        padding: 20px;
        text-align: center;
        justify-content: center;
    }
    .telegram-status { margin-left: 0; margin-top: 10px; }
    .btn-actions { flex-direction: column; }
    .btn-actions button { width: 100%; justify-content: center; }
}
</style>

<!-- Telegram Header -->
<div class="telegram-header">
    <div class="telegram-icon">
        <i class="fab fa-telegram"></i>
    </div>
    <div class="telegram-info">
        <h2>Telegram Bildirim</h2>
        <p>Yeni siparişler ve önemli olaylar için anlık Telegram bildirimleri alın</p>
    </div>
    <div class="telegram-status <?php echo $telegramSettings['telegram_bot_enabled'] === '1' ? 'active' : ''; ?>">
        <div class="dot"></div>
        <span><?php echo $telegramSettings['telegram_bot_enabled'] === '1' ? 'Aktif' : 'Pasif'; ?></span>
    </div>
</div>

<!-- Demo Mod Uyarısı -->
<?php if ($demoMode): ?>
<div class="demo-warning">
    <i class="fas fa-exclamation-triangle"></i>
    <p><strong>Demo Mod Aktif!</strong> Bu modda Telegram ayarları değiştirilemez ve hassas bilgiler gizlenir.</p>
</div>
<?php endif; ?>

<!-- Hatalar -->
<?php if (!empty($errors)): ?>
<div class="error-box">
    <ul>
        <?php foreach ($errors as $error): ?>
        <li><i class="fas fa-exclamation-circle"></i> <?php echo clean($error); ?></li>
        <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>

<form method="POST" id="telegramForm">
    <input type="hidden" name="csrf_token" value="<?php echo $csrfToken; ?>">
    <input type="hidden" name="action" value="save" id="formAction">
    
    <div class="settings-grid">
        <!-- Sol: Bot Ayarları -->
        <div class="settings-card">
            <div class="settings-card-header">
                <i class="fas fa-robot"></i>
                <h3>Bot Ayarları</h3>
            </div>
            <div class="settings-card-body">
                <!-- Ana Toggle -->
                <div class="toggle-item main-toggle">
                    <div class="toggle-left">
                        <i class="fab fa-telegram"></i>
                        <div class="toggle-text">
                            <h6>Telegram Bildirimleri</h6>
                            <span>Bildirimleri aç/kapat</span>
                        </div>
                    </div>
                    <div class="switch-wrapper">
                        <input type="checkbox" name="telegram_bot_enabled" value="1" 
                               id="telegram_bot_enabled"
                               <?php echo $telegramSettings['telegram_bot_enabled'] === '1' ? 'checked' : ''; ?>
                               <?php echo $demoMode ? 'disabled' : ''; ?>>
                        <span class="switch-slider"></span>
                    </div>
                </div>
                
                <div class="form-group" style="margin-top: 20px;">
                    <label class="form-label">Bot Token <span class="required">*</span></label>
                    <input type="text" name="telegram_bot_token" class="form-control" 
                           placeholder="7123456789:AAHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                           value="<?php echo clean($demoMode ? maskToken($telegramSettings['telegram_bot_token'], true) : $telegramSettings['telegram_bot_token']); ?>"
                           <?php echo $demoMode ? 'disabled' : ''; ?>>
                    <div class="form-hint">
                        <i class="fas fa-info-circle"></i>
                        <a href="https://t.me/BotFather" target="_blank">@BotFather</a>'dan aldığınız bot token
                    </div>
                </div>
                
                <div class="form-group">
                    <label class="form-label">Chat ID <span class="required">*</span></label>
                    <input type="text" name="telegram_chat_id" class="form-control" 
                           placeholder="123456789 veya -1001234567890"
                           value="<?php echo clean($demoMode ? maskChatId($telegramSettings['telegram_chat_id'], true) : $telegramSettings['telegram_chat_id']); ?>"
                           <?php echo $demoMode ? 'disabled' : ''; ?>>
                    <div class="form-hint">
                        <i class="fas fa-info-circle"></i>
                        Kişisel ID veya grup ID'si (grup için negatif sayı)
                    </div>
                </div>
                
                <!-- Yardım Kutusu -->
                <div class="help-box">
                    <h5><i class="fas fa-question-circle"></i> Nasıl Yapılır?</h5>
                    <ol>
                        <li>Telegram'da <code>@BotFather</code>'a gidin</li>
                        <li><code>/newbot</code> yazıp yeni bot oluşturun</li>
                        <li>Verilen <strong>Token</strong>'ı buraya yapıştırın</li>
                        <li>Oluşturduğunuz bota <code>/start</code> yazın</li>
                        <li><code>@userinfobot</code>'a gidip <strong>Chat ID</strong>'nizi öğrenin</li>
                    </ol>
                </div>
            </div>
        </div>
        
        <!-- Sağ: Bildirim Ayarları -->
        <div class="settings-card">
            <div class="settings-card-header">
                <i class="fas fa-bell"></i>
                <h3>Bildirim Tercihleri</h3>
            </div>
            <div class="settings-card-body">
                <p style="font-size: 13px; color: var(--text-muted); margin-bottom: 15px;">
                    Hangi durumlarda bildirim almak istediğinizi seçin:
                </p>
                
                <!-- Yeni Sipariş -->
                <div class="toggle-item">
                    <div class="toggle-left">
                        <i class="fas fa-shopping-cart"></i>
                        <div class="toggle-text">
                            <h6>Yeni Sipariş</h6>
                            <span>Yeni sipariş geldiğinde bildir</span>
                        </div>
                    </div>
                    <div class="switch-wrapper">
                        <input type="checkbox" name="telegram_notify_new_order" value="1"
                               <?php echo $telegramSettings['telegram_notify_new_order'] === '1' ? 'checked' : ''; ?>
                               <?php echo $demoMode ? 'disabled' : ''; ?>>
                        <span class="switch-slider"></span>
                    </div>
                </div>
                
                <!-- Ödeme Alındı -->
                <div class="toggle-item">
                    <div class="toggle-left">
                        <i class="fas fa-credit-card"></i>
                        <div class="toggle-text">
                            <h6>Ödeme Alındı</h6>
                            <span>Dekont yüklendiğinde bildir</span>
                        </div>
                    </div>
                    <div class="switch-wrapper">
                        <input type="checkbox" name="telegram_notify_payment" value="1"
                               <?php echo $telegramSettings['telegram_notify_payment'] === '1' ? 'checked' : ''; ?>
                               <?php echo $demoMode ? 'disabled' : ''; ?>>
                        <span class="switch-slider"></span>
                    </div>
                </div>
                
                <!-- Sipariş Tamamlandı -->
                <div class="toggle-item">
                    <div class="toggle-left">
                        <i class="fas fa-check-circle"></i>
                        <div class="toggle-text">
                            <h6>Sipariş Tamamlandı</h6>
                            <span>Sipariş tamamlandığında bildir</span>
                        </div>
                    </div>
                    <div class="switch-wrapper">
                        <input type="checkbox" name="telegram_notify_completed" value="1"
                               <?php echo $telegramSettings['telegram_notify_completed'] === '1' ? 'checked' : ''; ?>
                               <?php echo $demoMode ? 'disabled' : ''; ?>>
                        <span class="switch-slider"></span>
                    </div>
                </div>
                
                <!-- İletişim Formu -->
                <div class="toggle-item">
                    <div class="toggle-left">
                        <i class="fas fa-envelope"></i>
                        <div class="toggle-text">
                            <h6>İletişim Mesajı</h6>
                            <span>Yeni iletişim formu geldiğinde bildir</span>
                        </div>
                    </div>
                    <div class="switch-wrapper">
                        <input type="checkbox" name="telegram_notify_contact" value="1"
                               <?php echo $telegramSettings['telegram_notify_contact'] === '1' ? 'checked' : ''; ?>
                               <?php echo $demoMode ? 'disabled' : ''; ?>>
                        <span class="switch-slider"></span>
                    </div>
                </div>
                
                <!-- Örnek Bildirim -->
                <div class="notification-preview">
                    <div class="notification-preview-title">
                        <i class="fab fa-telegram"></i> Örnek Bildirim
                    </div>
                    <div class="notification-preview-content">
                        🛒 <strong>Yeni Sipariş!</strong><br><br>
                        📦 Instagram 5000 Takipçi<br>
                        👤 Ahmet Yılmaz<br>
                        💰 179,90₺<br>
                        🔗 @kullaniciadi<br>
                        📅 <?php echo date('d.m.Y H:i'); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Butonlar -->
    <div class="btn-actions">
        <button type="submit" class="btn-primary-custom" <?php echo $demoMode ? 'disabled' : ''; ?>>
            <i class="fas fa-save"></i> Ayarları Kaydet
        </button>
        <button type="button" class="btn-test" id="testBtn" <?php echo $demoMode ? 'disabled' : ''; ?>>
            <i class="fab fa-telegram"></i> Test Mesajı Gönder
        </button>
    </div>
</form>

<script>
document.getElementById('testBtn').addEventListener('click', function() {
    document.getElementById('formAction').value = 'test';
    document.getElementById('telegramForm').submit();
});

document.getElementById('telegramForm').addEventListener('submit', function(e) {
    const btn = e.submitter;
    if (btn && !btn.id.includes('test')) {
        document.getElementById('formAction').value = 'save';
    }
});
</script>

<?php require_once __DIR__ . '/footer.php'; ?>