<template>
  <div>
    <template v-if="fieldTree.length > 0">
      <template>
        <div style="margin-bottom: 10px;">
          <span style="font-size: 14px;">开启关联模式</span>
          <el-switch style="display: block" v-model="ifHaveChild" active-color="#13ce66"
            active-text="描述：主分类包含子分类" inactive-text="" @change="switchChange">
          </el-switch>
        </div>
      </template>
      <div>
        <el-collapse v-model="activeNames" @change="handleChange($event, 2)" style="margin: 0px 5px 0px 5px;">
          <el-collapse-item v-for="item in fieldTree" :title="item.name" :name="item.value + 'a' + item.filedKind"
            :key="item.value">
            <div v-if="ifShow" v-loading="item.loading">
              <el-tree @check-change="onChangeTree($event, item)" :ref="item.value" :data="item.options" node-key="id"
                check-strictly default-expand-all :props="{ children: 'child', label: 'name' }">
                <span class="custom-tree-node" slot-scope="{ node, data }">
                  <el-checkbox-group v-model="item.check">
                    <div class="source-checkbox">
                      <el-checkbox :label="data.id" @change="(val)=>checkData(data.id,item,val,data.name)">
                        <!-- {{ data.name }}({{ getCount(item, data.id) }}) -->
                        <myTooltip :content="data.name + '('+ getCount(item, data.id) +')'" placement="top" effect="light">
                          <span class="source-checkbox-label text noWrap">{{ data.name }}({{ getCount(item, data.id) }})</span>
                        </myTooltip>
                      </el-checkbox>
                    </div>
                    
                  </el-checkbox-group>
                </span>
              </el-tree>
            </div>

          </el-collapse-item>
        </el-collapse>
      </div>

    </template>
    <template v-else>
      <div>暂无数据</div>
    </template>
  </div>
</template>

<script>
import mixins from './mixins'
export default {
  mixins: [mixins],
  props: {
    fieldTree: {
      type: Array,
      default: () => {
        return []
      }
    },
  },
  data() {
    return {
      ifShow: true,
      // 是否关联子分类false不关联，true关联
      ifHaveChild: true,
    }
  },
  watch: {
    // fieldTree: {
    //   handler() {
    //     this.ifShow = false
    //     this.$nextTick(() => {
    //       this.ifShow = true
    //     })
    //   },
    //   deep: true
    // }
  },
  mounted() {

  },
  methods: {
    switchChange(val) {
      this.$emit('switchChange', val)
    },

    // 选中的值
    onChangeTree(data, field) {//当前节点的数据，当前栏位的数据 
      // console.log(data, field);
      // var obj = {
      //   fieldId: field.value,
      //   fieldType: field.fieldType,
      //   value: [],
      //   isStatistic:true,
      // }
      // let arr = []
      // arr.push(obj)
      // this.$emit('customTab',arr)
      // console.log(obj);
    },
  },
}
</script>

<style lang="scss" scoped>
  .text {
    /* 超出10个字隐藏，之所以设置11em是因为省略号占一个位置 */
    max-width: 11em;
    overflow: hidden;
    /* 显示省略符号来代表被修剪的文本。 */
    text-overflow: ellipsis;
    /* 文本不换行 */
    white-space: nowrap;
  }
</style>