<?php
session_start();
$expire = 1800;
if (empty($_SESSION['user_id']) || (time() - $_SESSION['login_time'] > $expire)) {
    echo json_encode(["code"=>401,"msg"=>"未登录"]);
    exit;
}
header("Content-Type:application/json;charset=utf-8");
$configFile = "./system_config.json";
if(!file_exists($configFile)){
    echo json_encode(["code"=>500,"msg"=>"配置文件缺失"]);
    exit;
}
$config = json_decode(file_get_contents($configFile),true);
$act = $_POST['act'] ?? '';

if($act == "save_style"){
    $config['theme_color'] = $_POST['theme_color'];
    $config['card_bg'] = $_POST['card_bg'];
    $config['body_bg'] = $_POST['body_bg'];
    $config['font_family'] = $_POST['font_family'];
    file_put_contents($configFile,json_encode($config,JSON_UNESCAPED_UNICODE));
    echo json_encode(["code"=>0,"msg"=>"样式保存成功，刷新页面生效"]);
    exit;
}

if($act == "save_sidebar_link"){
    $list = json_decode($_POST['link_list'],true);
    $config['sidebar_links'] = $list;
    file_put_contents($configFile,json_encode($config,JSON_UNESCAPED_UNICODE));
    echo json_encode(["code"=>0,"msg"=>"侧边栏链接更新成功"]);
    exit;
}

if($act == "get_config"){
    echo json_encode(["code"=>0,"data"=>$config]);
    exit;
}
echo json_encode(["code"=>400,"msg"=>"非法操作"]);
?>
