import { $RemoveItem } from '../utils/commonUtils';
import province from '../data/province.json';
import city from '../data/city.json';
import county from '../data/county.json';
import create from '../utils/create';
export default create({
  render: function render() {
    var _vm = this;

    var _h = _vm.$createElement;

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

    return _vm.isShow ? _c('div', {
      class: _vm.b(),
      on: {
        "click": _vm.hide
      }
    }, [_c('div', {
      class: _vm.b('content'),
      on: {
        "click": function click($event) {
          $event.stopPropagation();
        }
      }
    }, [_c('p', {
      class: _vm.b('content-title')
    }, [_vm._v("\n      请选择\n      "), _c('icon', {
      class: _vm.b('content-close'),
      attrs: {
        "name": "close",
        "color": "#707070",
        "size": "30"
      },
      on: {
        "click": _vm.hide
      }
    })], 1), _c('ul', {
      class: _vm.b('content-tab', {
        'landscape': _vm.landscape
      })
    }, _vm._l(_vm.tabList, function (item, index) {
      return _c('li', {
        key: index,
        class: {
          'selected': _vm.tabIndex === index
        },
        on: {
          "click": function click($event) {
            return _vm.selectTab(index);
          }
        }
      }, [_vm._v("\n        " + _vm._s(item.name) + "\n      ")]);
    }), 0), _c('ul', {
      class: _vm.b('content-data')
    }, _vm._l(_vm.showData, function (item, index) {
      return _c('li', {
        key: index,
        class: index % 2 === 0 ? 'two' : '',
        on: {
          "click": function click($event) {
            $event.stopPropagation();
            return _vm.select(item);
          }
        }
      }, [_c('span', {
        class: {
          'selected': item.name === _vm.tabList[_vm.tabIndex].name
        }
      }, [_vm._v("\n          " + _vm._s(item.name) + "\n        ")])]);
    }), 0)])]) : _vm._e();
  },
  name: 'address-picker',
  props: {
    value: Boolean,
    provinceName: {
      type: String,
      default: ''
    },
    landscape: {
      type: Boolean,
      default: false
    },
    level: {
      type: Number,
      default: 3
    }
  },
  watch: {
    address: function address() {
      this.$emit('changeAddress', this.address, this.addressList);

      if (this.level === this.addressList.length) {
        this.$emit('getAddress', this.address, this.addressList);
      } else {
        if (this.isEnd) {
          this.isEnd = false;
          this.$emit('getAddress', this.address, this.addressList);
        }
      }
    },
    value: function value() {
      this.isShow = this.value;
    },
    isShow: function isShow() {
      this.$emit('input', this.isShow);
    }
  },
  data: function data() {
    return {
      showData: [],
      tabList: [{
        name: '请选择省份'
      }],
      address: '',
      addressList: [],
      tabIndex: 0,
      isShow: false,
      isEnd: false
    };
  },
  methods: {
    //方法
    selectTab: function selectTab(index) {
      switch (index) {
        case 0:
          if (this.provinceName === '') {
            this.tabIndex = index;
            this.get(0);
          }

          break;

        case 1:
          this.tabIndex = index;
          this.get(1, this.tabList[index - 1].id);
          break;

        case 2:
          this.tabIndex = index;
          this.get(2, this.tabList[index - 1].id);
          break;

        default:
          break;
      }
    },
    select: function select(item) {
      switch (this.tabIndex) {
        case 0:
          this.tabList[0].name = item.name;
          this.tabList[0].id = item.id;

          if (this.level === 1) {
            this.hide();
          } else {
            this.get(1, item.id, true);
          }

          break;

        case 1:
          this.tabList[1].name = item.name;
          this.tabList[1].id = item.id;

          if (this.level === 2) {
            this.hide();
          } else {
            this.get(2, item.id, true);
          }

          break;

        case 2:
          this.tabList[2].name = item.name;
          this.tabList[2].id = item.id;
          this.hide();
          break;

        default:
          break;
      }

      var address = '';
      var addressList = [];
      this.tabList.forEach(function (val, index) {
        if (val.name.indexOf('请选择') === -1) {
          address += val.name;
          addressList[index] = val.name;
        }
      });
      this.address = address;
      this.addressList = addressList;
    },
    hide: function hide() {
      this.isShow = false;
    },
    get: function get(level, id, change) {
      var data = [];
      var self = this;

      switch (level) {
        case 0:
          data = province.list;
          break;

        case 1:
          data = city[id];
          break;

        case 2:
          data = county[id];
          break;
      }

      if (data && data.length > 0) {
        var showData = [];

        if (change) {
          this.tabList[this.tabIndex + 1] = {
            name: '请选择'
          };
          this.tabIndex = this.tabIndex + 1;
          this.tabList = $RemoveItem(this.tabList, this.tabIndex, 2);
        }

        data.forEach(function (val) {
          var obj = {};

          if (val.fullname.length > 5) {
            if (val.name) {
              obj.name = val.name;
            } else {
              obj.name = val.fullname;
            }
          } else {
            obj.name = val.fullname;
          }

          obj.id = val.id;
          showData.push(obj);
        });
        self.showData = showData.sort(function compareFunction(param1, param2) {
          return param1.name.localeCompare(param2.name, "zh");
        });
      } else {
        self.hide();
        var address = '';
        var addressList = [];
        self.tabList.forEach(function (val, index) {
          if (val.name.indexOf('请选择') === -1) {
            address += val.name;
            addressList[index] = val.name;
          }
        });
        self.address = address;
        self.addressList = addressList;
        self.isEnd = true;
      }
    },
    init: function init() {
      var _this = this;

      if (this.provinceName) {
        province.list.forEach(function (val) {
          if (val.fullname === _this.provinceName) {
            var obj = {};

            if (val.fullname.length > 5) {
              if (val.name) {
                obj.name = val.name;
              } else {
                obj.name = val.fullname;
              }
            } else {
              obj.name = val.fullname;
            }

            obj.id = val.id;

            _this.select(obj);
          }
        });
      } else {
        this.get(0);
      }
    }
  },
  created: function created() {//渲染前执行
  },
  mounted: function mounted() {
    //渲染后执行
    var self = this;
    self.isShow = self.value;
    self.init();
  },
  computed: {}
});