document.addEventListener('DOMContentLoaded', function () {
    // 动态加载内容
    document.querySelectorAll('nav a').forEach(link => {
        link.addEventListener('click', function (e) {
            e.preventDefault();
            const target = this.getAttribute('href').substring(1);
            loadContent(target);
        });
    });

    function loadContent(section) {
        document.querySelectorAll('.content-section').forEach(div => div.style.display = 'none');
        document.getElementById(section).style.display = 'block';
    }

    // 默认加载查看任务页面
    loadContent('view-tasks');

    // 获取任务列表
    function fetchTasks() {
        fetch('/checkurl/get_cron_list')
            .then(response => response.json())
            .then(data => {
                const taskList = document.getElementById('task-list').getElementsByTagName('tbody')[0];
                taskList.innerHTML = '';
                data.cron_jobs.forEach(job => {
                    const row = taskList.insertRow();
                    row.insertCell().textContent = job.id;
                    row.insertCell().textContent = job.name;
                    row.insertCell().textContent = job.url;
                    row.insertCell().textContent = job.expected_key;
                    row.insertCell().textContent = job.interval;
                });
            });
    }

    // 页面加载时获取任务列表
    fetchTasks();

    // 添加任务
    document.getElementById('add-task-form').addEventListener('submit', function (e) {
        e.preventDefault();
        const formData = new FormData(this);
        fetch('/checkurl/add_cron', {
            method: 'POST',
            body: JSON.stringify({
                name: formData.get('name'),
                url: formData.get('url'),
                expected_key: formData.get('expected_key'),
                interval: parseInt(formData.get('interval'))
            }),
            headers: {
                'Content-Type': 'application/json'
            }
        })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                fetchTasks(); // 刷新任务列表
            });
    });

    // 删除任务
    document.getElementById('delete-task-form').addEventListener('submit', function (e) {
        e.preventDefault();
        const taskId = document.getElementById('task-id').value;
        fetch('/checkurl/del_cron', {
            method: 'POST',
            body: JSON.stringify({ id: parseInt(taskId) }),
            headers: {
                'Content-Type': 'application/json'
            }
        })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                fetchTasks(); // 刷新任务列表
            });
    });

    // 获取配置
    function fetchConfig() {
        fetch('/checkurl/get_config')
            .then(response => response.json())
            .then(data => {
                const configDetails = document.getElementById('config-details');
                configDetails.innerHTML = `
                    <p><strong>Access Token:</strong> ${data.config.access_token}</p>
                    <p><strong>关键词:</strong> ${data.config.keyword}</p>
                    <p><strong>超时时间:</strong> ${data.config.timeout} 秒</p>
                    <p><strong>最大时间:</strong> ${data.config.max_time} 秒</p>
                    <p><strong>最大文件大小:</strong> ${data.config.max_file_size} MB</p>
                `;
            });
    }

    // 页面加载时获取配置
    fetchConfig();

    // 修改配置
    document.getElementById('edit-config-form').addEventListener('submit', function (e) {
        e.preventDefault();
        const formData = new FormData(this);
        fetch('/checkurl/config_cron', {
            method: 'POST',
            body: JSON.stringify({
                AccessToken: formData.get('access_token'),
                Keyword: formData.get('keyword'),
                Timeout: parseInt(formData.get('timeout')),
                MaxTime: parseInt(formData.get('max_time')),
                MaxFileSize: parseInt(formData.get('max_file_size'))
            }),
            headers: {
                'Content-Type': 'application/json'
            }
        })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                fetchConfig(); // 刷新配置显示
            });
    });

    // 响应式导航栏
    const navToggle = document.createElement('div');
    navToggle.innerHTML = '&#9776;'; // 汉堡图标
    navToggle.style.color = 'white';
    navToggle.style.cursor = 'pointer';
    navToggle.style.display = 'none'; // 默认隐藏
    navToggle.style.fontSize = '24px';
    navToggle.style.position = 'absolute';
    navToggle.style.right = '10px';
    navToggle.style.top = '10px';
    document.querySelector('nav').prepend(navToggle);

    navToggle.addEventListener('click', function () {
        const navUl = document.querySelector('nav ul');
        if (navUl.style.display === 'flex') {
            navUl.style.display = 'none';
        } else {
            navUl.style.display = 'flex';
        }
    });

    // 在屏幕宽度小于768px时显示汉堡图标
    window.addEventListener('resize', function () {
        if (window.innerWidth <= 768) {
            navToggle.style.display = 'block';
            document.querySelector('nav ul').style.display = 'none';
        } else {
            navToggle.style.display = 'none';
            document.querySelector('nav ul').style.display = 'flex';
        }
    });

    // 初始化时检查屏幕宽度
    if (window.innerWidth <= 768) {
        navToggle.style.display = 'block';
        document.querySelector('nav ul').style.display = 'none';
    }
});