import axios from '@/common/services/axios-instance'
import crossWindowWatcher from '@/common/services/cross-window-watcher'

export default function ({ button, eventScript, context: { args, dataList, fieldInfos } }) {
  return new Promise((resolve) => {
    const record = args[0]
    axios({
      url: `api/framework/v1/task-form-process/done-pages/${record.instId}`,
      method: 'get',
    }).then((res) => {
      let url = ''
      const todoInfo = res.data.buttons.find((f) => f.buttonId === 'workflow$openTodo')
      if (todoInfo) {
        url = '/sd-webflow/todo-pages/' + todoInfo.attrs.todoId
      } else {
        url = '/sd-webflow/done-pages/' + record.instId
      }
      // window.open(url, '_blank')
      crossWindowWatcher.waitForChanged(url).then((refreshFlag) => {
        if (refreshFlag) {
          this.$refs[this.activeKey][0].refresh(true)
        }
      })
      resolve()
    })
  })
}
