import { Message, Modal } from 'ant-design-vue'
import FlowService from '@/webflow/flow-service'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import LawService from '../law-service'
const DisputeMixins = {
  data() {
    return { stage: [], flag: true, create: false, type: '' }
  },
  created() {
    if (this.$route.params.id && this.$route.params.id === '0') {
      this.create = true
    }
  },
  methods: {
    initDataf() {
      if (this.create) {
        LawService.getProgressData(this.$route.query.id).then((res) => {
          const jfName = res.data[0].text
          this.$refs.webflow.setFieldValue('DISPUTE_ID', this.$route.query.id)
          this.$refs.webflow.setFieldValue('DISPUTE_NAME', jfName)
        })
      }
    },
    initData() {
      if (this.create) {
        LawService.getAlljfStage().then((res) => {
          const stage = []
          res.data.data.forEach((d) => {
            stage.push({
              name: d.STAGE_NAME,
              code: d.STAGE_CODE,
            })
          })
          this.stage = [...stage]
          LawService.getProgressData(this.$route.query.id).then((res) => {
            const jfName = res.data[0].text
            this.$refs.webflow.setFieldValue('DISPUTE_ID', this.$route.query.id)
            this.$refs.webflow.setFieldValue('PROGRESS_CODE', this.$route.query.stage)
            this.$refs.webflow.setFieldValue('DISPUTE_NAME', jfName)
            if (this.$route.query.parentid !== '') {
              this.$refs.webflow.setFieldValue('PARENT_STEP_ID', this.$route.query.parentid)
            }
            if (this.stage.length > 0) {
              const sobj = this.stage.find((s) => s.code === this.$route.query.stage)
              if (sobj) {
                this.$refs.webflow.setFieldValue('STEP_NAME', sobj.name)
              }
            }
          })
        })
      }
    },
    /**
     * 提交后事件
     */
    afterDispatch(obj) {
      // 提交也认为是保存
      obj.defaultPrevented = true
      Message.success({ content: `提交成功`, duration: 1 }).then(() => {
        const e = false
        const t = true
        const snap = this.$refs.webflow.getFormSnap()
        if (
          e &&
          this.$refs.webflow.FlowData.mode === 'EDIT' &&
          (this.$refs.webflow.getFormSnap() !== snap || this.$refs.webflow.disableActionAlert)
        )
          if (!window.confirm('正在编辑文档或正文附件，确认是否离开此网站')) return
        crossWindowWatcher.notifyChange(t)
        return FlowService.unLock(this.$refs.webflow.FlowData.instId).finally(() => {
          crossWindowWatcher.notifyChange(true)
          this.$refs.webflow.close()
          const url = window.location.href
          const obj = { url: url, type: this.type + 'close' }
          window.parent.postMessage(obj, '*')
        })
      })
      return obj
    },
    save() {
      if (this.create) {
        if (window.location.href.indexOf('draft/0') === -1) {
          if (window.parent) {
            const obj = { url: window.location.href, type: this.type }
            window.parent.postMessage(obj, '*')
          }
          this.create = false
        }
      }
    },
    actionBtnClick(evt, { button, FlowData }) {
      if (button.fakeId === 'we-doc$close') {
        this.$refs.webflow.close()
        window.parent.opener = null
        window.parent.open('', '_self')
        window.parent.close()
      }
    },
  },
}
export default DisputeMixins
