"use strict";

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

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

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

var _ksUtils = require("../ks-utils");

var method = 'address.regioninfo.getAreaAddress';
var _levels = ['province', 'city', 'zone', 'street'];

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-search",
    style: _vm.value ? 'height:100%' : ''
  }, [_c('ks-search', {
    attrs: {
      "show-action": _vm.value,
      "placeholder": _vm.placeholder
    },
    on: {
      "focus": _vm.onFocus,
      "input": _vm.handleInput,
      "cancel": _vm.handleCancel
    },
    model: {
      value: _vm.currentValue,
      callback: function callback($v) {
        _vm.currentValue = $v;
      },
      expression: "currentValue"
    }
  }), _vm._v(" "), _vm.value && _vm.list.length ? _c('div', {
    staticClass: "list"
  }, _vm._l(_vm.list, function (item) {
    return _c('div', {
      key: item.districtID,
      staticClass: "item",
      on: {
        "click": function click($event) {
          return _vm.clickEvent(item);
        }
      }
    }, [_c('span', {
      staticClass: "name",
      domProps: {
        "innerHTML": _vm._s(item.nameStr)
      }
    }), _vm._v(" "), _c('i', {
      staticClass: "checkbox-item",
      class: {
        'partial-checked': item.isIndeterminate,
        'no-checked': !item.isChecked,
        'all-checked': item.isChecked
      }
    })]);
  }), 0) : _vm._e(), _vm._v(" "), _vm.value && !_vm.list.length ? _c('div', {
    staticClass: "ks-hot-city"
  }, [_c('div', {
    staticClass: "ks-hot-city-title"
  }, [_vm._v("热门城市")]), _vm._v(" "), _c('div', {
    staticClass: "ks-hot-city-list"
  }, [_c('div', {
    staticClass: "city-list-container"
  }, _vm._l(_vm.hotCityList, function (item) {
    return _c('span', {
      key: item.districtID,
      staticClass: "city-txt",
      on: {
        "click": function click($event) {
          return _vm.clickEvent(item);
        }
      }
    }, [_vm._v("\n          " + _vm._s(item.simpleName) + "\n          "), _c('i', {
      staticClass: "checkbox-item",
      class: {
        'partial-checked': item.isIndeterminate,
        'no-checked': !item.isChecked,
        'all-checked': item.isChecked
      }
    })]);
  }), 0)])]) : _vm._e()], 1);
};

var __vue_staticRenderFns__ = [];
var _default = {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  props: {
    value: {
      type: Boolean
    },
    placeholder: {
      type: String,
      default: '请输入搜索内容'
    }
  },
  data: function data() {
    return {
      showKey: 'name',
      list: [],
      currentValue: '',
      hotCityList: []
    };
  },
  computed: {
    hotList: function hotList() {
      return this.$parent.hotList;
    },
    levels: function levels() {
      var includeLevel = [];
      this.levelOptions.forEach(function (levelOpt, index) {
        if (!levelOpt.disabled) {
          includeLevel.push(_levels[index]);
        }
      });
      return includeLevel;
    },
    level: function level() {
      return this.levelOptions.length;
    },
    levelOptions: function levelOptions() {
      return this.$parent.levelOptions;
    },
    checkedData: function checkedData() {
      return this.$parent.checkedData;
    },
    idMap: function idMap() {
      return this.$parent.idMap;
    }
  },
  watch: {
    hotList: {
      immediate: true,
      handler: function handler(val) {
        var hotCityList = [];

        if (val && val.length) {
          val.forEach(function (item) {
            hotCityList.push((0, _extends2.default)({}, item, {
              isChecked: false,
              isIndeterminate: false
            }));
          });
        }

        this.hotCityList = hotCityList;
      }
    },
    value: function value(val) {
      val && this.hotCityList.length && this.updatedStatus(this.hotCityList);
    }
  },
  methods: {
    updatedData: function updatedData() {
      this.updatedStatus(this.list);
      this.updatedStatus(this.hotCityList);
    },
    updatedStatus: function updatedStatus(list) {
      var _this = this;

      list && list.length && list.forEach(function (item) {
        var mapItem = _this.idMap.get(item.districtID);

        if (mapItem) {
          item.isChecked = mapItem.isChecked;
          item.isIndeterminate = mapItem.isIndeterminate;
        } else {
          item.isChecked = false;
          item.isIndeterminate = false;
        }
      });
    },
    onFocus: function onFocus() {
      var _this2 = this;

      this.$emit('closeDropdown');
      setTimeout(function () {
        _this2.$emit('input', true);
      }, 60);
    },
    handleInput: function handleInput(e) {
      var _this3 = this;

      if (!e || e.length < 2) {
        this.list = [];
        return;
      }

      var params = {
        jsonrpc: '2.0',
        keyWord: e,
        params: {
          keyWord: e
        }
      };
      (0, _ksUtils.http)(method, 10205, params).then(function (res) {
        if (res && res.districtList && res.districtList.length) {
          _this3.list = res && res.districtList.filter(function (item) {
            // if (!this.levels.includes(item.districtLevel)) return false
            // const names = item.districAllName.split('--')
            // if (names && names.length > 1 && names[names.length - 1].indexOf('-') === -1) return true
            return _this3.levels.includes(item.districtLevel);
          }).map(function (item) {
            var arr = item.districAllId.split(',');
            var ids = [];
            var names = item.districAllName.split('-').filter(function (addr, index) {
              if (addr) {
                ids.push(arr[index]);
                return true;
              }

              return false;
            });
            var parentID = ids.length === 1 ? '' : ids[ids.length - 2];

            var mapItem = _this3.idMap.get(item.districtID);

            var reg = new RegExp(e, 'gi');
            return (0, _extends2.default)({
              idStr: ids.join('/') + '/',
              parentID: parentID,
              isChecked: mapItem ? mapItem.isChecked : false,
              isIndeterminate: mapItem ? mapItem.isIndeterminate : false,
              nameStr: names && names.length && names.join(' / ').replace(reg, function (e) {
                return "<span style=\"color: #7972FE;\">" + e + "</span>";
              }),
              name: names && names.length && names.join(' / ')
            }, item, {
              districAllName: names.join('-'),
              districAllId: ids.join(',')
            });
          });
        } else {
          _this3.list = [];
        }
      }).catch(function () {
        _this3.list = [];
      });
    },
    handleCancel: function handleCancel() {
      this.list = [];
      this.currentValue = '';
      this.$emit('input', false);
    },
    clickEvent: function clickEvent(item) {
      this.$emit('searchSelect', item);
    }
  }
};
exports.default = _default;