<template>
  <a-card :loading="cardLoading" :bordered="false" style="height: 100%;">
    <a-spin :spinning="loading">
      <a-tree
        showLine
        checkStrictly
        :expandedKeys.sync="expandedKeys"
        :selectedKeys="selectedKeys"
        :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
        :treeData="treeDataSource"
        @select="handleTreeSelect"
      />
    </a-spin>
  </a-card>
</template>

<script>
  import { httpAction,getAction,deleteAction } from '@/api/manage'
  const queryDepartTreeList = (params)=>getAction("/saasmanager/hwPlatformCompanyAccount/myAccount",params);

  export default {
    name: 'AddressListLeft',
    props: ['value'],
    data() {
      return {
        cardLoading: true,
        loading: false,
        treeDataSource: [],
        selectedKeys: [],
        expandedKeys: [],
        url: {

        },
      }
    },
    created() {
      this.queryTreeData()
    },
    methods: {

      queryTreeData(keyword) {
        this.commonRequestThen(queryDepartTreeList())
      },

      handleTreeSelect(selectedKeys, event) {
        if (selectedKeys.length > 0 && this.selectedKeys[0] !== selectedKeys[0]) {
          this.selectedKeys = [selectedKeys[0]]
          let subcontractorId = event.node.dataRef.subcontractorId
          let cpType = event.node.dataRef.cpType
          this.emitInput(subcontractorId, cpType)
        }
      },

      emitInput(subcontractorId, cpType) {
        var arr = [subcontractorId, cpType];
        this.$emit('input', arr)
      },

      commonRequestThen(promise) {
        this.loading = true
        promise.then(res => {
          if (res.success) {
            //处理树结构
            res.result.forEach((item, index) => {
              item.key = item.subcontractorId
              item.value = item.cpType
              item.title = item.subcontractorName
            })
            this.treeDataSource = res.result
            // update-begin- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
            // 默认选中第一条数据、默认展开所有第一级
            if (res.result.length > 0) {
              this.expandedKeys = []
              res.result.forEach((item, index) => {
                if (index === 0) {
                  this.selectedKeys = [item.subcontractorId]
                  let subcontractorId = item.subcontractorId
                  let cpType = item.cpType
                  this.emitInput(subcontractorId, cpType)
                  this.expandedKeys.push(item.subcontractorId)
                }
              })
            }
           // update-end- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
          } else {
            this.$message.warn(res.message)
            console.error('组织机构查询失败:', res)
          }
        }).finally(() => {
          this.loading = false
          this.cardLoading = false
        })
      },

    }
  }
</script>

<style scoped>
  /deep/ .ant-card-body {
    padding: 24px 12px;
  }
</style>