<?php
require_once '../config/config.php';

// 连接数据库
$conn = db_connect();

// 检查是否已存在旧表结构
$old_table_exists = false;
$result = $conn->query("SHOW TABLES LIKE 'settings'");
if ($result && $result->num_rows > 0) {
    // 检查列结构
    $columns = $conn->query("SHOW COLUMNS FROM settings LIKE 'name'");
    if ($columns && $columns->num_rows > 0) {
        $old_table_exists = true;
        
        // 备份旧数据
        $old_data = [];
        $data_result = $conn->query("SELECT name, value FROM settings");
        if ($data_result && $data_result->num_rows > 0) {
            while ($row = $data_result->fetch_assoc()) {
                $old_data[$row['name']] = $row['value'];
            }
        }
        
        // 删除旧表
        $conn->query("DROP TABLE settings");
        
        echo "已备份原有设置数据，准备创建新表结构...<br>";
    }
}

// 创建新设置表
$sql = "CREATE TABLE IF NOT EXISTS settings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    site_title VARCHAR(255) DEFAULT '导航网站',
    site_description TEXT,
    announcement TEXT,
    footer_text TEXT,
    background_image VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

if ($conn->query($sql) === TRUE) {
    // 检查是否需要恢复数据
    if ($old_table_exists && !empty($old_data)) {
        // 构建数据恢复SQL
        $site_title = $conn->real_escape_string($old_data['site_title'] ?? '导航网站');
        $site_description = $conn->real_escape_string($old_data['site_description'] ?? '');
        $announcement = $conn->real_escape_string($old_data['announcement'] ?? '');
        $footer_text = $conn->real_escape_string($old_data['footer_text'] ?? '');
        
        $insert_sql = "INSERT INTO settings (site_title, site_description, announcement, footer_text) 
                       VALUES ('$site_title', '$site_description', '$announcement', '$footer_text')";
        
        if ($conn->query($insert_sql) === TRUE) {
            echo "旧数据已成功迁移到新表结构！<br>";
        } else {
            echo "迁移数据时出错: " . $conn->error . "<br>";
        }
    } else {
        // 插入默认设置
        $sql = "INSERT INTO settings (site_title, site_description, announcement, footer_text) 
                VALUES ('导航网站', '一个简洁优雅的个人导航网站', '欢迎使用导航网站', '© " . date('Y') . " 导航网站 All Rights Reserved')";
        
        if ($conn->query($sql) === TRUE) {
            echo "默认设置已插入！<br>";
        } else {
            echo "插入默认设置时出错: " . $conn->error . "<br>";
        }
    }
    
    echo '<div style="margin: 20px; padding: 15px; background-color: #dff0d8; color: #3c763d; border: 1px solid #d6e9c6; border-radius: 4px;">';
    echo "<strong>成功!</strong> 设置表已创建并配置完成。<br>";
    echo '<a href="settings.php" style="color: #3c763d; text-decoration: underline;">返回设置页面</a>';
    echo '</div>';
} else {
    echo '<div style="margin: 20px; padding: 15px; background-color: #f2dede; color: #a94442; border: 1px solid #ebccd1; border-radius: 4px;">';
    echo "<strong>错误!</strong> 创建设置表时出错: " . $conn->error;
    echo '</div>';
}

$conn->close();
?>
