import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _extends from "@babel/runtime/helpers/esm/extends";

var _render$staticRenderF;

import ksButton from '../button';
import ksCheckbox from '../checkbox';
import ksIcon from '../icon';
import KyeInputSearch from '../input-search';
import { deepSearch } from './utils.js';
import './tree-input.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-input"
  }, [_c('kye-input-search', {
    ref: "ksInputSearch",
    attrs: {
      "value": _vm.searchVal,
      "clearable": false,
      "placeholder": _vm.placeholder
    },
    on: {
      "focus": _vm.focus,
      "blur": _vm.blur,
      "change": _vm.change
    },
    scopedSlots: _vm._u([_vm.clearable ? {
      key: "right-icon",
      fn: function fn() {
        return [_c('div', {
          directives: [{
            name: "show",
            rawName: "v-show",
            value: _vm.showClearVal.length > 0,
            expression: "showClearVal.length > 0"
          }],
          staticStyle: {
            "color": "#c8c9cc",
            "padding": "0 8px"
          },
          on: {
            "click": function click($event) {
              $event.stopPropagation();
              return _vm.clear($event);
            }
          }
        }, [_c('ks-icon', {
          attrs: {
            "name": "clear"
          }
        })], 1)];
      },
      proxy: true
    } : null], null, true)
  }), _vm._v(" "), _c('transition', {
    attrs: {
      "name": "el-zoom-in-top"
    }
  }, [_c('div', {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.showList,
      expression: "showList"
    }],
    staticClass: "ks-org-input-search-list"
  }, [_c('ul', {
    staticClass: "ks-org-input-search-list-ul"
  }, [_vm._l(_vm.orgList, function (item) {
    return _c('li', {
      key: item.id,
      staticClass: "ks-org-input-search-list-list-item",
      on: {
        "click": function click($event) {
          return _vm.selectOrg(item);
        }
      }
    }, [_c('div', {
      staticClass: "ks-org-input-search-list-item-name",
      domProps: {
        "innerHTML": _vm._s(item.$name)
      }
    }), _vm._v(" "), _vm.multiple ? _c('div', {
      staticStyle: {
        "margin-right": "18px"
      }
    }, [_c('ks-checkbox', {
      attrs: {
        "value": _vm.getItemCheckedStatus(item.id, item),
        "shape": "square",
        "icon-size": "14px"
      },
      on: {
        "click": function click($event) {
          $event.stopPropagation();
          return function (e) {
            return _vm.nodeChecked(e, item);
          }($event);
        }
      }
    })], 1) : _vm._e()]);
  }), _vm._v(" "), _c('div', {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: !_vm.orgList.length,
      expression: "!orgList.length"
    }],
    staticClass: "ks-org-input-search-list-no-data-container"
  }, [_c('img', {
    staticClass: "ks-org-input-search-list-no-data-container-img",
    attrs: {
      "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/empty-image/NoResults.png"
    }
  }), _vm._v(" "), _c('div', {
    staticClass: "ks-org-input-search-list-no-data-container-text"
  }, [_vm._v("\n            没有找到"), _vm.searchVal ? _c('span', [_vm._v("“")]) : _vm._e(), _c('span', {
    staticClass: "ks-org-input-search-list-no-data-container-text-label"
  }, [_vm._v(_vm._s(_vm.searchVal))]), _vm.searchVal ? _c('span', [_vm._v("”")]) : _vm._e(), _vm._v("相关结果\n          ")])])], 2), _vm._v(" "), _vm.orgList.length && _vm.multiple ? _c('div', {
    staticClass: "ks-org-input-search-list-btn"
  }, [_c('ks-button', {
    staticStyle: {
      "min-width": "80px",
      "max-width": "80px",
      "font-size": "14px"
    },
    attrs: {
      "type": "primary"
    },
    on: {
      "click": _vm.handleClickBtn
    }
  }, [_vm._v("确定")])], 1) : _vm._e()])])], 1);
};

var __vue_staticRenderFns__ = [];
export default (_render$staticRenderF = {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: 'ks-tree-input',
  components: {
    KyeInputSearch: KyeInputSearch
  },
  props: {
    clearable: Boolean,
    inputNumber: {
      type: Number,
      default: 0
    },
    value: {
      type: String,
      default: ''
    },
    treeData: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data: function data() {
    return {
      searchVal: this.value,
      placeholder: '请搜索关键词',
      showClearVal: '',
      showList: false,
      orgList: []
    };
  }
}, _render$staticRenderF["components"] = {
  KyeInputSearch: KyeInputSearch
}, _render$staticRenderF.methods = {
  handleClickBtn: function handleClickBtn() {
    this.showList = false;
    this.$emit('handleClickMultipleBtn');
  },
  getItemCheckedStatus: function getItemCheckedStatus(id, node) {
    var that = this;
    var item = deepSearch(id, this.treeData);

    var _ref = item || {},
        _ref$checked = _ref.checked,
        checked = _ref$checked === void 0 ? false : _ref$checked,
        _ref$parentIdList = _ref.parentIdList,
        parentIdList = _ref$parentIdList === void 0 ? [] : _ref$parentIdList; // 如果父级中的某个节点被选中，则勾上


    var parentNodeIsChecked = parentIdList.length && parentIdList.some(function (x) {
      var parentNode = deepSearch(x, that.treeData);

      if (parentNode && parentNode.checked) {
        return true;
      }
    });
    this.$set(node, 'checked', parentNodeIsChecked || checked);
    return node.checked;
  },
  nodeChecked: function nodeChecked(e, node) {
    var checkedVm = e.currentTarget && e.currentTarget.__vue__;
    var v = checkedVm.value; // 反选

    node.checked = !node.checked;
    checkedVm && this.$emit('inputChecked', _extends({}, node, {
      checked: v
    }));
  },
  setSearchValue: function setSearchValue(val) {
    this.searchVal = val;
    this.$refs.ksInputSearch.setValue(val);
  },
  focus: function focus() {
    this.$emit('focus');
  },
  blur: function blur() {
    this.$emit('blur');
  },
  change: function change(val) {
    this.showClearVal = val;
    this.searchVal = val;
    this.searchInput(val);
    this.$emit('change', val);
  },
  clear: function clear() {
    this.searchVal = '';
    this.showClearVal = '';
    this.$refs.ksInputSearch && this.$refs.ksInputSearch.setValue('');
    this.orgList = [];
    this.showList = false;
    this.$emit('clear');
  },

  /*
   * 即时搜索
   */
  searchInput: function searchInput() {
    var _this = this;

    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(_this.searchVal.length === _this.inputNumber)) {
                _context.next = 4;
                break;
              }

              _this.orgList = [];
              _this.showList = false;
              return _context.abrupt("return");

            case 4:
              _this.showList = true;
              _context.next = 7;
              return _this.getOrgList();

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  },

  /*
   * 获取搜索数据
   */
  getOrgList: function getOrgList() {
    var _this2 = this;

    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
      var v, res;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              v = _this2.searchVal;
              _this2.showList = true;
              _context2.next = 4;
              return _this2.$http('hr.department.refactoring.getByFuzzyValue', 10179, {
                fuzzyValue: v
              });

            case 4:
              res = _context2.sent;
              _this2.orgList = _this2.processOrgList(v, res);

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }))();
  },
  processOrgList: function processOrgList(v, res) {
    if (!res || !res.length) {
      return [];
    }

    var reg = new RegExp(v, 'gi');
    res = res.map(function (v) {
      var $name = v.departmentRoute.replace(/\//g, " / "); // / 的前后添加空格

      $name = $name.replace(reg, function (value) {
        return "<span style=\"color: #7972fe;\">" + value + "</span>";
      });
      var id = v.id,
          parentId = v.parentId,
          name = v.name;
      return {
        id: id,
        parentId: parentId,
        name: name,
        $name: $name
      };
    });
    return res;
  },
  selectOrg: function selectOrg(val) {
    if (this.multiple) {
      return;
    }

    this.showList = false;
    this.searchVal = val.name;
    this.$emit('selectOrg', val);
  }
}, _render$staticRenderF);