import chooseCity, { getCityList } from '../../utils/chooseCity';
import { getNearStore } from '../../services/store';
import { connect } from 'herculex';
const app = getApp();
Component(connect({
  mapStateToProps: {}
})({
  props: {
    tabBar: {},
    shopType: ''
  },
  data: {
    stores: [],
    isRenderAdcFlatList: false
  },
  methods: {
    setPopup(ref) {
      this.$popup = ref;
    },

    setFlatList(flatList) {
      this.$flatList = flatList;
    },

    handleClose() {
      this.show(false);
    },

    /**
     * 重写show方法，处理加载数据.
     * 选择门店依赖当前位置和店铺详情.
     *
     * @param show
     */
    show(show) {
      // popup点击mask隐藏popup不会触发close事件，无法设置isRenderAdcFlatList为false
      // 只能在显示前先手动重置数据
      this.setData({
        isRenderAdcFlatList: false,
        stores: []
      }, () => {
        this.setData({
          isRenderAdcFlatList: show === undefined || show
        }, () => {
          this.$popup.show(show);
        });
      });
    },

    handleSelectStore(e) {
      const {
        id
      } = e.target.dataset;

      if (!id) {
        return;
      }

      const store = this.data.stores.find(item => item.storeId === id);

      if (!store) {
        return;
      } // 重置城市列表，因为依赖storeId


      this.$cityList = undefined;
      this.show(false);
      this.props.onSelectStore(store);
    },

    async handleSwitchCity() {
      const {
        store: {
          detail
        }
      } = app.globalData; // 缓存城市列表

      if (!this.$cityList) {
        this.$cityList = await getCityList(detail.storeId);
      }

      const cityInfo = await chooseCity(detail.storeId, this.$cityList);
      app.globalData.regionData.selectedCity = {
        city: cityInfo.city,
        cityCode: cityInfo.adCode
      };
      this.loadData();
    },

    loadData() {
      this.setData({
        stores: []
      });
      this.$flatList.refresh();
    },

    async onFetchNearStores(page, limit, type) {
      const {
        shopType
      } = this.props;
      const {
        regionData,
        store: {
          detail
        }
      } = app.globalData;
      const {
        selectedCity = {},
        selectedLocation = {}
      } = regionData;
      const result = await getNearStore({
        pageIndex: page,
        pageSize: limit,
        storeId: detail.storeId,
        pid: detail.pid,
        coordinate: regionData.latitude && regionData.longitude ? {
          latitude: regionData.latitude,
          longitude: regionData.longitude
        } : undefined,
        cityCode: selectedCity.cityCode || selectedLocation.cityCode || regionData.cityAdcode,
        shopType
      }).then(({
        data,
        total
      }) => ({
        list: data,
        total
      }));

      if (type === 'pullDown') {
        // 下拉刷新
        return result;
      } // 普通加载


      const stores = [...this.data.stores, ...result.list];
      this.setData({
        stores
      });
      return result;
    }

  },

  didUnmount() {
    app.globalData.regionData.selectedCity = undefined;
  }

}));