<script>
import components from './_import-components/risk-dept-sgtx-mixins-import'

export default {
  name: 'RiskDeptSgtxMixins',
  components,
  data() {
    return {
      // ======手动录入主送单位======
      ModalForm: {
        orgName: '', // 单位名称
      },
      currentEntryField: '', // 当前录入的表单字段
      ModalVisible: false,
      OrgNameData: [],
      flow: 'flow',
    }
  },
  methods: {
    // 手动录入按钮点击
    manualEntry(fieldName, flow) {
      this.ModalForm.orgName = ''
      this.currentEntryField = fieldName
      this.flow = flow
      this.ModalVisible = true
    },
    // 手动录入表单提交
    ModalHandleOk() {
      let flow = this.$refs[this.flow]
      if (flow.getFieldValue === undefined) {
        flow = flow.$refs.form
      }
      const mainSendOrgArr = flow.getFieldValue(this.currentEntryField)
        ? flow.getFieldValue(this.currentEntryField)
        : []
      const OrgName = this.ModalForm.orgName
      // 手工输入多个主送部门用逗号分隔开
      if (OrgName.includes('，') || OrgName.includes(',')) {
        const str = OrgName.split(/[,+，]/g)
        this.OrgNameData = []
        str.map((item) => {
          const sjs = Math.floor(Math.random() * 100000)
          this.OrgNameData.push({
            type: 'GROUP',
            name: item,
            text: item,
            code: sjs+"_sd",
            id: sjs+"_sd",
            attrNames: [],
          })
        })
        if (this.ModalForm.orgName.trim()) {
          // 去空格判断
          const newData = [...mainSendOrgArr, ...this.OrgNameData]
          flow.setFieldValue(this.currentEntryField, newData)
          flow.validateField(this.currentEntryField)
      if(this.changecompetentDept !== undefined && typeof this.changecompetentDept === 'function') this.changecompetentDept(newData)
        }
      } else {
        if (this.ModalForm.orgName.trim()) {
          const sjs = Math.floor(Math.random() * 100000)
          // 去空格判断
          const newData = [
            ...mainSendOrgArr,
            {
              type: 'GROUP',
              name: this.ModalForm.orgName.trim(),
              text: this.ModalForm.orgName.trim(),
            code: sjs+"_sd",
            id: sjs+"_sd",
              attrNames: [],
            },
          ]
          flow.setFieldValue(this.currentEntryField, newData)
          flow.validateField(this.currentEntryField)
      if(this.changecompetentDept !== undefined && typeof this.changecompetentDept === 'function') this.changecompetentDept(newData)
        }
      }
      this.ModalVisible = false
    },
  },
}
</script>
