`;
}).join('');
if (this.elements.mobileGroupContainer) {
this.elements.mobileGroupContainer.innerHTML = mobileListHtml;
}
}
// 事件处理器和UI更新函数,现在完全由 state 驱动
handleGroupCardClick(event) {
const clickedCard = event.target.closest('[data-group-id]');
if (!clickedCard) return;
const groupId = parseInt(clickedCard.dataset.groupId, 10);
if (this.state.activeGroupId !== groupId) {
this.state.activeGroupId = groupId;
this.renderGroupList();
this.updateDashboard(); // updateDashboard 现在会处理 API key 的加载
}
if (window.innerWidth < 1024) {
this._closeMobileMenu();
}
}
// [NEW HELPER METHOD] Centralizes the logic for closing the mobile menu.
_closeMobileMenu() {
const menu = this.elements.groupListCollapsible;
if (!menu) return;
menu.classList.remove('mobile-group-menu-active');
menu.classList.add('hidden');
}
updateDashboard() {
const activeGroup = this.state.groups.find(g => g.id === this.state.activeGroupId);
if (activeGroup) {
if (this.elements.dashboardTitle) {
this.elements.dashboardTitle.textContent = `${activeGroup.display_name}`;
}
if (this.elements.mobileActiveGroupDisplay) {
this.elements.mobileActiveGroupDisplay.innerHTML = `
${activeGroup.display_name}
当前选择
`;
}
// 更新 Dashboard 时,加载对应的 API Keys
this.apiKeyList.setActiveGroup(activeGroup.id, activeGroup.display_name);
this.apiKeyList.loadApiKeys(activeGroup.id);
} else {
if (this.elements.dashboardTitle) this.elements.dashboardTitle.textContent = 'No Group Selected';
if (this.elements.mobileActiveGroupDisplay) this.elements.mobileActiveGroupDisplay.innerHTML = `
请选择一个分组
`;
// 如果没有选中的分组,清空 API Key 列表
this.apiKeyList.loadApiKeys(null);
}
}
/**
* Handles the saving of a key group with modern toast notifications.
* @param {object} groupData The data collected from the KeyGroupModal.
*/
async handleSaveGroup(groupData) {
const isEditing = !!groupData.id;
const endpoint = isEditing ? `/admin/keygroups/${groupData.id}` : '/admin/keygroups';
const method = isEditing ? 'PUT' : 'POST';
console.log(`[CONTROLLER] ${isEditing ? 'Updating' : 'Creating'} group...`, { endpoint, method, data: groupData });
try {
const response = await apiFetch(endpoint, {
method: method,
body: JSON.stringify(groupData),
noCache: true
});
const result = await response.json();
if (!result.success) {
throw new Error(result.message || 'An unknown error occurred on the server.');
}
if (isEditing) {
console.log(`[CACHE INVALIDATION] Deleting cached details for group ${groupData.id}.`);
delete this.state.groupDetailsCache[groupData.id];
}
if (!isEditing && result.data && result.data.id) {
this.state.activeGroupId = result.data.id;
}
toastManager.show(`分组 "${groupData.display_name}" 已成功保存。`, 'success');
await this.loadKeyGroups(true);
} catch (error) {
console.error(`Failed to save group:`, error.message);
toastManager.show(`保存失败: ${error.message}`, 'error');
throw error;
}
}
/**
* Opens the KeyGroupModal for editing, utilizing a cache-then-fetch strategy.
* @param {number} groupId The ID of the group to edit.
*/
async openEditGroupModal(groupId) {
// Step 1: Check the details cache first.
if (this.state.groupDetailsCache[groupId]) {
console.log(`[CACHE HIT] Using cached details for group ${groupId}.`);
// If details exist, open the modal immediately with the cached data.
this.keyGroupModal.open(this.state.groupDetailsCache[groupId]);
return;
}
// Step 2: If not in cache, fetch from the API.
console.log(`[CACHE MISS] Fetching details for group ${groupId}.`);
try {
// NOTE: No complex UI spinners on the button itself. The user just waits a moment.
const endpoint = `/admin/keygroups/${groupId}`;
const responseData = await apiFetchJson(endpoint, { noCache: true });
if (responseData && responseData.success) {
const groupDetails = responseData.data;
// Step 3: Store the newly fetched details in the cache.
this.state.groupDetailsCache[groupId] = groupDetails;
// Step 4: Open the modal with the fetched data.
this.keyGroupModal.open(groupDetails);
} else {
throw new Error(responseData.message || 'Failed to load group details.');
}
} catch (error) {
console.error(`Failed to fetch details for group ${groupId}:`, error);
alert(`无法加载分组详情: ${error.message}`);
}
}
async openRequestSettingsModal() {
if (!this.state.activeGroupId) {
modalManager.showResult(false, "请先选择一个分组。");
return;
}
// [重構] 簡化後的邏輯:獲取數據,然後調用子模塊的 open 方法
console.log(`Opening request settings for group ID: ${this.state.activeGroupId}`);
const data = {}; // 模擬從API獲取數據
this.requestSettingsModal.open(data);
}
/**
* @param {object} data The data collected from the RequestSettingsModal.
*/
async handleSaveRequestSettings(data) {
if (!this.state.activeGroupId) {
throw new Error("No active group selected.");
}
console.log(`[CONTROLLER] Saving request settings for group ${this.state.activeGroupId}:`, data);
// 此處執行API調用
// await apiFetch(...)
// 成功後可以觸發一個全局通知或刷新列表
// this.loadKeyGroups();
return Promise.resolve(); // 模擬API調用成功
}
initCustomSelects() {
const customSelects = document.querySelectorAll('.custom-select');
customSelects.forEach(select => new CustomSelect(select));
}
_initBatchActions() {}
/**
* Sends the new group UI order to the backend API.
* @param {Array