/**
 * 单位树
 */
<template>
  <div>
    <TreeViewNode
      v-for="clientOrganization in datas"
      :key="clientOrganization.id"
      :node="clientOrganization"
      :is-root="true"
      :is-show-check="true"
      @node-select="clientSelect"
    />
  </div>
</template>

<script>

import Common from '../../base/common/Common.js';
import RoleOrganizationResource from '../../api/base/RoleOrganizationResource.js';
import TreeViewNode from '../../base/widget/TreeViewNode.vue';
import { Notify, Uuid } from 'pc-component-v3';

export default {

  components: {
    TreeViewNode,
  },

  props: {
    autoGetAllClientId: {
      type: Boolean,
      default: false,
    }, 
  },

  emits: ['selectChanged', 'getClientOrganizations'],

  data: function () {
    return {
      datas: [],   //所有根单位
      oldData: [],
      selectedClientIds: [], // 已选择的单位集合
      autoGetAllClient: false,
    };
  },
  watch: {
    'selectedClientIds': function (newVal) {
      var _self = this;
      _self.$emit('getClientOrganizations', newVal);
    },
  },

  mounted: function () {
    var _self = this;
    _self.autoGetAllClient = this.autoGetAllClientId;
    this.getClients();
  },

  methods: {

    /**
         * 获取可访问的单位
         */
    getClients: function () {
      var _self = this;
      RoleOrganizationResource.getClientOrganizations(true).then(successData => {
        if(successData.errorCode != 0){
          Notify.error('部门查询失败', successData.errorCode, false);
          return;
        }
        _self.initClient(successData.datas);
        _self.oldData = successData.datas;
        _self.$nextTick(function(){
          _self.$emit('loadFinished');
        });
      }, errorData => {
        Common.processException(errorData);
      });
    },

    /**
         * 初始化部门数据
         */
    initClient: function (data) {
      var _self = this;
      function setOpen(node) {
        node.open = false;
        if (_self.autoGetAllClient == undefined) {
          node.selected = false;
        } else {
          node.selected = _self.autoGetAllClient;
        }
        node.isShowCheck = true;
        node.text = node.name;
        if (node.childrenDatas != undefined && node.childrenDatas.length > 0) {
          node.childrenDatas.forEach(function (item) {
            setOpen(item);
          });
        }
      }
      data.forEach(function (item) {
        setOpen(item);
      });
      _self.datas = data;
      if (_self.autoGetAllClient != undefined && _self.autoGetAllClient) {
        this.getSelectedIds();
      }
    },

    /**
        * 父组件调用，用来初始化数据
        */
    initClient1: function () {
      var _self = this;
      _self.initClient(_self.oldData);
    },

    /**
         * 部门选择
         */
    clientSelect: function (node) {
      var _self = this;
      node.selected = !node.selected;
      var current = node.selected;

      function changeSelect(item) {
        item.selected = current;
        if (item.childrenDatas != undefined && item.childrenDatas.length > 0) {
          item.childrenDatas.forEach(function (item1) {
            changeSelect(item1);
          });
        }
      }
      changeSelect(node);

      function changeParentSelect(item) {
        if (item.childrenDatas != undefined && item.childrenDatas.length > 0) {
          var selectedCount = 0;
          item.childrenDatas.forEach(function (item1) {
            changeParentSelect(item1);
            if (item1.selected) {
              selectedCount++;
            }
          });
          if (item.isClient) {
            if (selectedCount < item.childrenDatas.length) {
              item.selected = false;
            } else {
              item.selected = true;
            }
          }
        }
      }
      _self.datas.forEach(function (item) {
        changeParentSelect(item);
      });
      // 触发selectChanged事件
      _self.$emit('selectChanged');
    },


    /**
         * 获取选择的部门ID集合
         */
    getSelectedIds: function () {
      var _self = this;
      _self.selectedClientIds.splice(0, _self.selectedClientIds.length);
      function getSelected(item) {
        if (item.isClient && item.selected) {
          _self.selectedClientIds.push(item.id);
        }
        if (item.childrenDatas != undefined && item.childrenDatas.length > 0) {
          item.childrenDatas.forEach(function (item1) {
            getSelected(item1);
          });
        }
      }
      _self.datas.forEach(function (item) {
        getSelected(item);
      });
      return _self.selectedClientIds;
    },
  },
};
</script>

<style>
</style> 