<?php

/**
 * @id 33
 * @name 下载地址管理器
 * @version 1.0.0
 * @description 管理网站常用下载地址，统一配置方便前台调用和修改，支持多种下载来源管理设置
 * @price 0
 * @type 插件
 * @website
 * @width 450
 * @height 410
 * @thumbnail /img/id33.png
 */

$root_path = realpath(dirname(__DIR__, 2));
require_once $root_path . '/common.php';

$plugin_config = <<<EOT
[config_params]
官方地址1 =
官方地址2 =
网盘地址1 =
网盘地址2 =
[/config_params]

[Plugin Instructions]

[/Plugin Instructions]
EOT;

$config_file = $root_path . '/config/download_urls.php';

if (!file_exists($config_file)) {
    $default_config = [
        'official1' => '',
        'official2' => '',
        'netdisk1' => '',
        'netdisk2' => ''
    ];
    file_put_contents($config_file, '<?php return ' . var_export($default_config, true) . ';');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['plugin_action'])) {
    header('Content-Type: application/json');

    $action = $_POST['plugin_action'];

    if ($action === 'save') {
        $official1 = isset($_POST['official1']) ? trim($_POST['official1']) : '';
        $official2 = isset($_POST['official2']) ? trim($_POST['official2']) : '';
        $netdisk1 = isset($_POST['netdisk1']) ? trim($_POST['netdisk1']) : '';
        $netdisk2 = isset($_POST['netdisk2']) ? trim($_POST['netdisk2']) : '';

        $config = [
            'official1' => $official1,
            'official2' => $official2,
            'netdisk1' => $netdisk1,
            'netdisk2' => $netdisk2
        ];

        $result = file_put_contents($config_file, '<?php return ' . var_export($config, true) . ';');

        if ($result !== false) {
            echo json_encode(['success' => true, 'message' => '保存成功']);
        } else {
            echo json_encode(['success' => false, 'message' => '保存失败，请检查文件权限']);
        }
        exit;
    }

    echo json_encode(['success' => false, 'message' => '未知操作']);
    exit;
}

$config = include $config_file;
$official1 = isset($config['official1']) ? $config['official1'] : '';
$official2 = isset($config['official2']) ? $config['official2'] : '';
$netdisk1 = isset($config['netdisk1']) ? $config['netdisk1'] : '';
$netdisk2 = isset($config['netdisk2']) ? $config['netdisk2'] : '';
?>

<div class="download-container">
    <div class="download-header">
        <h3>下载地址配置</h3>
        <p class="download-desc">配置常用下载地址，前台可通过配置文件统一调用</p>
    </div>

    <div class="download-form">
        <div class="download-item">
            <label class="download-label">官方地址 1</label>
            <input type="text" class="download-input" id="download-official1" value="<?php echo escape($official1); ?>" placeholder="请输入官方地址1">
        </div>

        <div class="download-item">
            <label class="download-label">官方地址 2</label>
            <input type="text" class="download-input" id="download-official2" value="<?php echo escape($official2); ?>" placeholder="请输入官方地址2">
        </div>

        <div class="download-item">
            <label class="download-label">网盘地址 1</label>
            <input type="text" class="download-input" id="download-netdisk1" value="<?php echo escape($netdisk1); ?>" placeholder="请输入网盘地址1">
        </div>

        <div class="download-item">
            <label class="download-label">网盘地址 2</label>
            <input type="text" class="download-input" id="download-netdisk2" value="<?php echo escape($netdisk2); ?>" placeholder="请输入网盘地址2">
        </div>

        <div class="download-actions">
            <button class="download-btn download-btn-save" id="download-save-btn">保存配置</button>
        </div>
    </div>
</div>

<style>
    .download-container {
        margin: 0 auto;
        font-family: 'Arial', sans-serif;
        padding: 15px;
        box-sizing: border-box;
    }

    .download-header {
        margin-bottom: 25px;
        padding-bottom: 15px;
        border-bottom: 2px solid #e2e8f0;
    }

    .download-header h3 {
        margin: 0 0 8px 0;
        font-size: 18px;
        font-weight: 600;
        color: #1e293b;
    }

    .download-desc {
        margin: 0;
        font-size: 13px;
        color: #64748b;
    }

    .download-form {
        display: flex;
        flex-direction: column;
        gap: 18px;
    }

    .download-item {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .download-label {
        font-size: 14px;
        font-weight: 500;
        color: #334155;
    }

    .download-input {
        padding: 8px 14px;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        font-size: 14px;
        color: #4a5568;
        outline: none;
        transition: all 0.3s ease;
        box-sizing: border-box;
        background-color: #ffffff;
        width: 100%;
    }

    .download-input:focus {
        border-color: #3b82f6;
        box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
    }

    .download-input::placeholder {
        color: #94a3b8;
    }

    .download-actions {
        margin-top: 10px;
        display: flex;
        justify-content: flex-end;
    }

    .download-btn {
        min-width: 120px;
        height: 42px;
        border: none;
        border-radius: 8px;
        font-size: 14px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }

    .download-btn-save {
        background-color: #3b82f6;
        color: #ffffff;
    }

    .download-btn-save:hover {
        background-color: #2563eb;
        box-shadow: 0 2px 6px rgba(59, 130, 246, 0.25);
    }

    .download-btn-save:active {
        transform: translateY(1px);
    }
</style>

<script>
    (function() {
        window.callParentToast = function(message, options) {
            window.parent.postMessage({
                type: 'toast',
                func: 'showToast',
                message: message,
                options: options || {}
            }, '*');
        };

        function saveConfig() {
            var official1 = document.getElementById('download-official1').value.trim();
            var official2 = document.getElementById('download-official2').value.trim();
            var netdisk1 = document.getElementById('download-netdisk1').value.trim();
            var netdisk2 = document.getElementById('download-netdisk2').value.trim();

            var xhr = new XMLHttpRequest();
            xhr.open('POST', '/admin/Plugin.php', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    try {
                        var data = JSON.parse(xhr.responseText);
                        callParentToast(data.message, {
                            type: data.success ? 'success' : 'error'
                        });
                    } catch (e) {
                        callParentToast('解析响应失败', {
                            type: 'error'
                        });
                    }
                }
            };
            xhr.send('plugin=33_download&plugin_action=save&official1=' + encodeURIComponent(official1) + '&official2=' + encodeURIComponent(official2) + '&netdisk1=' + encodeURIComponent(netdisk1) + '&netdisk2=' + encodeURIComponent(netdisk2));
        }

        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('download-save-btn').addEventListener('click', saveConfig);

            document.querySelectorAll('.download-input').forEach(function(input) {
                input.addEventListener('keypress', function(e) {
                    if (e.key === 'Enter') {
                        saveConfig();
                    }
                });
            });
        });
    })();
</script>
