<template>
  <a-card
    :class="[
      $style.treewrap,
      $style.ywlxtree,
      { [$style.collapse]: fold, [$style.single]: single },
    ]"
  >
    <a-form-model>
      <a-form-model-item :label="null" prop="orgValue">
        <a-select
          v-model="allTreeDataKeys"
          show-search
          :filter-option="filterOption"
          @change="onSelectName"
        >
          <a-select-option v-for="item in allTreeData" :key="item.id" :value="item.id">
            {{ item.name }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
    <a-spin :spinning="spinning" :class="$style.spin" />
    <a-empty v-if="empty" />
    <a-tree
      v-else
      ref="auditMaintainCatalogTree"
      v-model="checkedKeys"
      :block-node="false"
      :show-icon="true"
      :show-line="showLine"
      :check-strictly="true"
      :checkable="false"
      :tree-data="treeData"
      :replace-fields="replaceFields"
      :selected-keys="defaultSelectedKeys"
      :expanded-keys="expandedKeys"
      :default-expanded-keys="defaultTreeExpandedKeys"
      :default-selected-keys="defaultSelectedKeys"
      @select="treeSelect"
      @expand="onExpand"
    >
    </a-tree>
  </a-card>
</template>

<script>
import components from './_import-components/aduit-area-tree-import'
import { Message } from 'ant-design-vue'
import { getUserInfo } from '@/common/store-mixin'
import auditMaintainService from './audit-maintain-service'

export default {
  name: 'AduitAreaTree',
  metaInfo: {
    title: 'AduitAreaTree',
  },
  components,
  props: {
    // 默认选中节点
    selectedKeys: {
      type: Array,
      default: function() {
        return []
      },
    },
    // 查看模式 manager维护权限 read查看权限
    managerType: {
      type: String,
      default: 'manager',
    },
    // 是否显示复选框
    checkable: {
      type: Boolean,
      default: true,
    },
    // 是否可以拖拽
    draggable: {
      type: Boolean,
      default: false,
    },
    // 是否显示连线
    showLine: {
      type: Boolean,
      default: false,
    },
    // 根节点名称
    topNodeText: {
      type: String,
      default: '根节点',
    },
    // 地址树接口数据源
    treeparams: {
      type: Object,
      default: () => {
        return {}
      },
    },
    // 默认展开节点id
    defaultExpandedKeys: {
      type: Array,
      default: () => {
        return ['0']
      },
    },

    // 是否显示隐藏按钮
    showPrimary: {
      type: Boolean,
      default: true,
    },
    // 地址树是否单选
    single: {
      type: Boolean,
      default: false,
    },
    refreshKey: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      treeData: [],
      allTreeData: [],
      allTreeDataKeys: null,
      spinning: false,
      empty: false,
      fold: false,
      expandedKeys: [],
      defaultTreeExpandedKeys: [],
      defaultSelectedKeys: [],
      checkedKeys: [],
      replaceFields: {
        children: 'children',
        title: 'name',
        key: 'id',
      },
    }
  },
  created() {
    this.initTreeData()
  },

  methods: {
    // 获取树形图直接显示所有领域数据
    initTreeData() {
      auditMaintainService.getAreaListAll().then((res) => {
        if (res.data.length === 0) {
          this.empty = true
          return
        }
        this.allTreeData = res.data
        this.treeData = [
          {
            id: '0',
            name: '全部领域',
            props: {
              isroot: true,
            },
            children: res.data.map((item) => {
              return {
                ...item,
                props: {
                  isroot: false,
                },
              }
            }),
          },
        ]
      })
    },
    // 搜索
    filterOption(input, option) {
      return option.componentOptions.children[0].name.indexOf(input) >= 0
    },
    onSelectName(val) {
      const onfindData = this.allTreeData.find((item) => item.id === val)
      if (!onfindData) return
      this.expandedKeys = ['0']
      const selectedKeys = [onfindData.id]
      const info = {
        node: {
          dataRef: onfindData,
        },
      }
      this.treeSelect(selectedKeys, info)
    },
    // 选中节点
    treeSelect(selectedKeys, info) {
      // id为零传空数组，空对象
      this.defaultSelectedKeys = selectedKeys
      if (selectedKeys.length === 0 || selectedKeys[0] === '0') {
        this.allTreeDataKeys = null
        this.$emit('treeSelect', [], {})
        return
      }
      // 选中的节点
      this.$emit('treeSelect', selectedKeys, info.node.dataRef)
    },
    // 点击展开
    onExpand(expandedKeys) {
      // 用户点击展开时，取消自动展开效果
      this.expandedKeys = expandedKeys
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.spin {
  width: 100%;
  line-height: 30;
}
.ywlxtree {
  :global(.ant-tree-title) {
    display: inline-block;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  :global(.ant-input-search) {
    margin: 8px 0;
    overflow: hidden;
  }
  .active {
    color: $primary-color;
  }
  :global(.ant-select) {
    width: 100%;
    overflow: hidden;
  }
}
.treewrap {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 20%;
  min-height: 100%;
  margin-right: $padding-lg;
  transition: width 0.2s;
  .fold {
    position: absolute;
    top: calc(50% - 30px);
    right: -15px;
    z-index: 2;
    width: 15px;
    height: 75px;
    padding: 0;
    border-radius: 0 10px 10px 0;
  }
  :global(.ant-tree) {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  :global(.ant-card-body) {
    background: $white;
  }
}
.collapse {
  width: 0;
  :global(.ant-card-body) {
    background: transparent;
    :global(.ant-empty) {
      display: none;
    }
  }
}
// 单选样式
.single {
  :global .ant-tree-checkbox {
    .ant-tree-checkbox-inner {
      border-radius: 100px;
      &::after {
        position: absolute;
        top: 3px;
        left: 3px;
        display: table;
        width: 8px;
        height: 8px;
        content: ' ';
        background-color: $primary-color;
        border: 0;
        border-radius: 8px;
        opacity: 0;
        transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
        transform: scale(0);
      }
    }
    &.ant-tree-checkbox-checked {
      &::after {
        position: absolute;
        top: 16.67%;
        left: 0;
        width: 100%;
        height: 66.67%;
        content: '';
        border: 1px solid #1890ff;
        border-radius: 50%;
        animation: antRadioEffect 0.36s ease-in-out;
        animation-fill-mode: both;
      }
      .ant-tree-checkbox-inner {
        background-color: $white;
        &::after {
          opacity: 1;
          transition: all 0.3s cubic-bezier(0.78, 0.14, 0.15, 0.86);
          transform: scale(1);
        }
      }
    }
  }
}
</style>
