import axios from '@/common/services/axios-instance'
import Vue from 'vue'
import ywlxGuide from '@/guide/sd-ywlx-guide.vue'
import flowService from '@/webflow/flow-service'
import store from '@/common/store'
class ManageConfigService {
  getTypeList(params) {
    return axios({
      url: 'api/framework/v1/businesstype-manage/businesstypes/list-auth',
      method: 'get',
      params,
    })
  }

  getRedheadsList(params) {
    return axios({
      url: `api/framework/v1/redhead-manager/doctype/${params}/redheads`,
      method: 'get',
    })
  }

  // 业务类型组件
  selectYwlx(option = {}) {
    let mixin
    const p = new Promise(function(resolve, reject) {
      mixin = {
        data() {
          return {
            typeCode: option.typeCode,
            moduleId: option.moduleId,
            attachment: option.attachment, // 收转发使用
            title: option.title,
            autoSubmit: !!option.autoSubmit, // 业务类型只有一个时，自动确定
          }
        },
        methods: {
          SubmitClick() {
            // option接收callback参数，为true走then方法处理自己逻辑。不传或者传false走下面统一跳转逻辑
            const data = Object.assign(this.data, option)
            if (option.callback === true) {
              resolve(data)
            } else {
              if (data) {
                if (data.attachment) {
                  flowService.startProcessByBusinessTypeId(
                    data.id,
                    {
                      title: data.title,
                    },
                    {
                      newWindow: false,
                      tempField: { attachment: data.attachment },
                    }
                  )
                } else {
                  flowService.startProcessByBusinessTypeId(
                    data.id,
                    {},
                    {
                      newWindow: false,
                    }
                  )
                }
                this.$destroy()
              } else {
                // Message.error('请选择业务类型')
              }
            }
            this.$destroy()
          },
          handleCancel() {
            if (!this.attachment) {
              resolve()
            }
            this.$destroy()
          },
        },
      }
    })
    const mixins = { mixins: [mixin] }
    const c = { ...ywlxGuide, ...mixins, store }
    const vm = new Vue(c).$mount()
    document.body.appendChild(vm.$el)
    return p
  }
}

export default new ManageConfigService()
