<template>
  <!-- 控制点分类树 -->
  <a-modal
    :visible="catvisible"
    title="请选择"
    width="700px"
    :destroy-on-close="true"
    @ok="handlecatOk"
    @cancel="handlecatCancel"
  >
    <ic-measure-catalog-tree
      ref="mtxprocesstree"
      show-line
      top-node-text="内控矩阵"
      :is-select-dep="true"
      manager-type="view"
      :classstyle="true"
      :history="true"
      :hisv="this.versionId"
      :hisorg="this.orgId"
      :hisorgn="this.orgName"
      :selecttype="this.selecttype"
      :selectedKeys="this.selectedKeys"
      :single="single"
      @treeSelect="treeSelect"
      @treeCheck="treeCheck"
    ></ic-measure-catalog-tree>
  </a-modal>
</template>

<script>
import { Modal, Message } from 'ant-design-vue'
import icMeasureCatalogTree from './ic-measure-catalog-tree.vue'
import components from './_import-components/ic-measure-select-import'

export default {
  name: 'IcMeasureSelect',
  metaInfo: {
    title: 'IcMeasureSelect',
  },
  components: { ...components, icMeasureCatalogTree },
  props: {
    // 是否单选
    single: {
      type: Boolean,
      default: false,
    },
    // 弹出窗标题
    title: {
      type: String,
      default: '请选择',
    },
    // catvisible: {
    //   type: Boolean,
    //   default: false,
    // },
    versionId: {
      type: String,
      default: '271',
    },
    orgId: {
      type: String,
      default: '364',
    },
    orgName: {
      type: String,
      default: '江苏分公司',
    },
    selecttype: {
      type: String,
      default: 'control',
    },
    selectedKeys: {
      type: Array,
      default: function() {
        return []
      },
    },
  },
  data() {
    return {
      cat: null,
      catvisible: false,
    }
  },
  methods: {
    treeCheck(checked, info) {
      const checklist = []
      info.forEach((i) => {
        if (this.selectedKeys.indexOf(i.data.props.id) === -1) {
          let id = i.data.props.id.toString()
          if (id.indexOf('1000') > -1) {
            id = id.substring(4, id.length)
          }
          checklist.push(id)
        }
      })
      this.cat = checklist
    },
    treeSelect(select, info) {
      const id = info.selectedNodes[0].data.props.id
      this.cat = null
      if (info.selectedNodes[0].data.props.props) {
        if (info.selectedNodes[0].data.props.props.isEnd === '1') {
          if (id !== -1) {
            this.cat = id
          } else {
            this.cat = -1
          }
        } else {
          Modal.info({
            content: '请选择末级分类节点',
          })
        }
      }
    },
    handlecatOk() {
      if (this.cat !== null) {
        // this.$refs.docform.setFieldValue('mtxPrcPath', this.catpath)
        // this.$refs.docform.setFieldValue('mtxPrcId', this.catId)
        this.showtree()
        this.$emit('fnqd', this.cat)
        this.cat = null
      } else {
        Modal.warning({
          title: '提示',
          content: '请选择节点!',
        })
        return false
      }
    },
    handlecatCancel() {
      this.showtree()
    },

    showtree() {
      this.catvisible = !this.catvisible
      if (this.catvisible) {
        setTimeout(() => {
          this.$refs.mtxprocesstree.initDeptList('')
        }, 1000)
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
