import { connect } from 'herculex';
import { prefixPagePath } from '../../utils';

Component(connect({
  mapStateToProps: {
    cityTabs: state => state.cityTabs,
    currentCityIndex: (state) => {
      const { currentCityIndex } = state;
      return currentCityIndex;
    },
    currentAreaIndex: (state) => {
      const { currentAreaIndex } = state;
      return currentAreaIndex;
    },
    currentCity: (state) => {
      const { currentCityIndex, cityTabs } = state;
      return cityTabs[currentCityIndex];
    },
    noCache: state => state.$global.noCache,
  },
})({
  didMount() {
  },
  methods: {
    /**
     * 改变城市选择项
     */
    onChangeCity(e) {
      const { dataset } = e.target;
      const { index } = dataset;
      this.dispatch('updateStoreData', {
        currentCityIndex: index,
      });
    },
    /** 
     * 区域选择
     */
    async onChangeArea(e) {
      const { dataset } = e.target;
      const { index } = dataset;
      await this.dispatch('updateAreaData', {
        data: dataset.item,
        currentAreaIndex: index,
      });
      if (!this.data.noCache) {
        return my.navigateBack();
      }
      my.reLaunch({ url: `${prefixPagePath}/home/index?noCache=true` });
    },
  },
}));
