import { downloadFile } from '@/views/disaster/utils';
import type { DisasterLossDetailQuery } from '@/types/disaster-control';
import { exportLossRecordToXML } from '@/api/disaster-control';
import { TASK_STAGE_OPTIONS_DISPOSAL_MANAGEMENT, FIX_STATUS_OPTIONS } from './constant';

// 获取任务阶段
const getTaskStage = (taskStage: number) => {
  return TASK_STAGE_OPTIONS_DISPOSAL_MANAGEMENT.find((item) => item.value === taskStage)?.label;
};

const getfixStatus = (fixStatus: number) => {
  return FIX_STATUS_OPTIONS.find((item) => item.value === fixStatus)?.label;
};

// 导出损失明细
const exportLossRecord = async (data: DisasterLossDetailQuery, name: string) => {
  const res = await exportLossRecordToXML(data);
  if (res.size === 0) return;
  const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
  const url = window.URL.createObjectURL(blob);
  downloadFile(url, `${name}损失明细.xlsx`);
};

const isToolTip = (name: string) => {
  // 计算实际长度，中文算2个字符，英文和符号算1个字符
  const realLength = name.split('').reduce((acc, char) => {
    const charCode = char.charCodeAt(0);
    // 中文范围：0x4E00-0x9FA5
    return acc + (charCode >= 0x4e00 && charCode <= 0x9fa5 ? 2 : 1);
  }, 0);
  return realLength > 20;
};
export { getTaskStage, getfixStatus, exportLossRecord, isToolTip };
