<?php
include 'config.php';

// --- HANDLE RESET DEFAULTS ---
if (isset($_POST['reset_defaults'])) {
    // Resetting only Visuals, Colors, Sizes & Effects to Default (Not deleting content)
    $sql = "UPDATE site_settings SET 
            navbar_color='#000000', 
            footer_color='#111111', 
            bg_color='#0b0c10',
            active_effect='snow', 
            effect_color='#00f2ff', 
            effect_speed='1.0',
            col_mobile=2, 
            col_desktop=4, 
            card_size='medium',
            text_align='center', 
            category_align='center',
            title_color='#ffffff', 
            desc_color='#cccccc',
            show_title=1, 
            show_desc=1,
            fs_cat_mob=20, fs_cat_tab=24, fs_cat_pc=30,
            fs_card_mob=16, fs_card_tab=18, fs_card_pc=20
            WHERE id=1";
            
    if ($conn->query($sql)) {
        echo "<script>alert('ALL SETTINGS RESET TO DEFAULT!'); window.location.href='admin/notices.php';</script>";
    } else {
        echo "Error: " . $conn->error;
    }
    exit;
}

if (isset($_POST['update_settings'])) {
    
    // বর্তমান ডাটা আনা হচ্ছে
    $cur = $conn->query("SELECT * FROM site_settings WHERE id=1")->fetch_assoc();

    // সেফ ভ্যালু ফাংশন
    function val($conn, $key, $def) {
        return isset($_POST[$key]) ? mysqli_real_escape_string($conn, $_POST[$key]) : $def;
    }

    // --- EXISTING DATA ---
    $title = val($conn, 'site_title', $cur['site_title']);
    $nav   = val($conn, 'navbar_color', $cur['navbar_color']);
    $foot  = val($conn, 'footer_color', $cur['footer_color']);
    $bg    = val($conn, 'bg_color', $cur['bg_color']);
    $cat_align = val($conn, 'category_align', isset($cur['category_align']) ? $cur['category_align'] : 'center'); 
    $mob   = isset($_POST['col_mobile']) ? (int)$_POST['col_mobile'] : $cur['col_mobile'];
    $pc    = isset($_POST['col_desktop']) ? (int)$_POST['col_desktop'] : $cur['col_desktop'];
    $size  = val($conn, 'card_size', $cur['card_size']);
    $st    = isset($_POST['show_title']) ? (int)$_POST['show_title'] : $cur['show_title'];
    $sd    = isset($_POST['show_desc']) ? (int)$_POST['show_desc'] : $cur['show_desc'];
    $align = val($conn, 'text_align', $cur['text_align']);
    $tc    = val($conn, 'title_color', $cur['title_color']);
    $dc    = val($conn, 'desc_color', $cur['desc_color']);
    $eff   = val($conn, 'active_effect', $cur['active_effect']);
    $ec    = val($conn, 'effect_color', $cur['effect_color']);
    $esp   = isset($_POST['effect_speed']) ? (float)$_POST['effect_speed'] : $cur['effect_speed'];
    $hanim = val($conn, 'header_anim', $cur['header_anim']);
    $vid   = val($conn, 'bg_video', $cur['bg_video']);
    $bgm   = val($conn, 'youtube_bgm', $cur['youtube_bgm']);
    $app   = val($conn, 'app_link', $cur['app_link']);
    $fb    = val($conn, 'social_fb', $cur['social_fb']);
    $tg    = val($conn, 'social_tg', $cur['social_tg']);
    $yt    = val($conn, 'social_yt', $cur['social_yt']);
    $ds    = val($conn, 'social_ds', $cur['social_ds']);
    $show_off_btn = isset($_POST['show_officer_btn']) ? (int)$_POST['show_officer_btn'] : $cur['show_officer_btn']; // নতুন লজিক
    $p_status     = isset($_POST['popup_status']) ? (int)$_POST['popup_status'] : $cur['popup_status'];
    $p_style      = val($conn, 'popup_style', $cur['popup_style']);
    $p_timer      = isset($_POST['popup_timer']) ? (int)$_POST['popup_timer'] : $cur['popup_timer'];
    $p_title      = val($conn, 'popup_title', $cur['popup_title']);
    $p_text       = val($conn, 'popup_text', $cur['popup_text']);
    $p_link       = val($conn, 'popup_link', $cur['popup_link']);
    $p_btn_name   = val($conn, 'popup_btn_name', $cur['popup_btn_name']);
    $p_btn_status = isset($_POST['popup_btn_status']) ? (int)$_POST['popup_btn_status'] : $cur['popup_btn_status'];
    $i_notice_status = isset($_POST['inline_notice_status']) ? (int)$_POST['inline_notice_status'] : $cur['inline_notice_status'];
    $i_notice_text   = val($conn, 'inline_notice_text', $cur['inline_notice_text']);
    $i_anim          = val($conn, 'inline_animation', $cur['inline_animation']);
    $i_bg            = val($conn, 'inline_bg_color', $cur['inline_bg_color']);
    $i_tc            = val($conn, 'inline_text_color', $cur['inline_text_color']);
    $i_bc            = val($conn, 'inline_border_color', $cur['inline_border_color']);

    // --- NEW: FONT SIZES (Mobile, Tablet, PC) ---
    $fs_cat_mob  = isset($_POST['fs_cat_mob']) ? (int)$_POST['fs_cat_mob'] : 20;
    $fs_cat_tab  = isset($_POST['fs_cat_tab']) ? (int)$_POST['fs_cat_tab'] : 24;
    $fs_cat_pc   = isset($_POST['fs_cat_pc'])  ? (int)$_POST['fs_cat_pc']  : 30;

    $fs_card_mob = isset($_POST['fs_card_mob']) ? (int)$_POST['fs_card_mob'] : 16;
    $fs_card_tab = isset($_POST['fs_card_tab']) ? (int)$_POST['fs_card_tab'] : 18;
    $fs_card_pc  = isset($_POST['fs_card_pc'])  ? (int)$_POST['fs_card_pc']  : 20;


    // --- IMAGES ---
    $site_icon = $cur['site_icon'];
    if (!empty($_FILES['site_icon']['name'])) {
        $site_icon = "icon_" . time() . "_" . basename($_FILES["site_icon"]["name"]);
        move_uploaded_file($_FILES["site_icon"]["tmp_name"], "uploads/" . $site_icon);
    }
    $popup_image = $cur['popup_image'];
    if (!empty($_FILES['popup_image']['name'])) {
        $popup_image = "pop_" . time() . "_" . basename($_FILES["popup_image"]["name"]);
        move_uploaded_file($_FILES["popup_image"]["tmp_name"], "uploads/" . $popup_image);
    }

    // --- SAFE COLUMN CHECK (Creates columns if missing) ---
    $new_cols = [
        'category_align' => "VARCHAR(20) DEFAULT 'center'",
        'fs_cat_mob' => "INT(11) DEFAULT 20",
        'fs_cat_tab' => "INT(11) DEFAULT 24",
        'fs_cat_pc'  => "INT(11) DEFAULT 30",
        'fs_card_mob'=> "INT(11) DEFAULT 16",
        'fs_card_tab'=> "INT(11) DEFAULT 18",
        'fs_card_pc' => "INT(11) DEFAULT 20",
        'show_officer_btn' => "INT(1) DEFAULT 0" // নতুন কলাম চেক
    ];

    foreach($new_cols as $col => $def) {
        $chk = $conn->query("SHOW COLUMNS FROM site_settings LIKE '$col'");
        if($chk->num_rows == 0) {
            $conn->query("ALTER TABLE site_settings ADD COLUMN $col $def");
        }
    }

    // --- UPDATE QUERY ---
    $sql = "UPDATE site_settings SET 
            site_title='$title', navbar_color='$nav', footer_color='$foot', bg_color='$bg',
            col_mobile='$mob', col_desktop='$pc', card_size='$size',
            show_title='$st', show_desc='$sd', text_align='$align', category_align='$cat_align',
            title_color='$tc', desc_color='$dc',
            active_effect='$eff', effect_color='$ec', effect_speed='$esp', header_anim='$hanim',
            bg_video='$vid', youtube_bgm='$bgm', app_link='$app',
            social_fb='$fb', social_tg='$tg', social_yt='$yt', social_ds='$ds',
            site_icon='$site_icon', popup_status='$p_status', popup_style='$p_style', popup_timer='$p_timer',
            popup_title='$p_title', popup_text='$p_text', popup_image='$popup_image', popup_link='$p_link',
            popup_btn_name='$p_btn_name', popup_btn_status='$p_btn_status',
            inline_notice_status='$i_notice_status', inline_notice_text='$i_notice_text', 
            inline_animation='$i_anim', inline_bg_color='$i_bg', inline_text_color='$i_tc', inline_border_color='$i_bc',
            fs_cat_mob='$fs_cat_mob', fs_cat_tab='$fs_cat_tab', fs_cat_pc='$fs_cat_pc',
            fs_card_mob='$fs_card_mob', fs_card_tab='$fs_card_tab', fs_card_pc='$fs_card_pc',
            show_officer_btn='$show_off_btn'
            WHERE id=1";

    if ($conn->query($sql)) {
        // ডাইনামিক ব্যাক লিঙ্ক (যাতে অফিসার পেজ থেকে সেভ করলে সেখানেই ফেরে)
        $back_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'admin/categories.php';
        echo "<script>alert('SAVED!'); window.location.href='$back_url';</script>";
    } else {
        echo "Error: " . $conn->error;
    }
}
?>