(function attachSidepanelAccountRecordsManager(globalScope) { function createAccountRecordsManager(context = {}) { const { state, dom, helpers, runtime, constants = {}, } = context; const displayTimeZone = constants.displayTimeZone || 'Asia/Shanghai'; const pageSize = Math.max(1, Math.floor(Number(constants.pageSize) || 10)); let currentPage = 1; function escapeHtml(value) { if (typeof helpers.escapeHtml === 'function') { return helpers.escapeHtml(String(value || '')); } return String(value || ''); } function normalizeTimestamp(value) { const timestamp = Date.parse(String(value || '')); return Number.isFinite(timestamp) ? timestamp : 0; } function getAccountRunRecords(currentState = state.getLatestState()) { return (Array.isArray(currentState?.accountRunHistory) ? currentState.accountRunHistory : []) .filter((item) => item && typeof item === 'object') .slice() .sort((left, right) => normalizeTimestamp(right.finishedAt) - normalizeTimestamp(left.finishedAt)); } function summarizeAccountRunHistory(records = []) { return records.reduce((summary, record) => { summary.total += 1; if (record.finalStatus === 'success') { summary.success += 1; } else if (record.finalStatus === 'failed') { summary.failed += 1; } else if (record.finalStatus === 'stopped') { summary.stopped += 1; } summary.retryTotal += Math.max(0, Math.floor(Number(record.retryCount) || 0)); return summary; }, { total: 0, success: 0, failed: 0, stopped: 0, retryTotal: 0, }); } function formatAccountRecordTime(value) { const date = new Date(value); if (Number.isNaN(date.getTime())) { return '--:--'; } const now = new Date(); const sameYear = date.getFullYear() == now.getFullYear(); const sameDay = date.toDateString() === now.toDateString(); if (sameDay) { return date.toLocaleTimeString('zh-CN', { hour12: false, hour: '2-digit', minute: '2-digit', timeZone: displayTimeZone, }); } return date.toLocaleString('zh-CN', { hour12: false, month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', ...(sameYear ? {} : { year: '2-digit' }), timeZone: displayTimeZone, }).replace(/\//g, '-'); } function getStatusMeta(record = {}) { if (record.finalStatus === 'success') { return { kind: 'success', label: '成功' }; } if (record.finalStatus === 'stopped') { return { kind: 'stopped', label: '停止' }; } return { kind: 'failed', label: '失败' }; } function getRecordSummaryText(record = {}) { if (record.finalStatus === 'success') { return '流程完成'; } return String(record.failureLabel || '').trim() || '流程失败'; } function createStatChip(label, value, className = '') { return `${escapeHtml(String(value))}${escapeHtml(label)}`; } function updateHeader(records) { if (!dom.accountRecordsMeta || !dom.accountRecordsStats) { return; } if (!records.length) { dom.accountRecordsMeta.textContent = '暂无邮箱记录'; } else { dom.accountRecordsMeta.textContent = `共 ${records.length} 条,最近更新于 ${formatAccountRecordTime(records[0]?.finishedAt)}`; } const summary = summarizeAccountRunHistory(records); dom.accountRecordsStats.innerHTML = [ createStatChip('总', summary.total), createStatChip('成', summary.success, 'is-success'), createStatChip('失', summary.failed, 'is-failed'), createStatChip('停', summary.stopped, 'is-stopped'), createStatChip('重试', summary.retryTotal, 'is-retry'), ].join(''); } function updatePagination(totalRecords) { const totalPages = totalRecords > 0 ? Math.ceil(totalRecords / pageSize) : 0; if (totalPages === 0) { currentPage = 1; } else if (currentPage > totalPages) { currentPage = totalPages; } else if (currentPage < 1) { currentPage = 1; } if (dom.accountRecordsPageLabel) { dom.accountRecordsPageLabel.textContent = totalPages > 0 ? `${currentPage} / ${totalPages}` : '0 / 0'; } if (dom.btnAccountRecordsPrev) { dom.btnAccountRecordsPrev.disabled = totalPages <= 1 || currentPage <= 1; } if (dom.btnAccountRecordsNext) { dom.btnAccountRecordsNext.disabled = totalPages <= 1 || currentPage >= totalPages; } if (dom.btnClearAccountRecords) { dom.btnClearAccountRecords.disabled = totalRecords === 0; } return totalPages; } function renderRecordList(records = []) { if (!dom.accountRecordsList) { return; } const totalPages = updatePagination(records.length); if (!records.length) { dom.accountRecordsList.innerHTML = '