<?php
/**
 * ⚙️ BRANDING SETUP - Ubah nama, logo, dan deskripsi website
 * Hapus file ini setelah selesai digunakan!
 */
require 'RGShenn.php';

$EMERGENCY_KEY = 'gclhos-emergency-2026';
$success = [];
$error = '';

// Ambil data konfigurasi saat ini
$cfg = $call->query("SELECT * FROM conf WHERE code = 'webcfg'")->fetch_assoc();

if (isset($_POST['save_branding'])) {
    $key = $_POST['emergency_key'] ?? '';
    if ($key !== $EMERGENCY_KEY) {
        $error = '❌ Kunci akses salah!';
    } else {
        $judul   = $call->real_escape_string(trim($_POST['judul'] ?? ''));
        $deskripsi = $call->real_escape_string(trim($_POST['deskripsi'] ?? ''));
        $keyword = $call->real_escape_string(trim($_POST['keyword'] ?? ''));
        $icon    = $call->real_escape_string(trim($_POST['icon'] ?? ''));

        if ($judul)    { $call->query("UPDATE conf SET c1='$judul' WHERE code='webcfg'");    $success[] = 'Nama website'; }
        if ($deskripsi){ $call->query("UPDATE conf SET c2='$deskripsi' WHERE code='webcfg'"); $success[] = 'Deskripsi'; }
        if ($keyword)  { $call->query("UPDATE conf SET c3='$keyword' WHERE code='webcfg'");  $success[] = 'Keyword'; }
        if ($icon)     { $call->query("UPDATE conf SET c5='$icon' WHERE code='webcfg'");     $success[] = 'Icon/Logo URL'; }

        // Refresh data
        $cfg = $call->query("SELECT * FROM conf WHERE code = 'webcfg'")->fetch_assoc();
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>⚙️ Branding Setup</title>
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: Arial, sans-serif; background: #1a1a2e; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
        .card { background: #fff; border-radius: 12px; padding: 30px; max-width: 520px; width: 100%; box-shadow: 0 10px 40px rgba(0,0,0,0.4); }
        .warning { background: #fff3cd; border: 2px solid #ffc107; border-radius: 8px; padding: 12px 14px; margin-bottom: 20px; font-size: 13px; color: #856404; text-align: center; }
        h2 { color: #5A24A8; text-align: center; margin-bottom: 4px; font-size: 22px; }
        .subtitle { text-align: center; color: #666; font-size: 13px; margin-bottom: 22px; }
        .preview-box { display: flex; align-items: center; gap: 12px; background: #f8f0ff; border: 2px solid #5A24A8; border-radius: 10px; padding: 12px 16px; margin-bottom: 20px; }
        .preview-box img { width: 50px; height: 50px; border-radius: 8px; object-fit: cover; border: 1px solid #ddd; }
        .preview-box .info strong { display: block; font-size: 16px; color: #5A24A8; }
        .preview-box .info small { color: #888; font-size: 12px; }
        label { display: block; font-weight: bold; margin-bottom: 4px; font-size: 14px; color: #333; }
        .hint { font-size: 11px; color: #888; margin-bottom: 6px; display: block; }
        input, textarea { width: 100%; padding: 10px 12px; border: 2px solid #ddd; border-radius: 8px; font-size: 14px; margin-bottom: 14px; outline: none; }
        input:focus, textarea:focus { border-color: #5A24A8; }
        .current-val { font-size: 12px; color: #5A24A8; font-weight: 500; margin-bottom: 10px; background: #f0ebfa; padding: 4px 10px; border-radius: 4px; display: inline-block; }
        button { width: 100%; padding: 13px; background: #5A24A8; color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: bold; cursor: pointer; margin-top: 6px; }
        button:hover { background: #4a1a98; }
        .error { background: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
        .success { background: #d4edda; border: 1px solid #c3e6cb; color: #155724; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
        .delete-notice { background: #f8d7da; border-radius: 8px; padding: 10px; margin-top: 14px; font-size: 12px; color: #721c24; text-align: center; }
        hr { border: none; border-top: 1px solid #eee; margin: 16px 0; }
        .section-title { font-size: 13px; font-weight: bold; color: #999; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 12px; }
    </style>
</head>
<body>
<div class="card">
    <div class="warning">⚠️ Hapus file <code>branding-setup.php</code> setelah selesai!</div>
    <h2>⚙️ Pengaturan Branding</h2>
    <p class="subtitle">Ubah nama, logo, dan deskripsi website</p>

    <!-- Preview saat ini -->
    <div class="preview-box">
        <img src="<?= htmlspecialchars($cfg['c5'] ?? '') ?>" alt="Logo" onerror="this.src='data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 width=%2250%22 height=%2250%22><rect width=%2250%22 height=%2250%22 fill=%22%235A24A8%22/><text x=%2225%22 y=%2232%22 font-size=%2218%22 fill=%22white%22 text-anchor=%22middle%22>?</text></svg>'">
        <div class="info">
            <strong><?= htmlspecialchars($cfg['c1'] ?? 'Belum diatur') ?></strong>
            <small><?= htmlspecialchars(substr($cfg['c2'] ?? '-', 0, 60)) ?>...</small>
        </div>
    </div>

    <?php if ($error): ?><div class="error"><?= $error ?></div><?php endif; ?>
    <?php if ($success): ?>
        <div class="success">✅ Berhasil diperbarui: <b><?= implode(', ', $success) ?></b><br>
        <a href="<?= base_url() ?>" target="_blank" style="color:#155724;">→ Lihat perubahan di website</a></div>
    <?php endif; ?>

    <form method="POST">
        <div class="section-title">📝 Informasi Website</div>

        <label>Nama Website (Saat ini: <span class="current-val"><?= htmlspecialchars($cfg['c1'] ?? '-') ?></span>)</label>
        <span class="hint">Contoh: GCL Pay, Toko Digital Ghanim, dll</span>
        <input type="text" name="judul" placeholder="Nama baru website Anda" value="<?= htmlspecialchars($cfg['c1'] ?? '') ?>">

        <label>Deskripsi (Saat ini: <span class="current-val"><?= htmlspecialchars(substr($cfg['c2'] ?? '-', 0, 30)) ?>...</span>)</label>
        <span class="hint">Deskripsi singkat untuk SEO</span>
        <input type="text" name="deskripsi" placeholder="Deskripsi website" value="<?= htmlspecialchars($cfg['c2'] ?? '') ?>">

        <label>Keyword SEO</label>
        <input type="text" name="keyword" placeholder="pulsa, paket data, token pln" value="<?= htmlspecialchars($cfg['c3'] ?? '') ?>">

        <hr>
        <div class="section-title">🖼️ Logo / Icon</div>

        <label>URL Icon/Logo (Saat ini: <span class="current-val"><?= strlen($cfg['c5'] ?? '') > 30 ? '...'.substr($cfg['c5'] ?? '-', -25) : htmlspecialchars($cfg['c5'] ?? '-') ?></span>)</label>
        <span class="hint">Masukkan URL gambar PNG (rekomendasi 512x512). Contoh: https://ghanimofficial.my.id/gcl/library/assets/images/icon/logo.png</span>
        <input type="text" name="icon" placeholder="https://..." value="<?= htmlspecialchars($cfg['c5'] ?? '') ?>">

        <span class="hint" style="color:#c0392b; font-size:12px;">💡 <b>Tips upload icon:</b> Upload file logo ke <code>library/assets/images/icon/</code> di hosting, lalu tulis URL-nya di atas.</span>

        <hr>
        <label>Kunci Akses Darurat:</label>
        <input type="password" name="emergency_key" placeholder="Masukkan kunci" required>

        <button type="submit" name="save_branding">💾 Simpan Pengaturan Branding</button>
    </form>

    <div class="delete-notice">
        ⚠️ <b>PENTING:</b> Setelah selesai, hapus file <code>branding-setup.php</code> dari hosting!
    </div>
</div>
</body>
</html>
