import { getApproveFlowIdApi, getHkTokenApi } from '@/api/project/node';
import { enableConfigByTenant, TENANT_ENUM } from '@/tenants/tenant';
import { NODE_TYPE } from '@/utils/enum/node';
import { getUserId } from '@/utils/user';
import { ElMessage } from 'element-plus';

export const skipApprovePageBtns = (projectCode: string) => {
  if (enableConfigByTenant([TENANT_ENUM.LYRIC])) {
    return [
      {
        title: '审批跳转',
        type: 'primary',
        click: (row: any) => {
          skipApproveLyric(row, projectCode);
        },
        hide: (row: any) => {
          return row.nodeType !== NODE_TYPE.WORKSPACE || !enableConfigByTenant([TENANT_ENUM.LYRIC]);
        },
      },
    ];
  }
  return [];
};

const skipApproveLyric = async (row: any, projectCode: string) => {
  const res: any = await getApproveFlowIdApi({
    jobNo: getUserId(),
    queryColumn: 'flow_inst_id',
    conditionColumn: {
      project_number: projectCode,
      station_no: row.nodeCode,
      status: '处理中',
    },
  });
  if (res.code === 200) {
    if (res.data.length > 0) {
      const approveFlowId = res.data[0].flow_inst_id;
      const tokenRes: any = await getHkTokenApi({});
      if (tokenRes.code === 200) {
        window.open(
          'https://lyh.haikuicloud.com/v15/app/flow-process?instId=' +
            approveFlowId +
            '&hk_user_token=' +
            tokenRes.data
        );
      } else {
        ElMessage.error(tokenRes.message);
      }
    } else {
      ElMessage.error('没有找到审批流程！');
    }
  }
};

export const skipDesignLyric = () => {
  window.open('http://192.168.30.42:10001');
};
