<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MeoW</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"], input[type="url"], textarea {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            padding: 10px 15px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            margin-top: 15px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            display: none;
        }
        .success {
            color: green;
        }
        .error {
            color: red;
        }
        .content-row {
            display: flex;
            align-items: center;
            justify-content: start;
        }
        .content-row textarea {
            flex: 1;
            margin-right: 10px;
        }
        .content-row label {
            margin-bottom: 0;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .header h1 {
            margin: 0;
        }
        .header a {
            font-size: 16px;
            color: #007BFF;
            text-decoration: none;
        }
        .header a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>MeoW</h1>
        <a href="https://www.chuckfang.com/MeoW/api_doc.html" target="_blank">推送消息API文档</a>
    </div>
    <h3>给你的好友发推送</h3>
    <form id="requestForm">
        <label for="nickname">对方昵称：</label>
        <input type="text" id="nickname" name="nickname" required placeholder="请输入对方昵称">

        <label for="title">标题：</label>
        <input type="text" id="title" name="title" required placeholder="请输入标题">

        <div class="content-row">
            <label for="content">内容：</label>
            <label for="longTextSwitch">代码模式：</label>
            <input type="checkbox" id="longTextSwitch" name="longTextSwitch">
        </div>
        <textarea id="content" name="content" rows="4" required placeholder="请输入内容"></textarea>

        <label for="url">链接（可选）：</label>
        <input type="url" id="url" name="url" placeholder="请输入链接（可选）">

        <button type="submit">发送</button>
    </form>
    <div class="message" id="message"></div>
</div>

<script>
    document.getElementById('requestForm').addEventListener('submit', function(event) {
        event.preventDefault();

        const nickname = document.getElementById('nickname').value;
        const title = document.getElementById('title').value;
        const content = document.getElementById('content').value;
        const url = document.getElementById('url').value;
        const isLongText = document.getElementById('longTextSwitch').checked;

        // 如果链接为空，不添加到URL中
        let apiUrl = `./${nickname}/${title}/${content}`;
        if (url) {
            apiUrl += `?url=${encodeURIComponent(url)}`;
        }

        let options = {
            method: 'GET'
        };

        if (isLongText) {
            apiUrl = `./${nickname}/${title}`;
            if (url) {
                apiUrl += `?url=${encodeURIComponent(url)}`;
            }
            options = {
                method: 'POST',
                headers: {
                    'Content-Type': 'text/plain'
                },
                body: content
            };
        }

        fetch(apiUrl, options)
            .then(response => response.json()) // 解析返回的 JSON 数据
            .then(data => {
                const messageDiv = document.getElementById('message');
                messageDiv.style.display = 'block';

                if (data.status === 200 && data.data === true) {
                    messageDiv.className = 'message success';
                    messageDiv.textContent = data.msg;
                } else {
                    messageDiv.className = 'message error';
                    messageDiv.textContent = data.msg || '发送失败，请检查输入';
                }
            })
            .catch(error => {
                const messageDiv = document.getElementById('message');
                messageDiv.style.display = 'block';
                messageDiv.className = 'message error';
                messageDiv.textContent = '请求失败，请检查网络或稍后重试';
            });
    });
</script>
</body>
</html>