
import { saveFormData } from '@/api/platform/data/dataTemplate'
import { startFlowFromEdit } from '@/api/platform/bpmn/bpmInst'
// import Print from '@/utils/print'

export default {
  methods: {
    emitEventHandler(actionKey, args) {
      const action = args.attributes.button_type
      this.actionName = actionKey
      switch (action) {
        case 'close': // 关闭窗口
          this.closeDialog()
          break
        case 'save':
          this.handleSave()
          break
        case 'print':
          this.handlePrint()
          break
        case 'sefStartFlow':
          this.handlestartFlowFrom(args.attributes.deflow)
          break
        case 'custom':
        default:
          break
      }
    },
    handlestartFlowFrom(flowKey) {
      // 表单提交校验
      if (this.$utils.isEmpty(flowKey)) {
        this.$notify({
          message: '请检查是否绑定流程！',
          type: 'success'
        })
        return
      }
      this.formSubmitVerify((result, errorMsg) => {
        if (!result) {
          this.$message.closeAll()
          return this.$message.warning(errorMsg)
        }
        if (flowKey) {
          this.$confirm('确定启动流程吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.handlestartFlowFromEdit(flowKey)
          }).catch(() => {})
        } else {
          this.bpmDefDialogVisible = true
        }
      })
    },
    handleDialogActionEvent(key, data) {
      if (key === 'clean') {
        this.dialogValue = {}
      } else if (key === 'confirm') {
        this.handlestartFlowFromEdit(data.defKey)
      }
    },
    handlestartFlowFromEdit(flowKey) {
      const formData = JSON.stringify(this.getFormData())
      startFlowFromEdit({
        data: formData,
        flowKey: flowKey,
        pk: this.pkValue || ''
      }).then(response => {
        this.bpmDefDialogVisible = false
        this.$notify({
          message: '流程启动成功！',
          type: 'success'
        })
        // 后置事件
        this.afterScript(this.actionName, {
          data: response.data,
          variables: response.variables
        }, () => {
          this.callbackPage()
        })
      }).catch(() => {
        this.bpmDefDialogVisible = false
      })
    },
    // 保存
    handleSave() {
      // 验证表单是否正确
      this.validate(valid => {
        if (valid) {
          // 表单提交校验
          this.formSubmitVerify((result, errorMsg) => {
            if (!result) {
              this.$notify({
                type: 'warning',
                message: errorMsg
              })
              return
            }
            // 保存数据
            this.saveData()
          })
        } else {
          this.formErrorToast()
        }
      })
    },
    // 保存数据
    saveData() {
      // 表单数据
      const formData = this.getFormData()
      const jsonData = {
        boCode: this.boCode,
        version: this.version,
        formKey: this.formKey,
        pk: this.pkValue,
        data: JSON.stringify(formData)
      }
      saveFormData(jsonData).then(response => {
        this.$notify({
          message: '保存成功！',
          type: 'success'
        })
        // 后置事件
        this.afterScript(this.actionName, {
          data: response.data,
          variables: response.variables
        }, () => {
          this.callbackPage()
        })
      }).catch(() => {
      })
    },
    /**
     * 打印
     */
    handlePrint() {
      const printTemplateId = null
      // const printTemplateId = this.attributes.printTemplateId
      if (this.$utils.isNotEmpty(printTemplateId)) {
        // 打开打印模版页面
      } else {
      //  window.print()
        // setTimeout(() => {
        //   Print(this.$refs.formrender.$el, {
        //     title: '',
        //     callback: () => {
        //     }
        //   })
        // }, 300)
      }
    }

  }
}
