<template>
  <div>
    <selector
      :show-popup="showPopup"
      :value="checkedValue"
      :data-list="listData"
      :cache-data="cacheData"
      :path-data="pathData"
      :multiple="multiple"

      :left-text="leftText"
      :search-placeholder="searchPlaceholder"
      :id-key="idKey"
      :parent-id-key="parentIdKey"
      :value-key="valueKey"
      :label-key="labelKey"
      :children-key="childrenKey"
      :party-type-id="partyTypeId"
      :root-name="$t('components.selector.position')"

      :loading="loading"
      :finished="finished"

      @load-data="loadData"
      @change-path="changePath"
      @toggle-child="toggleChild"
      @loading="changeLoading"
      @init-page-data="initPageData"
      @confirm="onConfirm"
      @cancel="onCancel"
      @click-overlay="clickOverlay"
    />
  </div>
</template>
<script>
import { getScriptValue } from '@/api/platform/form/formDef'// 脚本
import { findTreeData as queryByParentId, get as getPositionById } from '@/api/platform/org/position'
import { findTreeData as getPositionTreeData } from '@/api/platform/org/position'
import { getScriptValue as getTreeDataByScriptValue } from '@/api/platform/form/common'
import ActionUtils from '@/utils/action'
import Selector from './selector'
import i18n from '@/utils/i18n' // Internationalization 国际化
import TreeUtils from '@/utils/tree'

export default {
  components: {
    Selector
  },
  props: {
    value: Array,
    showPopup: Boolean,
    multiple: Boolean,
    leftText: String,
    searchPlaceholder: {
      type: String,
      default: i18n.t('common.input')
    },
    idKey: {
      type: String,
      default: 'id'
    },
    parentIdKey: {
      type: String,
      default: 'parentId'
    },
    valueKey: {
      type: String,
      default: 'id'
    },
    labelKey: {
      type: String,
      default: 'name'
    },
    childrenKey: {
      type: String,
      default: 'children'
    },
    otherFilter: String,
    otherFilterIncludeSub: [String, Boolean],
    otherFilterCondition: {
      type: Object,
      default: () => {
        return {}
      }
    },
    partyTypeId: { // 类型节点ID
      type: [String, Number],
      default: ''
    },
    currentOrgId: { // 用户组ID
      type: [String, Number],
      default: ''
    },
    script: String,
    isUseScope: { // 是否启用范围
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      checkedValue: [],
      pathData: [],
      listData: [],
      cacheData: {}, // 缓存数据
      cacheCheckData: [],
      currentListData: [],
      parentId: '0',

      pagination: {},
      loading: false,
      finished: true,
      refreshing: false,
      resultType: 'init',
      errorType: null,
      resultMessage: null
    }
  },
  watch: {
    value() {
      this.initData(this.value)
    },
    showPopup(val) {
      if (val) {
        this.parentId = '0'
        this.pathData = [{
          id: '',
          name: '部门'
        }]
        this.initPageData()
        this.cacheData = []
        if (this.otherFilter === 'script') {
          this.getTreeDataByScriptValue()
        } else {
          this.loadData({})
        }
      }
    }
  },
  methods: {
    initData(data) {
      this.checkedValue = []
      if (this.$utils.isEmpty(data)) {
        return
      }
      data.forEach(v => {
        if (this.cacheData[v]) {
          this.checkedValue.push(v)
        } else {
          this.getDataInfo(v)
        }
      })
    },
    /**
     * 获取数据信息
     */
    getDataInfo(id) {
      getPositionById({ positionId: id }).then(response => {
        const data = response.data
        if (this.$utils.isEmpty(data)) {
          return
        }
        this.cacheData[data[this.idKey]] = data
        this.checkedValue.push(data[this.idKey])
        this.$emit('init-data', this.getSelectedData(this.checkedValue))
      }).catch((e) => {
        console.error(e)
      })
    },
    toTree(data) {
      return TreeUtils.transformToTreeFormat(data, {
        idKey: 'id',
        pIdKey: 'parentId',
        childrenKey: 'children'
      })
    },
    // 根据渲染的数据范围类型，筛选目标节点
    filterTreeChildren(treeData, treeScope, nodeId) {
      const data = []
      // 返回设置的范围节点
      if (treeScope === 'root') {
        const currentOrgIds = this.currentOrgId.split(',')
        treeData.forEach(f => {
          if (currentOrgIds.includes(f.id)) {
            f.leaf = true
            data.push(f)
          }
        })
      }
      return data
    },
    // 脚本函数
    getScriptData(scriptValue) {
      getScriptValue({
        script: scriptValue,
        vars: ''
      }).then(response => {
        const data = JSON.parse(response.data)
        this.treeData = TreeUtils.transformToTreeFormat(data.data)
      })
    },
    loadData(data, isInit) {
      this.loading = true
      let params = {}
      const type = this.partyTypeId !== '' ? this.partyTypeId : '1'
      if (this.isUseScope) {
        params.type = type
        params.includeSub = type === '1'
        if (type === '2') {
          params.partyId = this.$utils.isEmpty(data) ? '0' : data.id === '' ? '0' : data.id
        }
        if (type === '3') {
          params.partyId = this.currentOrgId
        }
        if (type === '1') {
          params.posId = this.$utils.isEmpty(data) ? '' : data.id
        }

        //  脚本类型初始化
        if (type === 'script') {
          this.getScriptData(this.script)
          this.loading = false
          return
        }
      } else {
        params.type = '1'
        params.posId = data.id === 0 ? null : data.id
      }
      if (this.$utils.isEmpty(data) || data.id === '0' || data.id === '') {
        getPositionTreeData(params).then(res => {
          console.log(res, '1111')
          this.loading = false
          this.refreshing = false
          this.finished = true
          const arr = JSON.parse(JSON.stringify(res.data))
          let treeData
          if (this.$utils.isEmpty(data) || data.id === '') {
            this.listData = this.toTree(arr)
            // 合并之前选中得数据
            this.currentListData = this.listData.concat(this.cacheCheckData)
          } else {
            treeData = type === '1' ? arr : this.filterTreeChildren(arr, 'root')
            this.listData = this.toTree(treeData)
            this.currentListData = this.listData.concat(this.cacheCheckData)
          }
        }).catch(res => {
          this.loading = false
          this.listData = []
        })
      } else if (data.id !== '0') {
        if (type === '2') {
          params = {}
          params.type = '1'
          params.posId = data.id
        }
        queryByParentId(params).then(res => {
          this.loading = false
          this.refreshing = false
          this.finished = true
          const arr = JSON.parse(JSON.stringify(res.data))
          this.listData = this.toTree(arr)
          this.currentListData = this.listData.concat(this.cacheCheckData)
        }).catch(res => {
          this.loading = false
          this.listData = []
        })
      }
    },
    getSelectedData(data) {
      const result = []
      data.forEach((d) => {
        if (this.cacheData[d]) {
          result.push(this.cacheData[d])
        }
      })
      return result
    },
    onCancel() {
      this.$emit('cancel')
    },
    onConfirm(data, type) {
      const checkData = Array.from(new Set(data))
      const listData = this.currentListData
      const arr = []
      if (this.multiple) {
        for (let i = 0; i < listData.length; i++) {
          for (let l = 0; l < checkData.length; l++) {
            if (listData[i].id === checkData[l]) {
              arr.push(listData[i])
              this.cacheCheckData = arr // 缓存
              break
            }
          }
        }
      } else {
        for (let j = 0; j < listData.length; j++) {
          if (listData[j].id === checkData[0]) {
            arr.push(listData[j])
            this.cacheCheckData = arr // 缓存
            break
          }
        }
      }
      if (type !== 'confirm') return
      this.$emit('confirm', arr)
      this.$emit('callbackData', arr)
      this.cacheCheckData = []
      this.currentListData = []
    },
    clickOverlay() {
      this.$emit('click-overlay')
    },
    toggleChild(data) {
      this.parentId = data.id
      this.pathData.push({ id: data.id, name: data.name })
      this.initPageData()
      this.loadData(data)
    },
    changePath(index, data) {
      this.parentId = this.pathData[index][this.idKey]
      this.pathData.splice(index + 1, this.pathData.length - 1)
      this.initPageData()
      this.loadData(data)
    },
    initPageData() {
      this.pagination = {
        page: 1,
        limit: 999999999
      }
      this.listData = []
      this.finished = false
      this.resultType = 'init'
      // this.cacheData = []
    },
    changeLoading(data) {
      this.loading = data
    },
    loadDataByScript() {
      getTreeDataByScriptValue({ 'script': this.otherFilterCondition.scriptContent }).then(response => {
        ActionUtils.handleListData(this, response.data)
        // 缓存
        if (this.$utils.isNotEmpty(this.listData)) {
          this.listData.forEach((d) => {
            // d[this.labelKey] = d[this.labelKey]
            this.cacheData[d[this.valueKey]] = d
          })
        }
      }).catch((e) => {
        ActionUtils.handleErrorData(this, e)
      })
    }
  }
}
</script>
