import { message } from 'ant-design-vue'
import XmSingleDataService from './xm-single-data-service'

export default function({ button, eventScript, context: { args, dataList, fieldInfos } }) {
  return new Promise((resolve) => {
    const record = args[0]
    let href = ''
    const orgId = fieldInfos.find((f) => f.name === 'OWNER_ORG_ID')?.value
    if (orgId) {
      // 判断是否有查看权限
      XmSingleDataService.lawTeamDetail(orgId)
        .then((res) => {
          if (res.data) {
            // 打开详情页
            if (record.openLink) {
              // href =
              //   '#/sd-webflow/' + task.openLink?.split('task-form-process/')[1] + '/' + task[task.openParam]
              href = '#/sd-webflow/done-pages/' + record.instId
            }
            window.open(href, '_blank')
          } else {
            message.error('您没有查看权限。')
          }
        })
        .catch((e) => {
          // eslint-disable-next-line no-console
          console.log(e)
          message.error('出错了，请联系管理员')
        })
        .finally(() => {
          resolve()
        })
    } else {
      // 所属组织机构没有值时直接打开
      if (record.openLink) {
        href = '#/sd-webflow/done-pages/' + record.instId
      }
      window.open(href, '_blank')
      resolve()
    }
  })
}
