// 树形结构
<template>
  <div class="treeSelect">
    <ul class="treeUl" :id="treeUlId?treeUlId:'treeUl'"></ul>
    <Modal v-model="delModel" width="360">
      <p slot="header" style="color:#f60;text-align:center">
        <Icon type="information-circled"></Icon>
        <span>删除确认</span>
      </p>
      <div style="text-align:center">
        <p>是否继续删除？</p>
      </div>
      <div slot="footer">
        <Button type="error" size="large" long :loading="modal_loading" @click="delTreeData()">删除</Button>
      </div>
    </Modal>
  </div>

</template>
<script>
export default {
  /**
   * newType: 3.表示基础数据信息
   * */
  props: ['passData', 'newType', 'flag_add', 'flag_edit', 'child_add_data', 'child_edit_data', 'rolePowerList', 'savePowerFlag', 'saveRoleId', 'newType2', 'allPositionData', 'treeUlId'],
  data () {
    return {
      sHtml: '',
      delModel: false,
      currentDelData: null,
      modal_loading: false,
      havePowerPoint: [],
      departmentPosition: [],
      $delLi: null,
      $addLi: null,
      $editLi: null
    }
  },

  mounted () {
    let _this = this;
    let treeUlId = _this.treeUlId ? _this.treeUlId : 'treeUl';
    $('#' + treeUlId).on('click', ('.icon-font,.tree-icon-add,.tree-icon-delete,.tree-icon-edit'), function () {
      if (_this.newType !== '1' && _this.newType2 !== '2') {
        if (_this.newType2 !== '1') {
          if (_this.newType !== 3) {
            let $thisLi = $('.st-config-add-red').parent();
            $thisLi.css('color', 'black');
            $thisLi.children().eq(2).removeClass('st-config-add-red');
            $thisLi.children().eq(2).addClass('st-config-add-grey');
            $thisLi.children().eq(3).removeClass('st-config-delete-red');
            $thisLi.children().eq(3).addClass('st-config-delete-grey');
            $(this).parent().css('color', '#4086ff');
            $(this).parent().children().eq(2).removeClass('st-config-add-grey');
            $(this).parent().children().eq(2).addClass('st-config-add-red');
            $(this).parent().children().eq(3).removeClass('st-config-delete-grey');
            $(this).parent().children().eq(3).addClass('st-config-delete-red');
          } else {
            let $thisLi = $('.st-config-add-red').parent();
            $thisLi.css('color', 'black');
            $thisLi.children().eq(4).removeClass('st-config-delete-red');
            $thisLi.children().eq(4).addClass('st-config-delete-grey');
            $thisLi.children().eq(3).removeClass('st-config-add-red');
            $thisLi.children().eq(3).addClass('st-config-add-grey');
            $thisLi.children().eq(2).removeClass('st-config-edit-red');
            $thisLi.children().eq(2).addClass('st-config-edit-grey');
            $(this).parent().css('color', '#4086ff');
            $(this).parent().children().eq(4).removeClass('st-config-delete-grey');
            $(this).parent().children().eq(4).addClass('st-config-delete-red');
            $(this).parent().children().eq(3).removeClass('st-config-add-grey');
            $(this).parent().children().eq(3).addClass('st-config-add-red');
            $(this).parent().children().eq(2).removeClass('st-config-edit-grey');
            $(this).parent().children().eq(2).addClass('st-config-edit-red');
          }
        } else {
          let $thisLi = $('.none');
          $thisLi.css('color', 'black');
          if ($thisLi.children().eq(2).attr('class') !== undefined && $thisLi.children().eq(2).attr('class').indexOf('st-config-delete-red') > 0) {
            $thisLi.children().eq(2).removeClass('st-config-delete-red');
            $thisLi.children().eq(2).addClass('st-config-delete-grey');
          }
          if ($thisLi.children().eq(2).attr('class') !== undefined && $thisLi.children().eq(2).attr('class').indexOf('st-config-add-red') > 0) {
            $thisLi.children().eq(2).removeClass('st-config-add-red');
            $thisLi.children().eq(2).addClass('st-config-add-grey');
          }
          if ($(this).parent().parent().data('tree_data').position_no !== undefined) {
            $(this).parent().children().eq(2).removeClass('st-config-delete-grey');
            $(this).parent().children().eq(2).addClass('st-config-delete-red');
          } else {
            $(this).parent().children().eq(2).removeClass('st-config-add-grey');
            $(this).parent().children().eq(2).addClass('st-config-add-red');
          }
          $thisLi.removeClass('none');
          $(this).parent().addClass('none');
          $(this).parent().css('color', '#4086ff');
        }
      }
    });

    $('#' + treeUlId).on('click', '.icon-front', function () {
      if (_this.newType2 === '1') {
        let $ul = $(this).parent().parent().children('ul');
        $ul.stop();
        if ($ul.is(':hidden')) {
          if ($(this).attr('class').indexOf('st-config-spread') > 0) {
            $(this).addClass('st-config-retract');
            $(this).removeClass('st-config-spread');
          }
          $ul.slideDown(300);
        } else {
          if ($(this).attr('class').indexOf('st-config-retract') > 0) {
            $(this).addClass('st-config-spread');
            $(this).removeClass('st-config-retract');
          }
          $ul.slideUp(300);
        }
      } else if (_this.newType2 === '2') {
        if ($(this).attr('class').indexOf('st-icons-shiAdd') > 0 || $(this).attr('class').indexOf('st-icons-shiClose') > 0) {
          let $ul = $(this).parent().parent().children('ul');
          $ul.stop();
          $('.oa-im-background').removeClass('oa-im-background');
          $('#blockdiv').css({
            'display': 'none'
          });
          if ($ul.is(':hidden')) {
            $(this).addClass('st-icons-shiClose');
            $(this).removeClass('st-icons-shiAdd');
            $ul.slideDown(300);
          } else {
            $(this).addClass('st-icons-shiAdd');
            $(this).removeClass('st-icons-shiClose');
            $ul.slideUp(300);
          }
        }
      } else {
        if ($(this).parent().find('.st-config-function').length !== 1) {
          let $ul = $(this).parent().parent().children('ul');
          $ul.stop();
          if ($ul.is(':hidden')) {
            $(this).addClass('st-config-retract');
            $(this).removeClass('st-config-spread');
            $ul.slideDown(300);
          } else {
            $(this).addClass('st-config-spread');
            $(this).removeClass('st-config-retract');
            $ul.slideUp(300);
          }
        } else {
          let $ul = $(this).parent().parent().children('ul');
          $ul.stop();
          if ($ul.is(':hidden')) {
            $ul.slideDown(300);
          } else {
            $ul.slideUp(300);
          }
        }
      }
    });

    $('#' + treeUlId).on('click', '.icon-font', function (event) {
      let data = $(this).parent().parent().data('tree_data');
      _this.$editLi = $(this).parent();
      if (_this.newType === '1' && (!data.checked || data.checked === undefined)) {
        // 给他加个√
        if (data.haveChildren === 1) {
          $('#' + data.id).find('.treeUlDiv').each(function () {
            if (!$(this).parent().data('tree_data').checked) {
              if ($(this).parent().data('tree_data').templeChecked) {
                $(this).children().eq(0).remove();
              }
              $(this).prepend("<span class='st-icon-config st-config-rightB'></span>");
            }
            let obj = $(this).parent().data('tree_data');
            let obj2 = {};
            if (obj.haveChildren === 1) {
              obj2 = Object.assign({}, obj, {'checked': true, 'templeChecked': true});
            } else {
              obj2 = Object.assign({}, obj, {'checked': true});
            }
            $(this).parent().data('tree_data', obj2);
          });
          let obj = Object.assign({}, data, {'checked': true, 'templeChecked': true});
          $(this).parent().parent().data('tree_data', obj);
          fatherHaveCheck($(this).parent().parent().data('tree_data'));
        } else {
          $(this).parent().prepend("<span class='st-icon-config st-config-rightB'></span>");
          let obj = Object.assign({}, data, {'checked': true});
          $(this).parent().parent().data('tree_data', obj);
          fatherHaveCheck($(this).parent().parent().data('tree_data'));
        }
      } else if (_this.newType === '1' && data.checked) {
        // 给他取消√
        if (data.haveChildren === 1) {
          $('#' + data.id).find('.treeUlDiv').each(function () {
            if ($(this).parent().data('tree_data').checked) {
              $(this).children().eq(0).remove();
            }
            let obj = $(this).parent().data('tree_data');
            let obj2 = {};
            if (obj.haveChildren === 1) {
              obj2 = Object.assign({}, obj, {'checked': false, 'templeChecked': false});
            } else {
              obj2 = Object.assign({}, obj, {'checked': false});
            }
            $(this).parent().data('tree_data', obj2);
          });
          let obj = Object.assign({}, data, {'checked': false, 'templeChecked': false});
          $(this).parent().parent().data('tree_data', obj);
          fatherNoCheck($(this).parent().parent().data('tree_data'));
        } else {
          $(this).parent().children().eq(0).remove();
          let obj = Object.assign({}, data, {'checked': false});
          $(this).parent().parent().data('tree_data', obj);
          fatherNoCheck($(this).parent().parent().data('tree_data'));
        }
      }
      _this.$emit('passChildData', data);
      event.stopPropagation();
    });
    $('#treeUl2').click(function () {
      $('.oa-im-background').removeClass('oa-im-background');
      $('#blockdiv').css({
        'display': 'none'
      });
    });
    // 检测添加
    $('#' + treeUlId).on('click', '.tree-icon-add', function () {
      let data = $(this).parent().parent().data('tree_data');
      _this.$addLi = $(this).parent().parent();
      _this.addTreeData(data);
      if (_this.newType === '2' || _this.newType2 === '1') {
        _this.$emit('addChild', data);
      }
    });

    // 检测删除
    $('#' + treeUlId).on('click', '.tree-icon-delete', function () {
      _this.currentDelData = $(this).parent().parent().data('tree_data');
      _this.$delLi = $(this).parent().parent();
      _this.delModel = true;
    });

    // 检测编辑
    $('#' + treeUlId).on('click', '.tree-icon-edit', function () {
      let data = $(this).parent().parent().data('tree_data');
      _this.$editLi = $(this).parent();
      _this.$emit('editTreeNode', data);
    });
  },

  watch: {
    passData () {
      if (this.newType2 === '2') {
        $('#treeUl2').html('');
      }
      this.init(this.passData);
    },

    // 实时增加树节点
    flag_add () {
      let n = this.child_add_data;
      let $li = $('<li>', {});
      let span = '';
      if (this.newType === 3) {
        span = "<span class='st-icon-config st-config-edit-grey tree-icon-edit'></span><span class='st-icon-config st-config-add-grey tree-icon-add' style='right:30px'></span><span class='st-icon-config st-config-delete-grey tree-icon-delete'  style='right:37px'></span>";
      } else {
        span = "<span class='st-icon-config st-config-add-grey tree-icon-add'></span><span class='st-icon-config st-config-delete-grey tree-icon-delete'></span>";
      }
      let $div = $('<div>', {
        class: 'treeUlDiv',
        html: "<span class=' icon-front'></span> <span class='icon-font'>" + n.text + '</span>' + span
      });
      let $ul1 = $('<ul>', {});
      $li.append($div);
      $li.append($ul1);
      $li.data('tree_data', n);
      this.$addLi.children('ul').append($li);
    },
    allPositionData () {
      let dataArray = [];
      if (this.newType2 === '1') {
        $('#treeUl3').children('li').each(function () {
          dataArray.push($(this).data('tree_data'));
        });
      } else if (this.newType2 === '2') {
        $('#treeUl2').children('li').each(function () {
          dataArray.push($(this).data('tree_data'));
        });
      }
      getPosition(dataArray, this);
    },

    // 实时编辑树节点
    flag_edit () {
      this.$editLi.find('.icon-font').text(this.child_edit_data);
    },

    rolePowerList () {
      $('#treeUl').html('');
      this.init(this.passData);
      let dataArray = [];
      $('#treeUl').children('li').each(function () {
        dataArray.push($(this).data('tree_data'));
      });
      loopList(dataArray, this);
    },

    savePowerFlag () {
      let _this = this;
      _this.havePowerPoint = [];
      let dataArray = [];
      $('#treeUl').children('li').each(function () {
        dataArray.push($(this).data('tree_data'));
      });
      getCheckPointList(dataArray, _this);
      _this.axios.post('/core/auth/privilege/save',
        [
          _this.saveRoleId, _this.havePowerPoint
        ]
      ).then(function (response) {
        // 获得数据
        if (response) {
          _this.$Message.success('提交成功');
          _this.$emit('savePowerSuccess', null);
        }
      }).catch(function (response) {

      });
    }
  },
  methods: {
    /*
     * 菜单树 初始化函数
     * @param {JsonArray} oTree 菜单数据 数据结构参考 MenuTree.getTestData()
     */
    init (oTree) {
      let treeUlId = this.treeUlId ? this.treeUlId : 'treeUl';
      let $ul = $('#' + treeUlId);
      if (this.newType === 3) {
        let $li = $('<li>', {
          id: '',
          html: "<div><span style='display:none'></span></span><span style='display:none'></span><span style='display:none'></span><span style='position: absolute;top: -30px;right: 50px;' class='st-icon-config st-config-add-grey tree-icon-add' >"
        })
        let $ul1 = $('<ul>', {});
        $li.append($ul1);
        $li.data('tree_data', null);
        $ul.append($li);
        _recursive(oTree, $ul1, this);
        $('.treeUl').slideDown();
      } else {
        _recursive(oTree, $ul, this);
        $('.treeUl').slideDown();
      }
    },

    /**
     * 删除树节点
     */
    delTreeData () {
      let _this = this;
      _this.delModel = false;
      if (_this.newType !== '2' && _this.newType2 !== '1') {
        _this.$delLi.remove();
      }
      _this.$emit('delTreeNode', _this.currentDelData);
    },

    /**
     * 增加树的节点
     */
    addTreeData (data) {
      let _this = this;
      _this.$emit('addTreeNode', data);
    }
  }
}
function _recursive (oTree, $ul, dataThis) {
  if (dataThis.newType !== '2' && dataThis.newType !== '1') {
    let flag = true;
    $.each(oTree, function (i, n) {
      let $li = $('<li>', {
        id: n.id
      });
      let span = '';
      if (dataThis.newType === 3) {
        span = "<span class='st-icon-config st-config-edit-grey tree-icon-edit'></span><span class='st-icon-config st-config-add-grey tree-icon-add' style='right:30px'></span><span class='st-icon-config st-config-delete-grey tree-icon-delete' style='right:37px'></span>";
      } else {
        span = "<span class='st-icon-config st-config-add-grey tree-icon-add'></span><span class='st-icon-config st-config-delete-grey tree-icon-delete'></span>";
      }
      if (n.children && n.children.length > 0) {
        let html = '';
        if (n.pid === null || n.pid === '') {
          html = "<span class='st-icon-config icon-front st-config-function'></span> <span class='icon-font'>" + n.text + "</span><span class='st-icon-config st-config-add-grey tree-icon-add  '></span><span class='st-icon-config st-config-delete-grey tree-icon-delete'></span>";
        } else {
          html = "<span class='st-icon-config icon-front st-config-spread'></span> <span class='icon-font'>" + n.text + '</span>' + span;
          flag = false;
        }
        let $div = $('<div>', {
          class: 'treeUlDiv',
          html: html
        });
        let $ul1 = null;
        if (flag) {
          $ul1 = $('<ul>', {});
        } else {
          $ul1 = $('<ul>', {
            style: 'display:none'
          });
        }
        $li.append($div);
        $li.append($ul1);
        let obj = Object.assign({}, n);
        // 移除节点数据
        delete obj.children;
        $li.data('tree_data', obj);
        _recursive(n.children, $ul1, dataThis);
      } else {
        let $div = $('<div>', {
          class: 'treeUlDiv',
          html: "<span class=' icon-front'></span> <span class='icon-font'>" + n.text + '</span>' + span
        });
        let $ul1 = $('<ul>', {});
        $li.append($div);
        $li.append($ul1);
        $li.data('tree_data', n);
      }
      $ul.append($li);
    });
  } else if (dataThis.newType === '2') {
    let flag = true;
    $.each(oTree, function (i, n) {
      let $li = $('<li>', {
        id: n.id
      });
      if (n.children && n.children.length > 0) {
        let html = '';
        if (n.pid === null || n.pid === '') {
          if (dataThis.newType2 === '1') {
            html = "<span class='st-icon-config icon-front st-config-function'></span> <span class='icon-font'>" + n.name + '</span>';
            flag = false;
          } else if (dataThis.newType2 === '2') {
            html = "<span class='st-icons icon-front st-icons-shiAdd'></span> <span class='icon-font'>" + n.name + '</span>';
            flag = false;
          } else {
            html = "<span class='st-icon-config icon-front st-config-function'></span> <span class='icon-font'>" + n.name + "</span><span class='st-icon-config st-config-add-grey tree-icon-add  '></span><span class='st-icon-config st-config-delete-grey tree-icon-delete'></span>";
          }
        } else {
          if (dataThis.newType2 === '1') {
            html = "<span class='st-icon-config icon-front st-config-spread'></span> <span class='icon-font'>" + n.name + '</span>';
          } else if (dataThis.newType2 === '2') {
            html = "<span class='st-icons icon-front st-icons-shiAdd'></span> <span class='icon-font'>" + n.name + '</span>';
          } else {
            html = "<span class='st-icon-config icon-front st-config-spread'></span> <span class='icon-font'>" + n.name + "</span><span class='st-icon-config st-config-add-grey tree-icon-add  '></span><span class='st-icon-config st-config-delete-grey tree-icon-delete'></span>";
          }
          flag = false;
        }
        let $div = null;
        if (dataThis.newType2 === '2') {
          $div = $('<div>', {
            class: 'treeUlDiv',
            style: 'border:none',
            html: html
          });
        } else {
          $div = $('<div>', {
            class: 'treeUlDiv',
            html: html
          });
        }
        let $ul1 = null;
        if (flag) {
          $ul1 = $('<ul>', {});
        } else {
          $ul1 = $('<ul>', {
            style: 'display:none'
          });
        }
        $li.append($div);
        $li.append($ul1);
        let obj = Object.assign({}, n, {'haveChildren': 1});
        // 移除节点数据
        delete obj.children;
        $li.data('tree_data', obj);
        _recursive(n.children, $ul1, dataThis)
      } else {
        let html = '';
        if (dataThis.newType2 === '1') {
          html = "<span class='st-icon-config'></span> <span class='icon-font'>" + n.name + "</span><span class='st-icon-config st-config-add-grey tree-icon-add  '></span>";
        } else if (dataThis.newType2 === '2') {
          html = "<span class='st-icons icon-front st-icons-xuAdd'></span> <span class='icon-font'>" + n.name + '</span>';
        } else {
          html = "<span class='st-icon-config'></span> <span class='icon-font'>" + n.name + "</span><span class='st-icon-config st-config-add-grey tree-icon-add  '></span><span class='st-icon-config st-config-delete-grey tree-icon-delete'></span>";
        }
        let $div = null;
        if (dataThis.newType2 === '2') {
          $div = $('<div>', {
            class: 'treeUlDiv',
            style: 'border:none',
            html: html
          });
        } else {
          $div = $('<div>', {
            class: 'treeUlDiv',
            html: html
          });
        }
        let $ul1 = $('<ul>', {});
        $li.append($div);
        $li.append($ul1);
        $li.data('tree_data', n);
      }
      $ul.append($li);
    });
  } else {
    let flag = true;
    $.each(oTree, function (i, n) {
      let $li = $('<li>', {
        id: n.id
      });
      if (n.children && n.children.length > 0) {
        let html = '';
        if (n.pid === null || n.pid === '') {
          html = "<span class='st-icon-config icon-front st-config-function'></span> <span class='icon-font'>" + n.text + '</span>';
        } else {
          html = "<span class='st-icon-config icon-front st-config-spread'></span> <span class='icon-font'>" + n.text + '</span>';
          flag = false;
        }
        let $div = $('<div>', {
          class: 'treeUlDiv',
          html: html
        });
        let $ul1 = null;
        if (flag) {
          $ul1 = $('<ul>', {});
        } else {
          $ul1 = $('<ul>', {
            style: 'display:none'
          });
        }
        $li.append($div);
        $li.append($ul1);
        let obj = Object.assign({}, n, {'haveChildren': 1});
        // 移除节点数据
        delete obj.children;
        $li.data('tree_data', obj);
        _recursive(n.children, $ul1, dataThis);
      } else {
        let html = "<span class='st-icon-config'></span> <span class='icon-font'>" + n.text + '</span>';
        let $div = $('<div>', {
          class: 'treeUlDiv',
          html: html
        });
        let $ul1 = $('<ul>', {});
        $li.append($div);
        $li.append($ul1);
        $li.data('tree_data', n);
      }
      // }
      $ul.append($li);
    });
  }
}
function fatherHaveCheck (data) {
  if (data.pid !== 1 && data.pid !== undefined) {
    let parentData = $('#' + data.pid).data('tree_data');
    if (parentData.templeChecked === undefined || !parentData.templeChecked) {
      let noneFlag = false;
      $('#' + data.id).siblings().each(function () {
        if (!$(this).data('tree_data').checked) {
          noneFlag = true;
        }
      });
      // 如果全被选中
      if (!data.checked) {
        noneFlag = true;
      }
      if (!noneFlag) {
        $('#' + data.pid).children().eq(0).prepend("<span class='st-icon-config st-config-rightB'></span>");
        let obj = Object.assign({}, parentData, {'templeChecked': false, 'checked': true});
        $('#' + data.pid).data('tree_data', obj);
        fatherHaveCheck($('#' + data.pid).data('tree_data'));
      } else {
        $('#' + data.pid).children().eq(0).prepend("<span class='st-icon-config st-config-rightA'></span>");
        let obj = Object.assign({}, parentData, {'templeChecked': true, 'checked': false});
        $('#' + data.pid).data('tree_data', obj);
        fatherHaveCheck($('#' + data.pid).data('tree_data'));
      }
    } else {
      let noneFlag = false;
      $('#' + data.id).siblings().each(function () {
        if (!$(this).data('tree_data').checked) {
          noneFlag = true;
        }
      });
      // 已经是有了的，想要变只有满了之后才能变
      if (!noneFlag) {
        $('#' + data.pid).children().eq(0).children().eq(0).remove();
        $('#' + data.pid).children().eq(0).prepend("<span class='st-icon-config st-config-rightB'></span>");
        let obj = Object.assign({}, parentData, {'templeChecked': true, 'checked': true});
        $('#' + data.pid).data('tree_data', obj);
        fatherHaveCheck($('#' + data.pid).data('tree_data'));
      }
    }
  } else if (data.pid === 1) {
    // 已经选的是根节点了，不要操作
  }
}
function fatherNoCheck (data) {
  if (data.pid !== 1 && data.pid !== undefined) {
    let noneFlag = false;
    $('#' + data.id).siblings().each(function () {
      if ($(this).data('tree_data').checked || $(this).data('tree_data').templeChecked) {
        noneFlag = true;
      }
    });
    if (data.templeChecked && data.haveChildren === 1) {
      noneFlag = true;
    }
    // 如果同辈没了，他也没了，还有一个，改成有
    let parentData = $('#' + data.pid).data('tree_data');
    if (noneFlag) {
      $('#' + data.pid).children().eq(0).children().eq(0).remove();
      $('#' + data.pid).children().eq(0).prepend("<span class='st-icon-config st-config-rightA'></span>");
      let obj = Object.assign({}, parentData, {'templeChecked': true, 'checked': false});
      $('#' + data.pid).data('tree_data', obj);
      fatherNoCheck($('#' + data.pid).data('tree_data'));
    } else {
      // 全空
      $('#' + data.pid).children().eq(0).children().eq(0).remove();
      let obj = Object.assign({}, parentData, {'templeChecked': false, 'checked': false});
      $('#' + data.pid).data('tree_data', obj);
      fatherNoCheck($('#' + data.pid).data('tree_data'));
    }
  } else if (data.pid === 1) {
    // 已经选的是根节点了，不要操作
  }
}
function loopList (data, data2) {
  let allHaveFlag = 0;
  let littleFlag = false;
  for (let i in data) {
    if (data[i].haveChildren === 1) {
      let dataArray = [];
      $('#' + data[i].id).children('ul').eq(0).children('li').each(function () {
        dataArray.push($(this).data('tree_data'));
      });
      let getAnswer = loopList(dataArray, data2);
      if (getAnswer === 'little') {
        $('#' + data[i].id).children().eq(0).prepend("<span class='st-icon-config st-config-rightA'></span>");
        let obj = Object.assign({}, $('#' + data[i].id).data('tree_data'), {'templeChecked': true, 'checked': false});
        $('#' + data[i].id).data('tree_data', obj);
        littleFlag = true;
      } else if (getAnswer === 'all') {
        allHaveFlag++;
        $('#' + data[i].id).children().eq(0).prepend("<span class='st-icon-config st-config-rightB'></span>");
        let obj = Object.assign({}, $('#' + data[i].id).data('tree_data'), {'templeChecked': true, 'checked': true});
        $('#' + data[i].id).data('tree_data', obj);
      }
    } else {
      for (let j in data2.rolePowerList) {
        if (data2.rolePowerList[j] === data[i].id) {
          allHaveFlag++;
          $('#' + data[i].id).children().eq(0).prepend("<span class='st-icon-config st-config-rightB'></span>");
          let obj = Object.assign({}, $('#' + data[i].id).data('tree_data'), {'checked': true});
          $('#' + data[i].id).data('tree_data', obj);
        }
      }
    }
  }
  if (allHaveFlag === 0) {
    return 'none';
  } else if ((allHaveFlag > 0 && allHaveFlag < data.length) || littleFlag) {
    return 'little';
  } else {
    return 'all';
  }
}
function getCheckPointList (data, data2) {
  let allHaveFlag = 0;
  for (let i in data) {
    if (data[i].haveChildren === 1) {
      let dataArray = [];
      $('#' + data[i].id).children('ul').eq(0).children('li').each(function () {
        dataArray.push($(this).data('tree_data'));
      });
      let getAnswer = getCheckPointList(dataArray, data2);
      if (getAnswer === 'have') {
        data2.havePowerPoint.push({id: data[i].id});
        allHaveFlag++;
      }
    } else {
      if (data[i].checked) {
        allHaveFlag++;
        data2.havePowerPoint.push({id: data[i].id});
      }
    }
  }
  if (allHaveFlag === 0) {
    return 'none';
  } else {
    return 'have';
  }
}
function getPosition (data, data2) {
  for (let i in data) {
    if (data[i].haveChildren === 1) {
      let dataArray = [];
      $('#' + data[i].id).children('ul').eq(0).children('li').each(function () {
        dataArray.push($(this).data('tree_data'));
      });
      getPosition(dataArray, data2);
    } else {
      for (let j in data2.allPositionData) {
        if (data2.allPositionData[j].department_no === data[i].id) {
          if (data2.newType2 !== '2') {
            $('#' + data[i].id).children().eq(0).children().eq(0).addClass('icon-front st-config-spread');
            $('#' + data[i].id).children().eq(1).append("<li id='" + data2.allPositionData[j].position_no + "'>" +
              "<div class='treeUlDiv'>" +
              "<span class='st-icon-config'></span>" +
              "<span class='icon-font'>" + data2.allPositionData[j].position_name + '</span>' +
              "<span class='st-icon-config st-config-delete-grey tree-icon-delete'></span>" +
              '</div><ul></ul></li>');
          } else {
            $('#' + data[i].id).children().eq(1).append("<li id='" + data2.allPositionData[j].position_no + "'>" +
              "<div class='treeUlDiv' style='border: 0px;'>" +
              "<span class='st-icon-config'></span>" +
              "<span class='icon-font'>" + data2.allPositionData[j].position_name + '</span>' +
              '</div><ul></ul></li>');
            $('#' + data[i].id).find('span').eq(0).removeClass('st-icons-xuAdd');
            $('#' + data[i].id).find('span').eq(0).addClass('st-icons-shiAdd');
          }
          $('#' + data[i].id).children('ul').hide();
          $('#' + data2.allPositionData[j].position_no).data('tree_data', data2.allPositionData[j]);
        }
      }
    }
  }
}
</script>
