<?php
/**
 * 管理员后台入口：只负责登录校验、页面骨架与模块资源加载。
 * 作者: @ccfork
 */
session_start();
$admin_password = 'admin123';
if (!isset($_GET['auth']) || $_GET['auth'] !== $admin_password) {
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['password'] ?? '') === $admin_password) {
        header('Location: chat_admin.php?auth=' . $admin_password);
        exit;
    }
    ?>
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
        <title>管理员登录</title>
        <link rel="stylesheet" href="admin/assets/login.css?v=20260615f">
    </head>
    <body>
        <div class="login-card">
            <h2>🌸 管理员入口</h2>
            <form method="post">
                <input type="password" name="password" placeholder="请输入密码" autofocus>
                <button type="submit">进入小纸条后台</button>
            </form>
        </div>
    </body>
    </html>
    <?php
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>小纸条后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <link rel="stylesheet" href="chat_emoji.css?v=20260615c">
    <link rel="stylesheet" href="admin/assets/chat-admin.css?v=20260615g">
    <link rel="stylesheet" href="image_viewer.css?v=20260615a">
    <link rel="stylesheet" href="scrollbar.css?v=20260615a">
</head>
<body>
<div class="admin-shell show-users" id="adminShell">
    <aside class="users-panel">
        <div class="panel-header">
            <h1>🌸 用户列表</h1>
            <div class="panel-sub">小纸条、问卷访问和未读提醒都在这里</div>
            <div class="admin-toolbar">
                <input class="search-input" id="searchInput" type="text" placeholder="搜索用户ID / IP / 已备注 / 未备注">
            </div>
        </div>
        <div class="users-list" id="usersList"></div>
        <div class="users-footer">
            <button class="list-tool-btn" id="questionnaireManageBtn" type="button">
                <svg viewBox="0 0 24 24" aria-hidden="true">
                    <path d="M4 5h16"></path>
                    <path d="M4 12h10"></path>
                    <path d="M4 19h7"></path>
                    <path d="M17 15v6"></path>
                    <path d="M14 18h6"></path>
                </svg>
                <span>问卷管理</span>
            </button>
        </div>
    </aside>

    <main class="main-panel">
        <section class="chat-panel" id="chatPanel">
            <header class="chat-header">
                <div class="chat-user">
                    <h2><button class="chat-id-btn" id="currentTitle" type="button">选择一个用户</button></h2>
                    <p id="currentMeta">左侧点开后就可以写小纸条</p>
                </div>
                <div class="chat-actions">
                    <button class="action-icon-btn" id="resultsBtn" type="button" aria-label="问卷" title="问卷">
                        <svg viewBox="0 0 24 24" aria-hidden="true">
                            <path d="M9 4h6l1 2h3v14H5V6h3z"></path>
                            <path d="M9 11h6"></path>
                            <path d="M9 15h4"></path>
                        </svg>
                    </button>
                </div>
            </header>

            <div class="presence-line" id="presenceLine">状态会在这里冒泡～</div>
            <section class="watch-panel admin-watch-panel" id="adminWatchPanel" hidden>
                <video id="adminWatchVideo" playsinline controls></video>
                <div class="watch-toolbar">
                    <div class="watch-status" id="adminWatchStatus">一起看准备中～</div>
                    <button class="watch-close-btn" id="adminWatchCloseBtn" type="button">结束一起看</button>
                </div>
            </section>

            <div class="messages-area" id="adminMessages">
                <div class="empty-state">先从用户列表里选一个会话～</div>
            </div>

            <div class="input-area">
                <div id="adminEmojiHost"></div>
                <div class="upload-progress" id="adminImageUploadProgress" hidden>
                    <div class="upload-progress-bar"><span></span></div>
                    <p>图片发送中 0%</p>
                </div>
                <div class="input-wrapper">
                    <button class="tool-btn" id="adminEmojiToggle" type="button" aria-label="打开表情"><i class="far fa-face-smile"></i></button>
                    <button class="tool-btn" id="adminWatchToggle" type="button" aria-label="一起看" title="一起看"><i class="fas fa-play"></i></button>
                    <button class="tool-btn" id="adminImageBtn" type="button" aria-label="发送图片" title="图片"><i class="far fa-image"></i></button>
                    <input id="adminImageInput" class="hidden-file-input" type="file" accept="image/jpeg,image/png,image/gif,image/webp">
                    <textarea id="adminMessageInput" class="message-input" rows="1" placeholder="写一张小纸条..."></textarea>
                    <button class="send-btn" id="adminSendBtn" type="button" aria-label="发送"><i class="fas fa-paper-plane"></i></button>
                </div>
            </div>
        </section>
    </main>

    <aside class="side-panel results-panel" id="resultsPanel">
        <div class="side-header">
            <h2>问卷结果</h2>
            <button class="icon-btn" id="closeResultsBtn" type="button" aria-label="关闭结果"><i class="fas fa-times"></i></button>
        </div>
        <div class="side-content" id="resultsContent"></div>
    </aside>

    <aside class="side-panel questionnaire-panel" id="questionnairePanel">
        <div class="side-header">
            <h2>问卷管理</h2>
            <button class="icon-btn" id="closeQuestionnaireBtn" type="button" aria-label="关闭问卷管理"><i class="fas fa-times"></i></button>
        </div>
        <div class="side-content">
            <div class="question-set-tools">
                <select id="questionnaireSetSelect" aria-label="当前题套"></select>
                <input id="newQuestionnaireTitleInput" type="text" placeholder="新题套标题">
                <div class="question-set-actions">
                    <button class="cute-btn secondary" id="createQuestionnaireSetBtn" type="button">新建题套</button>
                    <button class="cute-btn danger-btn" id="deleteQuestionnaireSetBtn" type="button">删除题套</button>
                </div>
            </div>
            <label class="switch-row">
                <input id="questionnaireEnabledInput" type="checkbox" checked>
                <span>启用问卷</span>
            </label>
            <div class="question-form">
                <input id="questionnaireTitleInput" type="text" placeholder="当前题套标题">
                <button class="cute-btn secondary" id="saveQuestionnaireTitleBtn" type="button">保存标题</button>
            </div>
            <div class="question-form">
                <input id="newQuestionText" type="text" placeholder="新增题目">
                <textarea id="newQuestionOptions" placeholder="每行一个选项"></textarea>
                <button class="cute-btn" id="addQuestionBtn" type="button">添加题目</button>
            </div>
            <div class="question-list" id="questionList"></div>
        </div>
    </aside>
</div>

<div class="profile-modal" id="profileModal">
    <div class="profile-dialog">
        <h2>用户小档案</h2>
        <div class="profile-id" id="profileIdText"></div>
        <input id="remarkInput" type="text" placeholder="备注名">
        <textarea id="introInput" placeholder="简介 / 观察备注"></textarea>
        <div class="profile-import">
            <div class="profile-section-title">聊天记录</div>
            <div class="import-card">
                <div class="import-row">
                    <select id="importFormat" aria-label="记录格式">
                        <option value="json">JSON</option>
                        <option value="md">Markdown</option>
                    </select>
                    <button class="cute-btn secondary" id="exportBtn" type="button">导出</button>
                    <button class="cute-btn" id="importBtn" type="button">导入</button>
                </div>
                <textarea id="importContent" placeholder="这里可以粘贴或生成聊天记录"></textarea>
            </div>
        </div>
        <div class="profile-danger">
            <div class="profile-section-title">记录操作</div>
            <div class="clear-row">
                <label class="clear-check">
                    <input id="clearChatSideInput" type="checkbox" checked>
                    同时清空 chat 端记录
                </label>
                <button class="cute-btn danger-btn" id="clearMessagesBtn" type="button">清空</button>
                <button class="cute-btn danger-btn" id="deleteSessionBtn" type="button">删除会话</button>
            </div>
        </div>
        <div class="profile-actions">
            <button class="cute-btn" id="saveProfileBtn" type="button">保存</button>
            <button class="cute-btn secondary" id="closeProfileBtn" type="button">关闭</button>
        </div>
    </div>
</div>

<nav class="mobile-tabs">
    <button type="button" data-view="users" class="active">用户</button>
    <button type="button" data-view="chat">会话</button>
    <button type="button" data-view="results">结果</button>
    <button type="button" data-view="questionnaire">问卷</button>
</nav>

<script src="chat_emoji.js?v=20260615c"></script>
<script src="message_actions.js?v=20260615a"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.5.18/dist/hls.min.js"></script>
<script src="watch_player.js?v=20260615a"></script>
<script src="image_viewer.js?v=20260615a"></script>
<script>
    window.ADMIN_CONFIG = {
        auth: <?php echo json_encode($admin_password, JSON_UNESCAPED_UNICODE); ?>
    };
</script>
<script type="module" src="admin/modules/app.js?v=20260615g"></script>
</body>
</html>
