"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 _common = require("./common");

var _areaSearch = _interopRequireDefault(require("./area-search"));

var _areasPop = _interopRequireDefault(require("./areas-pop"));

var _areaPicker = _interopRequireDefault(require("./area-picker"));

require("./style");

// const districtLevels = ['province', 'city', 'zone', 'street']
var getRouteName = function getRouteName(node, key, join) {
  if (key === void 0) {
    key = "simpleName";
  }

  if (join === void 0) {
    join = "";
  }

  var routeNames = [node[key]];
  var currentNode = node._parent;

  while (currentNode && currentNode.districtID && currentNode.districtID !== "all") {
    routeNames.push(currentNode[key]);
    currentNode = currentNode._parent;
  }

  return routeNames.reverse().join(join);
};

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

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

  return _c('div', {
    staticClass: "ks-area-select-multi"
  }, [_c('area-search', {
    ref: "areaSearch",
    attrs: {
      "placeholder": _vm.placeholder
    },
    on: {
      "closeDropdown": _vm.closeDropdown,
      "searchSelect": _vm.searchSelect
    },
    model: {
      value: _vm.searchListShow,
      callback: function callback($v) {
        _vm.searchListShow = $v;
      },
      expression: "searchListShow"
    }
  }), _vm._v(" "), _c('div', {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: !_vm.searchListShow,
      expression: "!searchListShow"
    }],
    staticClass: "formItem-input"
  }, [_c('div', {
    staticClass: "formItem-input-content"
  }, [_c('ks-dropdown-menu', {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.textOver,
      expression: "textOver"
    }],
    attrs: {
      "active-color": "#03050D",
      "z-index": "10002",
      "overlay": false
    }
  }, [_c('ks-dropdown-item', {
    ref: "areaPopDropdownItem",
    staticClass: "areapop-dropdown",
    attrs: {
      "title-class": "ks-area-txt-icon"
    },
    on: {
      "opened": function opened($event) {
        _vm.showAreaPop = true;
      },
      "close": function close($event) {
        _vm.showAreaPop = false;
      }
    },
    scopedSlots: _vm._u([{
      key: "title",
      fn: function fn() {
        return [_c('div', {
          staticClass: "nav-item"
        }, [_vm.showAreaPop ? _c('img', {
          attrs: {
            "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-area-select/arrow-up@2x.png",
            "alt": "收起"
          }
        }) : _c('img', {
          attrs: {
            "src": "https://erphostjs.kye-erp.com/kuasheng-h5/assets/ks-area-select/arrow-down@2x.png",
            "alt": "展开"
          }
        })])];
      },
      proxy: true
    }])
  }, [_vm._v(" "), _c('area-list-pop', {
    style: {
      height: _vm.selectedPopHeight + 'px'
    },
    attrs: {
      "areaList": _vm.selectDisplayData
    },
    on: {
      "remove": _vm.remove
    }
  })], 1)], 1), _vm._v(" "), !_vm.showAreaPop ? _c('div', {
    ref: "areaNamesBox",
    staticClass: "formItem-input-content-tags"
  }, [_vm.selectDisplayData && _vm.selectDisplayData.length ? _c('div', {
    ref: "areaNames",
    staticClass: "formItem-input-value"
  }, _vm._l(_vm.selectDisplayData, function (item) {
    return _c('span', {
      key: item.districtID,
      staticClass: "txtItem"
    }, [_vm._v(_vm._s(item.name) + " "), _c('ks-icon', {
      attrs: {
        "name": "cross"
      },
      on: {
        "click": function click($event) {
          return _vm.remove(item);
        }
      }
    })], 1);
  }), 0) : _c('div', {
    staticClass: "formItem-input-placeholder"
  }, [_vm._v(_vm._s(_vm.selectPlaceholder))])]) : _c('div', {
    staticClass: "area-pop-tip"
  }, [_vm._v("已选区域")])], 1)]), _vm._v(" "), _c('div', {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: !_vm.searchListShow,
      expression: "!searchListShow"
    }],
    staticClass: "ks-area-picker-wrap"
  }, [_c('areaPicker', {
    ref: "areaPickerRef",
    attrs: {
      "level": _vm.levelOptions.length
    }
  })], 1), _vm._v(" "), _c('div', {
    staticClass: "bot-btn-wrap"
  }, [_c('ks-button', {
    staticClass: "btn-normal",
    attrs: {
      "type": "primary",
      "plain": "",
      "round": ""
    },
    on: {
      "click": _vm.onReset
    }
  }, [_vm._v("重置")]), _vm._v(" "), _c('ks-button', {
    attrs: {
      "type": "primary",
      "round": ""
    },
    on: {
      "click": _vm.onSubmit
    }
  }, [_vm._v("确定" + _vm._s(_vm.selectDisplayData.length ? "(" + _vm.selectDisplayData.length + ")" : ""))])], 1)], 1);
};

var __vue_staticRenderFns__ = [];
var _default2 = {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: "ks-area-select",
  props: {
    // 最多可选择
    maxlength: {
      type: Number
    },
    placeholder: {
      type: String,
      default: "请输入搜索内容"
    },
    selectPlaceholder: {
      type: String,
      default: "请选择区域"
    },
    // 是否包括大区
    isArea: {
      type: Boolean,
      default: false
    },
    // 热门城市列表
    hotList: {
      type: Array,
      default: function _default() {
        return _common.HOT_CITY_LIST;
      }
    },
    // 地址树数据
    areaData: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    // 只读状态
    readOnly: {
      type: Boolean,
      default: false
    },
    levelOptions: {
      type: Array,
      default: function _default() {
        return [{
          name: "省/直辖市",
          disabled: false
        }, {
          name: "城市"
        }, {
          name: "区/县"
        }, {
          name: "镇/街道"
        }];
      }
    }
  },
  components: {
    areaSearch: _areaSearch.default,
    areaListPop: _areasPop.default,
    areaPicker: _areaPicker.default
  },
  data: function data() {
    return {
      idMap: null,
      selectedPopHeight: 200,
      checkedData: [],
      searchListShow: false,
      showAreaPop: false,
      textOver: false
    };
  },
  computed: {
    selectDisplayData: function selectDisplayData() {
      var displayData = this.checkedData.map(function (node) {
        return {
          districtID: node.districtID,
          name: getRouteName(node)
        };
      }) || [];
      return displayData;
    }
  },
  watch: {
    selectDisplayData: function selectDisplayData() {
      var _this = this;

      setTimeout(function () {
        if (_this.$refs.areaNames) {
          _this.textOver = _this.$refs.areaNames.clientWidth > _this.$refs.areaNamesBox.clientWidth;

          if (_this.textOver) {
            _this.selectedPopHeight = _this.$refs.areaPickerRef.$el.clientHeight + 56;
          }
        }
      }, 0);
    }
  },
  // updated() {
  //   this.$refs.areaSearch.handleCancel()
  // },
  methods: {
    closeDropdown: function closeDropdown() {
      this.$refs.areaPopDropdownItem && this.$refs.areaPopDropdownItem.toggle(false);
    },
    closePop: function closePop() {
      // this.searchListShow = false
      this.$refs.areaSearch && this.$refs.areaSearch.handleCancel();
      this.closeDropdown();
    },
    remove: function remove(item) {
      if (this.idMap) {
        var mapItem = this.idMap.get(item.districtID); // console.error(mapItem, 'mapItem')

        this.$refs.areaPickerRef.checkboxClick(mapItem, mapItem.index, mapItem._erpZoom);
      }
    },
    searchSelect: function searchSelect(item) {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var mapItem;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (item.isChecked) {
                  _context.next = 5;
                  break;
                }

                _context.next = 3;
                return _this2.$refs.areaPickerRef.showHistoryDataChecked([{
                  districtID: item.districtID,
                  districtName: item.districAllName,
                  simpleName: item.districtName
                }]);

              case 3:
                _context.next = 9;
                break;

              case 5:
                if (!_this2.idMap) {
                  _context.next = 9;
                  break;
                }

                mapItem = _this2.idMap.get(item.districtID);
                _context.next = 9;
                return _this2.$refs.areaPickerRef.checkboxClick(mapItem, mapItem.index, mapItem._erpZoom);

              case 9:
                _this2.$nextTick(function () {
                  _this2.$refs.areaSearch.updatedData();
                });

              case 10:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    onSubmit: function onSubmit() {
      this.$emit("change", this.checkedData.map(function (item) {
        return {
          districtLevel: item.districtLevel,
          districtID: item.districtID,
          allDistrictID: getRouteName(item, "districtID", "/"),
          districtName: item.simpleName,
          allDistrictName: getRouteName(item, "simpleName", "/")
        };
      }));
      this.$emit("close");
    },
    onReset: function onReset() {
      this.textOver = false;
      this.searchListShow = false;
      this.closeDropdown();
      this.$refs.areaPickerRef.clearAll();
      this.$refs.areaSearch.updatedData();
    }
  }
};
exports.default = _default2;