<?php
// 设置页面
require 'config.php';
require 'login_check.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['file_suffix'])) {
        $fileSuffix = $_POST['file_suffix'];
        
        // 读取当前配置文件
        $configContent = file_get_contents('config.php');
        
        // 替换file_suffix值
        $newConfigContent = preg_replace('/\'file_suffix\' => \'.*?\'/', "'file_suffix' => '$fileSuffix'", $configContent);
        
        if (file_put_contents('config.php', $newConfigContent)) {
            $success = '文件后缀签名设置成功！';
            // 重新加载配置
            require 'config.php';
        } else {
            $error = '设置失败，请检查文件权限';
        }
    }
}

// 获取当前设置
$currentFileSuffix = isset($siteconfig['file_suffix']) ? $siteconfig['file_suffix'] : '';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设置 - 音视频转MP3工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
            color: #333;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        
        h1 {
            text-align: center;
            margin-bottom: 30px;
            color: #2c3e50;
        }
        
        .nav {
            background-color: #fff;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .nav ul {
            list-style: none;
            display: flex;
            gap: 20px;
        }
        
        .nav li a {
            text-decoration: none;
            color: #3498db;
            padding: 10px 15px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .nav li a:hover {
            background-color: #f0f8ff;
        }
        
        .settings-form {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        
        .btn {
            display: block;
            width: 100%;
            padding: 15px;
            background-color: #3498db;
            color: #fff;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .btn:hover {
            background-color: #2980b9;
        }
        
        .error {
            background-color: #fee;
            border: 1px solid #fcc;
            color: #c00;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        
        .success {
            background-color: #efe;
            border: 1px solid #cfc;
            color: #0c0;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        
        .footer {
            text-align: center;
            margin-top: 30px;
            color: #666;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>音视频转MP3工具 - 设置</h1>
        
        <div class="nav">
            <ul>
                <li><a href="index.php">转换记录</a></li>
                <li><a href="ad_manage.php">广告管理</a></li>
                <li><a href="settings.php">系统设置</a></li>
                <li><a href="logout.php">退出登录</a></li>
            </ul>
        </div>
        
        <div class="settings-form">
            <h2>文件后缀设置</h2>
            
            <?php if ($error): ?>
                <div class="error"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="success"><?php echo $success; ?></div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="form-group">
                    <label for="file_suffix">文件后缀签名</label>
                    <input type="text" id="file_suffix" name="file_suffix" value="<?php echo htmlspecialchars($currentFileSuffix); ?>" placeholder="例如：_我的网站 或 _www.example.com">
                    <small style="color: #666; display: block; margin-top: 5px;">转换后的文件名将添加此后缀，留空则不添加后缀</small>
                </div>
                
                <button type="submit" class="btn">保存设置</button>
            </form>
        </div>
        
        <div class="footer">
            <p>音视频转MP3工具 v1.0</p>
        </div>
    </div>
</body>
</html>