import type { DictData } from "../../../api/crm";

/** 字典类型常量（与 web 端 @vben/constants DICT_TYPE 保持一致） */
export const DICT = {
  COMMON_STATUS: "common_status",
  MES_PD_PARAM_TYPE: "mes_pd_param_type",
  MES_PD_TEMPLATE_TYPE: "mes_pd_template_type",
  MES_PRO_LINK_TYPE: "mes_pro_link_type",
  MES_TIME_UNIT_TYPE: "mes_time_unit_type",
} as const;

/** 自动编码规则（与 web 端 MesAutoCodeRuleCode 一致） */
export const RULE = {
  PRO_PROCESS_CODE: "PRO_PROCESS_CODE",
  PRO_ROUTE_CODE: "PRO_ROUTE_CODE",
} as const;

export const STATUS_ENABLE = 0;
export const STATUS_DISABLE = 1;

export function dateText(value?: string) {
  return value?.replace("T", " ").slice(0, 16) || "--";
}

export function booleanText(value?: boolean) {
  return value ? "是" : "否";
}

export function numberText(value?: number | string) {
  return value === undefined || value === null || value === "" ? "--" : String(value);
}

export function dictLabel(dicts: DictData[], type: string, value?: number | string, fallback = "--") {
  if (value === undefined || value === null) return fallback;
  return dicts.find(item => item.dictType === type && String(item.value) === String(value))?.label || fallback;
}

export function dictOptions(dicts: DictData[], type: string) {
  return dicts
    .filter(item => item.dictType === type && item.status !== 1)
    .sort((a, b) => (a.sort || 0) - (b.sort || 0));
}

/** 工序状态（common_status）：0 启用 / 1 停用 */
export function statusText(status?: number) {
  return Number(status) === STATUS_DISABLE ? "停用" : "启用";
}

/** 底部选择面板的候选项结构 */
export interface SheetOption {
  key: number | string;
  label: string;
  sub?: string;
  extra?: string;
}

export function statusClass(status?: number) {
  return Number(status) === STATUS_DISABLE ? "tag-disabled" : "tag-enabled";
}

/** 工艺路线颜色圆点样式（甘特图颜色） */
export function colorStyle(color?: string) {
  return color ? `background:${color}` : "background:#d5dde8";
}
