<?php
session_start();
if (!isset($_SESSION['unohs'])) {
    header("Location: index.php");
    exit();
}
include "conn.php";

// ✅ আগে থেকে থাকা "site_settings" Table পুনরায় ব্যবহার করা হচ্ছে
// (Wallet Number Feature-এর জন্য আগেই তৈরি হয়েছিল) — নতুন Table
// বানানোর দরকার নেই।
$conn->query("
    CREATE TABLE IF NOT EXISTS site_settings (
        setting_key VARCHAR(50) PRIMARY KEY,
        setting_value VARCHAR(255)
    )
");

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $links = [
        'social_telegram' => trim($_POST['telegram'] ?? ''),
        'social_facebook' => trim($_POST['facebook'] ?? ''),
        'social_whatsapp' => trim($_POST['whatsapp'] ?? ''),
    ];
    foreach ($links as $key => $value) {
        $stmt = $conn->prepare("
            INSERT INTO site_settings (setting_key, setting_value) VALUES (?, ?)
            ON DUPLICATE KEY UPDATE setting_value = ?
        ");
        $stmt->bind_param("sss", $key, $value, $value);
        $stmt->execute();
        $stmt->close();
    }
    $message = '✅ Social Media Link সফলভাবে সংরক্ষণ হয়েছে।';
}

$current = ['social_telegram' => '', 'social_facebook' => '', 'social_whatsapp' => ''];
$result = $conn->query("SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('social_telegram','social_facebook','social_whatsapp')");
while ($row = $result->fetch_assoc()) {
    $current[$row['setting_key']] = $row['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Social Links Settings - RAJ365 Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="admin-gold-theme.css">
    <style>
        body { background: #0A0A0A; padding: 20px; }
        input { background: #171512; border: 1px solid #3A3226; color: #F5EFDC; border-radius: 8px; padding: 10px 14px; width: 100%; margin-bottom: 14px; }
        label { color: #8A8270; font-size: 12px; display: block; margin-bottom: 6px; }
    </style>
</head>
<body>
<div class="container" style="max-width:560px;">
    <h3 style="color:#D4AF37;">📱 Social Media Links</h3>
    <p style="color:#8A8270; font-size:12.5px;">Website-এর Floating Icon-এর Link এখান থেকে বদলাও</p>

    <?php if ($message): ?>
        <div class="alert" style="background:#171512; border:1px solid #D4AF37; color:#F5EFDC;"><?= $message ?></div>
    <?php endif; ?>

    <form method="POST">
        <label>Telegram Link</label>
        <input type="text" name="telegram" placeholder="https://t.me/YourUsername" value="<?= htmlspecialchars($current['social_telegram']) ?>">

        <label>Facebook Link</label>
        <input type="text" name="facebook" placeholder="https://facebook.com/YourPage" value="<?= htmlspecialchars($current['social_facebook']) ?>">

        <label>WhatsApp Link</label>
        <input type="text" name="whatsapp" placeholder="https://wa.me/8801XXXXXXXXX" value="<?= htmlspecialchars($current['social_whatsapp']) ?>">

        <button type="submit" style="background:#D4AF37; color:#1A1400; font-weight:700; border:none; border-radius:8px; padding:10px; width:100%;">Save করো</button>
    </form>

    <a href="dashboard.php" style="color:#D4AF37; display:block; margin-top:16px;">← Dashboard-এ ফিরে যাও</a>
</div>
</body>
</html>
