import { syncPhaseApi } from '@/api/project/node';
import { getTagMapList } from '@/utils/task';
import { ElMessage, ElMessageBox } from 'element-plus';

export const disposeAsyncPhase = async (phaseList: any[], currentProjectInfo: any) => {
  if (!currentProjectInfo.projectId) {
    return;
  }
  let needAsync = false;
  if (currentProjectInfo.currentPhase) {
    needAsync = !phaseList.some((phase) => {
      return phase.label === currentProjectInfo.currentPhase;
    });
  }
  await new Promise((resolve) => {
    if (needAsync) {
      ElMessageBox.confirm('当前阶段未同步，是否从EP系统中同步当前阶段及工位信息？', '提示', {
        type: 'warning',
        confirmButtonText: '同步当前阶段',
        cancelButtonText: '取消',
      })
        .then(async () => {
          const tagMap = getTagMapList();
          const res: any = await syncPhaseApi({
            projectId: currentProjectInfo.projectId,
            uuid: currentProjectInfo.uuid,
            nodeCode: currentProjectInfo.nodeCode,
            tagMap,
          });
          if (res.code === 200) {
            ElMessage.success('同步成功');
          } else {
            ElMessage.error(res.message);
          }
          resolve(true);
        })
        .catch(() => {
          resolve(true);
        });
    } else {
      resolve(true);
    }
  });
};
