/*
 * 管理员后台共享状态：集中保存当前会话、DOM引用和轮询句柄。
 * 作者: @ccfork
 */
export const state = {
    currentSessionId: null,
    currentSession: null,
    lastMessageId: 0,
    lastRecallSeq: 0,
    pollingInterval: null,
    sessionPollInterval: null,
    watchPollInterval: null,
    allSessions: [],
    emojiPanel: null,
    messageActions: null,
    questionsData: [],
    watch: {
        controlSeq: 0,
        applyingRemote: false,
        currentUrl: '',
        localControlUntil: 0,
        remoteLockUntil: 0,
        syncTimer: null
    }
};

export const els = {};

export function initElements() {
    [
        'adminShell', 'usersList', 'searchInput', 'chatPanel', 'currentTitle', 'currentMeta', 'presenceLine',
        'profileModal', 'profileIdText', 'remarkInput', 'introInput', 'saveProfileBtn', 'closeProfileBtn',
        'importFormat', 'importContent', 'importBtn', 'exportBtn', 'clearChatSideInput', 'clearMessagesBtn',
        'deleteSessionBtn', 'adminMessages', 'adminEmojiHost', 'adminEmojiToggle', 'adminMessageInput',
        'adminWatchToggle', 'adminImageBtn', 'adminImageInput', 'adminImageUploadProgress', 'adminWatchPanel', 'adminWatchVideo',
        'adminWatchStatus', 'adminWatchCloseBtn',
        'adminSendBtn', 'resultsBtn', 'resultsPanel', 'resultsContent', 'closeResultsBtn',
        'questionnaireManageBtn', 'questionnairePanel', 'closeQuestionnaireBtn', 'questionnaireEnabledInput',
        'questionnaireSetSelect', 'newQuestionnaireTitleInput', 'createQuestionnaireSetBtn',
        'deleteQuestionnaireSetBtn', 'questionnaireTitleInput', 'saveQuestionnaireTitleBtn',
        'newQuestionText', 'newQuestionOptions', 'addQuestionBtn', 'questionList'
    ].forEach((id) => {
        els[id] = document.getElementById(id);
    });
}

export function setMobileView(view) {
    els.adminShell.classList.remove('show-users', 'show-chat', 'show-results', 'show-questionnaire');
    els.adminShell.classList.add(`show-${view}`);
    document.querySelectorAll('.mobile-tabs button').forEach((btn) => {
        btn.classList.toggle('active', btn.dataset.view === view);
    });
}
