"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _gridItem = _interopRequireDefault(require("../grid-item"));

var _grid = _interopRequireDefault(require("../grid"));

var _button = _interopRequireDefault(require("../button"));

var _tree = _interopRequireDefault(require("./tree"));

var _treeInput = _interopRequireDefault(require("./tree-input"));

var _utils = require("./utils");

// import './index.css'
var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "ks-org-tree"
  }, [_c('div', {
    staticClass: "ks-org-tree-header"
  }, [_vm._t("title", [_vm.showTitle ? _c('div', {
    staticClass: "ks-org-tree-header-title"
  }, [_c('ks-icon', {
    attrs: {
      "name": "arrow-left",
      "color": "#000",
      "size": "22"
    },
    on: {
      "click": _vm.onClose
    }
  }), _vm._v(" "), _c('span', {
    staticClass: "ks-org-tree-header-title-label"
  }, [_vm._v("选择组织")])], 1) : _vm._e()]), _vm._v(" "), _c('tree-input', {
    ref: "treeInput",
    staticClass: "ks-org-tree-header-input",
    staticStyle: {
      "width": "100%"
    },
    attrs: {
      "multiple": _vm.multiple,
      "clearable": true,
      "inputNumber": _vm.inputNumber,
      "tree-data": _vm.treeData
    },
    on: {
      "change": _vm.change,
      "selectOrg": _vm.selectOrg,
      "inputChecked": _vm.inputChecked,
      "handleClickMultipleBtn": _vm.handleClickMultipleBtn,
      "clear": _vm.clearInput
    }
  })], 2), _vm._v(" "), _c('div', {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.visibleTree,
      expression: "visibleTree"
    }],
    staticClass: "ks-org-tree-wrapper"
  }, [_c('tree', {
    ref: "tree",
    attrs: {
      "multiple": _vm.multiple,
      "treeId": _vm.treeId,
      "default-checked-keys": _vm.defaultCheckedKeys
    },
    on: {
      "node-click": _vm.treeNodeClick,
      "node-checked": _vm.checkedListChange,
      "updateTreeData": _vm.updateTreeData
    },
    scopedSlots: _vm._u([{
      key: "treeNode",
      fn: function fn(ref) {
        var node = ref.node;
        var attrs = ref.attrs;
        return [_vm._t("treeNode", null, {
          "node": node,
          "attrs": attrs
        })];
      }
    }], null, true)
  })], 1), _vm._v(" "), _c('div', {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.visibleTree,
      expression: "visibleTree"
    }],
    staticClass: "ks-org-tree-footer"
  }, [_c('ks-grid', {
    attrs: {
      "column-num": "2",
      "border": false
    }
  }, [_c('ks-grid-item', [_c('ks-button', {
    staticClass: "btn-normal",
    attrs: {
      "size": "large",
      "disabled": _vm.btnDisabled
    },
    on: {
      "click": _vm.onReset
    }
  }, [_vm._v("重置")])], 1), _vm._v(" "), _c('ks-grid-item', [_c('ks-button', {
    attrs: {
      "type": "primary",
      "size": "large",
      "disabled": _vm.btnDisabled
    },
    on: {
      "click": _vm.handleChange
    }
  }, [_vm._v("确定")])], 1)], 1)], 1)]);
};

var __vue_staticRenderFns__ = [];
var _default2 = {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: 'ks-org-tree',
  components: {
    tree: _tree.default,
    treeInput: _treeInput.default,
    ksButton: _button.default,
    ksGrid: _grid.default,
    KsGridItem: _gridItem.default
  },
  props: {
    defaultCheckedKeys: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    inputNumber: {
      type: Number,
      default: 0
    },
    treeId: {
      type: String,
      default: ''
    },
    showTitle: {
      type: Boolean,
      default: true
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data: function data() {
    this.treeData = [];
    return {
      selectNode: null,
      visibleTree: true,
      checkedList: []
    };
  },
  computed: {
    btnDisabled: function btnDisabled() {
      return this.multiple ? !this.checkedList.length : !this.selectNode;
    }
  },
  mounted: function mounted() {
    this.open();
  },
  methods: {
    getMultipleTreeData: function getMultipleTreeData() {
      // 递归找到被选中的数据
      // 规则1 如果所有的子集被勾上，则只返回 父级， 反之 返回所有的数据
      var data = [];
      this.treeData.forEach(function iter(node) {
        var _ref = node || {},
            _ref$children = _ref.children,
            children = _ref$children === void 0 ? [] : _ref$children;

        var hasChild = (0, _utils.hasArrLength)(children);
        var isCheckedAllChild = hasChild && children.every(function (x) {
          return x.checked;
        });
        isCheckedAllChild && data.push(node);
        !hasChild && !isCheckedAllChild && node.checked && data.push(node);
        hasChild && node.children.forEach(iter);
      });
      var idList = data.map(function (x) {
        return x.id;
      });
      data = data.filter(function (ele) {
        return !ele.parentIdList.some(function (id) {
          return idList.indexOf(id) > -1;
        });
      });
      return data;
    },
    updateTreeData: function updateTreeData(treeData) {
      this.treeData = treeData;
    },
    inputChecked: function inputChecked(node) {
      var id = node.id;
      var item = (0, _utils.deepSearch)(id, this.treeData);
      this.$refs.tree.nodeChecked(item);
    },
    checkedListChange: function checkedListChange(checkedList, treeData) {
      this.checkedList = checkedList;
      this.treeData = treeData;
    },
    onClose: function onClose() {
      this.$emit('onClose');
    },
    change: function change(val) {
      this.visibleTree = !val;
    },
    clearInput: function clearInput() {
      this.visibleTree = true;
      this.$emit('clear');
    },
    treeNodeClick: function treeNodeClick(item) {
      if (this.multiple) {
        // 多选的时候不改变input
        this.$emit('node-click', item);
        return;
      }

      this.selectNode = item;
      item && item.name && this.$refs.treeInput.setSearchValue(item.name);
      this.$emit('node-click', item);
    },
    queryNodeDetail: function queryNodeDetail(id) {
      var _this = this;

      return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _this.$refs.tree.queryNodeDetail(id);

              case 2:
                return _context.abrupt("return", _context.sent);

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    getTreeData: function getTreeData() {
      return this.$refs.tree.treeData;
    },
    setNodeStatus: function setNodeStatus(id) {
      this.$refs.tree.setNodeStatus(id);
    },
    open: function open() {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
        var _ref2, _ref2$id, id, defaultId, item;

        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _this2.$refs.tree.openTree();

                _ref2 = _this2.selectNode || {}, _ref2$id = _ref2.id, id = _ref2$id === void 0 ? '' : _ref2$id;

                if (_this2.multiple) {
                  _context2.next = 11;
                  break;
                }

                defaultId = _this2.defaultCheckedKeys[0];

                if (!(defaultId !== id)) {
                  _context2.next = 10;
                  break;
                }

                _context2.next = 7;
                return _this2.queryNodeDetail(defaultId);

              case 7:
                item = _context2.sent;
                item.name && _this2.$refs.treeInput.setSearchValue(item.name);
                _this2.selectNode = item;

              case 10:
                _this2.setNodeStatus(defaultId);

              case 11:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    },
    handleClickMultipleBtn: function handleClickMultipleBtn() {
      this.visibleTree = true;
    },
    selectOrg: function selectOrg(val) {
      if (this.multiple) {
        return;
      }

      this.visibleTree = true;
      this.selectNode = val;
      this.$refs.tree.setNodeStatus(val.id);
      this.handleChange();
    },

    /*
     * 确定
     */
    handleChange: function handleChange() {
      if (this.multiple) {
        var data = this.getMultipleTreeData();
        this.$emit('handleChange', data);
        return;
      }

      if (!this.selectNode) {
        window.vant.Toast('请选择组织');
        return;
      }

      this.$emit('handleChange', this.selectNode);
    },

    /*
     * 重置
     */
    onReset: function onReset() {
      this.inputValue = '';
      this.$refs.tree.resetNodeStatus();
    }
  }
};
exports.default = _default2;