<?php
// 设置默认字符编码为 UTF-8
header('Content-Type: text/html; charset=utf-8');

// 处理表单提交以保存或修改文件
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['note']) && isset($_POST['filename'])) {
        $note = trim($_POST['note']);
        $customFilename = trim($_POST['filename']);

        if (!empty($note) && !empty($customFilename)) {
            // 确保文件名以 .txt 结尾
            if (substr($customFilename, -4) !== '.txt') {
                $customFilename .= '.txt';
            }
            
            // 保存内容到指定文件
            file_put_contents($customFilename, $note);
        }
    } elseif (isset($_POST['delete'])) {
        // 处理删除文件请求
        $deleteFilename = $_POST['delete'];
        if (file_exists($deleteFilename)) {
            unlink($deleteFilename); // 删除文件
        }
    }
}

// 获取文件列表
$files = glob('*.txt');

// 处理加载文件内容请求
$loadedFileContent = '';
$loadedFilename = '';
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['file'])) {
    $loadedFilename = trim($_GET['file']);
    if (file_exists($loadedFilename)) {
        $loadedFileContent = file_get_contents($loadedFilename);
    }
}
?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>记事本</title>
    <style>
        body { font-family: Arial, sans-serif; }
        .container { width: 80%; margin: 0 auto; padding: 20px; }
        textarea { width: 100%; height: 300px; }
        button { margin-top: 10px; }
        ul { list-style-type: none; padding: 0; }
        li { margin: 5px 0; }
        a { text-decoration: none; color: #007BFF; cursor: pointer; }
        .delete-btn { color: red; cursor: pointer; margin-right: 10px; }
        .copy-btn { color: green; cursor: pointer; margin-right: 10px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>记事本</h1>
        <form method="post" action="">
            <textarea name="note" placeholder="输入文本..."><?php echo htmlspecialchars($loadedFileContent); ?></textarea>
            <br>
            <input type="text" name="filename" placeholder="输入文件名" value="<?php echo htmlspecialchars($loadedFilename); ?>" required>
            <button type="submit">保存</button>
        </form>
        
        <h2>保存的文件</h2>
        <ul>
            <?php foreach ($files as $file): ?>
                <li>
                    <a href="?file=<?php echo urlencode($file); ?>">
                        <?php echo htmlspecialchars($file); ?>
                    </a>
                    <button class="delete-btn" onclick="deleteFile('<?php echo htmlspecialchars($file, ENT_QUOTES, 'UTF-8'); ?>')">删除</button>
                    <button class="copy-btn" onclick="copyToClipboard('<?php echo htmlspecialchars($file, ENT_QUOTES, 'UTF-8'); ?>')">复制地址</button>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>

    <script>
        function copyToClipboard(filePath) {
            // 创建一个临时文本输入框
            var tempInput = document.createElement('input');
            tempInput.value = window.location.href.split('?')[0] + '/' + encodeURIComponent(filePath); // 编码文件路径
            document.body.appendChild(tempInput);
            tempInput.select();
            document.execCommand('copy');
            document.body.removeChild(tempInput);
            alert('文件地址已复制到剪贴板：' + tempInput.value);
        }

        function deleteFile(filePath) {
            if (confirm('确定要删除这个文件吗？')) {
                var form = document.createElement('form');
                form.method = 'post';
                form.action = '';
                
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = 'delete';
                input.value = filePath;
                form.appendChild(input);
                
                document.body.appendChild(form);
                form.submit();
            }
        }
    </script>
</body>
</html>
