import notebook from '@/common/components/sd-icon-library/icons/outline/notebook'

// 判断是否已经选择
function isChecked(selectedKeys, eventKey) {
  return selectedKeys.indexOf(eventKey) === -1
}

const AuditPermissionTreeMixins = {
  created() {
    // 判断单选多选
    if (!this.single) {
      this.checkedKeys = {
        checked: [],
        halfChecked: [],
      }
    }
    // 如果有默认展开节点，则赋值
    if (this.defaultExpandedKeys) {
      this.defaultTreeExpandedKeys = [...this.defaultExpandedKeys]
    }
    // 如果有传根节点名称，则赋值
    if (this.topNodeText) {
      this.defaultTopNodeText = this.topNodeText
    }
    // 如果有传默认选中节点，则赋值
    if (this.selectedKeys.length > 0) {
      this.defaultSelectedKeys = [...this.selectedKeys]
    }
    // 如果有传部门下拉默认值，则赋值
    if (this.defaultDepValue.length > 0) {
      this.depvalue = [...this.defaultDepValue]
      console.log('---有部分---')
      this.initTreeData(this.defaultDepValue[0])
    } else {
      console.log('--初次')
      this.initTreeData()
    }
  },
  methods: {
    /**
     * 显示分类负责人
     */
    showUser(item) {
      this.$emit('showUser', item)
    },
    // 判断当前树需要展示的节点信息
    bindTree(nodeinfo) {
      const checklist = this.checkedKeys.checked
      if (nodeinfo.children) {
        this.bindTree(nodeinfo.children)
        const hiddenItem = nodeinfo.children.find((item, index) => {
          return item.hidden !== true
        })
        // 判断当前进节点是否隐藏
        if (!hiddenItem && checklist.indexOf(nodeinfo.id) === -1) {
          nodeinfo.hidden = true
        }
        // 如果显示但是没有在选择列表，则不可选中
        if (!nodeinfo.hidden && checklist.indexOf(nodeinfo.id) === -1) {
          nodeinfo.disabled = true
        }
      } else {
        nodeinfo.forEach((node, nodeindex) => {
          if (node.children) {
            // 判断子节点是否有选中的节点
            this.bindTree(node.children)
            const hiddenItem = node.children.find((item, index) => {
              return item.hidden !== true
            })
            // 判断当前进节点是否隐藏
            if (!hiddenItem && checklist.indexOf(node.id) === -1) {
              node.hidden = true
            }
            // 如果显示但是没有在选择列表，则不可选中
            if (!node.hidden && checklist.indexOf(node.id) === -1) {
              node.disabled = true
            }
          } else {
            if (checklist.indexOf(node.id) === -1) {
              node.hidden = true
            }
            // 如果显示但是没有在选择列表，则不可选中
            if (!node.hidden && checklist.indexOf(node.id) === -1) {
              node.disabled = true
            }
          }
        })
      }
    },
    /***
     * 选中复选框时的事件
     */
    treeCheck(allKeys, echecked) {
      // echecked.checkedNodes 选中的所有节点信息为数组   .data.props 获取详细信息
      // echecked.node.dataRef  当前选中的节点信息
      if (this.single) {
        this.checkedKeys.checked = []
        this.checkedKeys.checked = [echecked.node.dataRef.id]
      }
      this.$emit('checkedKeys', echecked.node.dataRef, echecked.checkedNodes)
    },
    getTree(node) {
      const removeIndex = []
      node.children.forEach((item, index) => {
        if (item.hidden === true) {
          removeIndex.push(index)
        } else {
          if (item.children) {
            this.getTree(item)
          }
        }
      })
      // 删除元素
      removeIndex.forEach((oneindex, index) => {
        node.children.splice(oneindex - index, 1)
      })
    },

    // 点击树
    treeSelect(selectedKeys, info) {
      this.defaultSelectedKeys = selectedKeys
      if (this.single) {
        this.checkedKeys = []
        this.checkedKeys = [info.node.dataRef.id]
      } else {
        if (isChecked(this.checkedKeys.checked, info.node.dataRef.id)) {
          this.checkedKeys.checked.push(info.node.dataRef.id)
        } else {
          this.checkedKeys.checked.splice(this.checkedKeys.checked.indexOf(info.node.dataRef.id), 1)
        }
        this.checkedKeys.checked = [...new Set(this.checkedKeys.checked)]
      }
      this.$emit('treeSelect', [...selectedKeys, ...this.checkedKeys.checked], info)
    },
    // 筛选树
    onSearchChange(e) {
      const value = e.target.value

      const expandedKeys = this.dataList
        .map((item) => {
          if (item.title.indexOf(value) > -1) {
            return this.getParentKey(item.key, this.treeData)
          }
          return null
        })
        .filter((item, i, self) => item && self.indexOf(item) === i)

      expandedKeys.unshift('0') // 顶结点永远展开

      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true,
      })
    },

    getParentKey(key, tree) {
      let parentKey
      for (let i = 0; i < tree.length; i++) {
        const node = tree[i]
        if (node.children) {
          if (node.children.some((item) => item.id === key)) {
            parentKey = node.id
          } else if (this.getParentKey(key, node.children)) {
            parentKey = this.getParentKey(key, node.children)
          }
        }
      }
      return parentKey
    },

    transformData(data) {
      return data.map((d) => {
        const { children, ...rest } = d
        return {
          ...rest,
          children: children && this.transformData(children),
          scopedSlots: { title: 'title' },
        }
      })
    },

    // 处理搜索用的dataList
    generateList(data) {
      for (let i = 0; i < data.length; i++) {
        const node = data[i]
        const key = node.id
        const title = node.text
        const props = node.props
        // 用来判断此几点是否已经配置了用户，需要后台返回标识位进行判断
        // if (props.showuser) {
        //   data[i].scopedSlots.icon = 'hasuser'
        // }
        this.dataList.push({ key, id: key, title: title, props })
        if (node.children) {
          this.generateList(node.children)
        }
      }
    },

    onExpand(expandedKeys) {
      // 用户点击展开时，取消自动展开效果
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    // 小箭头点击事件
    foldClick() {
      // const newtree = {}
      // Object.assign(newtree, this.treeData)
      // this.bindTree(newtree[0])
      // this.getTree(newtree[0])
      this.fold = !this.fold
      if (this.fold) {
        this.icontype = 'right'
      } else {
        this.icontype = 'left'
      }
    },
    // 组织下拉框change时触发
    changedep(value, option, extra) {
      this.key++
      if (!value) {
        this.defaultTreeExpandedKeys = ['0']
        this.defaultSelectedKeys = ['0']
        this.defaultTopNodeId = '0'
        this.defaultTopNodeText = this.topNodeText
      } else {
        this.defaultTreeExpandedKeys = [extra.triggerNode.dataRef.value]
        this.defaultSelectedKeys = [extra.triggerNode.dataRef.value]
        this.defaultTopNodeId = extra.triggerNode.dataRef.value
        this.defaultTopNodeText = extra.triggerNode.dataRef.title
      }
      this.initTreeData(value)
      this.$emit('depChanged', value, option)
    },
    // 刷新树方法
    refreshTree() {
      this.key++
    },
    fnchangewidth() {
      let obj = null
      let item = null
      let e = null
      let width = 0
      if (this.checkable) {
        obj = document.getElementsByClassName('ant-tree')[1]
        if (obj) {
          width = obj.clientWidth
          if (obj.children.length > 0) {
            item = obj.children[0]
            if (item.children.length > 0) {
              e = item.children[3]
            }
          }
        }
      } else {
        obj = document.getElementsByClassName('ant-tree ant-tree-show-line')[0]
        if (obj) {
          width = obj.clientWidth
          if (obj.children.length > 0) {
            item = obj.children[0]
            if (item.children.length > 0) {
              e = item.children[2]
            }
          }
        }
      }
      // const
      if (e) {
        if (item.scrollWidth > width) {
          width = item.scrollWidth
        }
        if (e.scrollWidth > width) {
          width = e.scrollWidth
        }
        if (e.children.length > 0) {
          for (var k = 0; k < e.children.length; k++) {
            const a = e.children[k]
            if (a.scrollWidth > width) {
              width = a.scrollWidth
            }
            if (a.children.length > 2) {
              const b = a.children[2]
              if (b.scrollWidth > width) {
                width = b.scrollWidth
              }
              for (var l = 0; l < b.children.length; l++) {
                const c = b.children[l]
                if (c.scrollWidth > width) {
                  width = c.scrollWidth
                }
              }
            }
          }
        }
      }
      obj.style.width = width + 'px'
    },
  },
}

export default AuditPermissionTreeMixins
