import { queryTaskListApi } from '@/api/project/task';
import { getTagKeyMap } from '@/utils/enum/node';
import emitter from '@/utils/eventBus';
import {
  DEMAND_STATUS,
  DEMAND_STATUS_MAP,
  TASK_CALCULATE_STATUS_OPTIONS,
  TASK_PROCESS_STATUS_OPTIONS,
} from '@/utils/enum/task';
import { getUserId, getUserTenantId } from '@/utils/user';
import { getIdMap } from '../projectDetail/components/project';
import { syncDemandListApi } from '@/api/project/node';
import { ElMessage } from 'element-plus';

export const disposeAttentionParams = (uuid: string, isAttention: boolean) => {
  const addList = [];
  const cancelList = [];
  if (isAttention) {
    addList.push(getUserId());
  } else {
    cancelList.push(getUserId());
  }
  return {
    taskId: uuid,
    addUserIdList: addList,
    cancelUserIdList: cancelList,
  };
};

export const setTaskSearchOptions = (tableRef: any) => {
  tableRef.setOptionsFun('exeStatus', TASK_PROCESS_STATUS_OPTIONS);
  tableRef.setOptionsFun('achieveStatus', TASK_CALCULATE_STATUS_OPTIONS);
};

export const setDemandSearchOptions = (tableRef: any) => {
  tableRef.setOptionsFun('demandStatus', [
    { label: DEMAND_STATUS_MAP[DEMAND_STATUS.NO_SEND], value: DEMAND_STATUS.NO_SEND },
    ...TASK_PROCESS_STATUS_OPTIONS,
  ]);
  tableRef.setOptionsFun('achieveStatus', TASK_CALCULATE_STATUS_OPTIONS);
};

export const disposeTagName = (row: any, nodeType: string) => {
  const tagMap = getTagKeyMap();
  return row[tagMap.get(nodeType)] || '--';
};

export const formatQueryTaskListApi = (params: any) => {
  emitter.emit('TASK_SEARCH', params.project || '');
  params.idMap = getIdMap(params.project, params.phase);
  return queryTaskListApi(params);
};

export const syncDemandList = async () => {
  const res: any = await syncDemandListApi({
    jobnumber: getUserId(),
    tenantid: getUserTenantId(),
    userId: getUserId(),
  });
  if (res.code === 200) {
    ElMessage.success('同步成功');
  }
};
