/**
 * 首页看板：模拟数据与回退
 * 当 VUE_APP_DASHBOARD_MOCK=1 或接口失败 / KPI 全 0 且未禁用回退时，用此处数据填充
 */

const palette = {
  line1: '#b45309',
  line2: '#d97706',
  line3: '#2563eb',
  line4: '#6366f1',
  type: ['#d97706', '#f59e0b', '#2563eb', '#1d4ed8'],
  status: ['#d97706', '#2563eb', '#60a5fa']
}

export function getMockKpi() {
  return [
    { title: '今日事件总数', value: 12, suffix: '起', icon: 'el-icon-warning', color: '#b45309', desc: '含全部级别' },
    { title: '处理中事件', value: 4, suffix: '起', icon: 'el-icon-time', color: '#d97706', desc: '实时统计' },
    { title: '已完成事件', value: 8, suffix: '起', icon: 'el-icon-success', color: '#2563eb', desc: '今日完成' },
    { title: '调度进行中', value: 3, suffix: '个', icon: 'el-icon-coordinate', color: '#4f46e5', desc: '执行中的调度' },
    { title: '预案执行中', value: 1, suffix: '个', icon: 'el-icon-document', color: '#64748b', desc: '实时执行' },
    { title: '资源可用率', value: 86, suffix: '%', icon: 'el-icon-box', color: '#1d4ed8', desc: '队伍/车辆/物资' },
    { title: '今日接警', value: 5, suffix: '条', icon: 'el-icon-message', color: '#f59e0b', desc: '值守记录' },
    { title: '预警信息', value: 2, suffix: '条', icon: 'el-icon-bell', color: '#d97706', desc: '近24小时' }
  ]
}

export function getMockEventTrend() {
  return {
    dates: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    level1: [2, 1, 3, 2, 1, 2, 1],
    level2: [5, 6, 4, 7, 5, 6, 5],
    level3: [12, 15, 13, 14, 12, 13, 14],
    level4: [18, 20, 19, 21, 18, 19, 20]
  }
}

/** 柱图：近 7 天事件总量 */
export function getMockEventBar() {
  const t = getMockEventTrend()
  const values = t.dates.map((_, i) => t.level1[i] + t.level2[i] + t.level3[i] + t.level4[i])
  return { dates: t.dates, values, barColor: '#2563eb' }
}

export function getMockEventType() {
  return {
    types: ['自然灾害', '事故灾难', '公共卫生', '社会安全'],
    values: [32, 45, 18, 12]
  }
}

export function getMockEventStatus() {
  return [
    { value: 35, name: '待处理', itemStyle: { color: palette.status[0] } },
    { value: 28, name: '处理中', itemStyle: { color: palette.status[1] } },
    { value: 52, name: '已完成', itemStyle: { color: palette.status[2] } }
  ]
}

export function getMockActivityLists() {
  return {
    latestEvents: [
      { eventNo: 'EVT20240101001', title: '某地区发生交通事故', type: '事故灾难', level: 'II', time: '10:30' },
      { eventNo: 'EVT20240101002', title: '某区域出现火情', type: '事故灾难', level: 'I', time: '09:15' },
      { eventNo: 'EVT20240101003', title: '某路段交通拥堵', type: '事故灾难', level: 'III', time: '08:45' }
    ],
    activeDispatches: [
      { id: 1, title: '紧急调度消防队伍', eventNo: 'EVT20240101001', assignee: '消防支队', progress: 65 },
      { id: 2, title: '调配医疗资源', eventNo: 'EVT20240101002', assignee: '医疗救援队', progress: 45 }
    ],
    todos: [
      { type: 'approval', title: '待审批预案', count: 3 },
      { type: 'warning', title: '库存预警', count: 5 }
    ],
    resourceTypeData: [
      { name: '队伍', value: 45 },
      { name: '车辆', value: 120 },
      { name: '装备', value: 230 },
      { name: '物资', value: 540 },
      { name: '专家', value: 32 }
    ],
    resourceStatusData: [
      { name: '可用', value: 720 },
      { name: '占用', value: 120 },
      { name: '维修', value: 28 },
      { name: '离线', value: 15 }
    ],
    dutyWeeklyData: [12, 18, 9, 16, 22, 15, 13],
    warningList: [
      { title: '某仓库物资低于阈值', time: '10:10', level: 'medium' },
      { title: '某车辆设备离线', time: '09:45', level: 'low' }
    ],
    announcements: [
      { title: '系统计划于周六 02:00-04:00 维护升级', time: '今天 09:20' },
      { title: '关于应急演练的通知', time: '昨天 16:10' }
    ],
    unreadCount: 2
  }
}

function num(v) {
  const n = Number(v)
  return Number.isFinite(n) ? n : 0
}

/** 全部 KPI 数值为 0（忽略占位）时用于触发演示数据回退 */
export function isKpiAllZero(stats) {
  if (!Array.isArray(stats) || !stats.length) return true
  return stats.every(s => num(s.value) === 0)
}

export function isForceMock() {
  return String(process.env.VUE_APP_DASHBOARD_MOCK || '') === '1'
}

export function isZeroMockDisabled() {
  return String(process.env.VUE_APP_DASHBOARD_ZERO_MOCK || 'true') === 'false'
}
