import { router } from '@/common/router-ready'
import flowService from '@/webflow/flow-service'

export default function({ button, eventScript, context }) {
  return new Promise((resolve) => {
    // 新窗口打开
    const isNewWindow = eventScript.newWindow === '1'
    const url = eventScript.targetLink
    const appModuleId = eventScript.appModuleId
    const formListId = eventScript.formListId
    // 获取设置的默认值
    const { fieldInfos } = context
    const id = fieldInfos.find((f) => f.name === 'id')?.value
    const groupId = fieldInfos.find((f) => f.name === 'subProjectId')?.value
    const query = {}
    if (appModuleId && appModuleId !== '') {
      this.$set(query, 'moduleId', appModuleId)
    }
    if (formListId && formListId !== '') {
      this.$set(query, 'formListId', formListId)
    }
    this.$set(query, 'projectId', id)
    if (groupId && groupId !== '') {
      this.$set(query, 'subProjectId', groupId)
    }
    const oRoute = router.resolve({
      path: url,
      query: {
        ...query,
      },
    })
    if (isNewWindow) {
      window.open(oRoute.href, '_blank')
    } else {
      router.push(oRoute.route.fullPath)
    }

    resolve()
  })
}
