<template>
  <div :class="$style.example">
    <a-spin tip="正在加载......" size="large" :spinning="spinning" />
  </div>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import ManageConfigService from '@/guide/manage-config-service'
import flowService from '@/webflow/flow-service'
import PageService from '@/common/services/page-service'
import components from './_import-components/km-flow-guide-import'

export default {
  name: 'KmFlowGuide',
  metaInfo: {
    title: '业务类型选择',
  },
  components,
  props: {
    typeCode: {
      type: String,
      default: '',
    },
  },
  data() {
    return { spinning: true }
  },
  watch: {
    $route() {
      this.getFlowId()
    },
  },
  created() {
    this.getFlowId()
  },
  methods: {
    getFlowId() {
      // 有code优先用code，没有的话再看是不是知识库传了业务类型id
      if (this.$route.query.code) {
        const urlCode = this.$route.query.code
        this.getTypeByCode(urlCode)
      } else {
        // 为了不和后面实际获取到的业务类型id搞混，知识新建时的叫businessId
        const urlbusinessId = this.$route.query.businessId
        this.getTypeByBusinessId(urlbusinessId)
      }
    },

    // 定制这个方法主要是为了把km-flow-guide后面所有的参数都传到新建页面
    getTypeByCode(urlCode) {
      const params = {
        categoryCode: urlCode, // 链接取code区分不同业务类型数据
      }
      ManageConfigService.getTypeList(params)
        .then((res) => {
          if (res.status === 200) {
            this.typeData = res.data
            if (this.typeData.length === 1) {
              this.flowId = this.typeData[0].businessFlowId
              if (this.flowId) {
                const businessTypeObj = {
                  businessTypeId: this.typeData[0].id,
                  businessTypeName: this.typeData[0].businessTypeName,
                }

                Object.assign(businessTypeObj, this.$route.query)

                flowService.startProcess(this.flowId, businessTypeObj, {
                  newWindow: false,
                })
              }
            } else {
              this.spinning = false
              // 弹出业务类型
              ManageConfigService.selectYwlx({ typeCode: urlCode, callback: true }).then((data) => {
                if (data) {
                  const businessTypeObj = {
                    businessTypeId: data.id,
                    businessTypeName: data.businessTypeName,
                  }
                  Object.assign(businessTypeObj, this.$route.query)

                  flowService.startProcess(data.businessFlowId, businessTypeObj, {
                    newWindow: false,
                  })
                }
              })
            }
          } else {
            Message.error(res.statusText)
          }
        })
        .catch((error) => {
          /* eslint-disable-next-line */
          console.log(error)
        })
    },

    // 暂时只考虑一个知识分类对应一个业务类型/流程，多个的以后再考虑
    getTypeByBusinessId(businessId) {
      const params = { id: businessId }
      PageService.get(params, 'base/businesstype/oaBaseBusinessType')
        .then((res) => {
          if (res.status === 200) {
            const status = res.data.pageFormData.pageFieldInfos.find((item) => {
              return item.name === 'status'
            })
            if (status.value === '1') {
              const businessTypeName = res.data.pageFormData.pageFieldInfos.find((item) => {
                return item.name === 'businessTypeName'
              })

              const businessTypeId = res.data.pageFormData.pageFieldInfos.find((item) => {
                return item.name === 'id'
              })

              const flowField = res.data.pageFormData.pageFieldInfos.find((item) => {
                return item.name === 'flowField'
              })

              const businessTypeObj = {
                businessTypeId: businessTypeId.value,
                businessTypeName: businessTypeName.value,
              }

              Object.assign(businessTypeObj, this.$route.query)

              flowService.startProcess(JSON.parse(flowField.value)[0].id, businessTypeObj, {
                newWindow: false,
              })
            } else {
              Modal.warning({
                title: '新建失败',
                content: '当前分类审批流程使用的业务类型已停用，请联系管理员。',
                onOk() {
                  window.close()
                },
              })
            }
          } else {
            Message.error(res.statusText)
          }
        })
        .catch((error) => {
          /* eslint-disable-next-line */
          console.log(error)
        })
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.example {
  position: relative;
  height: 100%;
  :global .ant-spin {
    position: absolute;
    top: 50%;
    left: 0;
    width: 100%;
    margin-top: -25px;
    .ant-spin-text {
      /* stylelint-disable-next-line */
      color: #000;
    }
  }
}
</style>
