import history from '../../utils/history';
import * as Api from './service';
import { createSubscribe } from 'applet-page-component';
import { connect } from 'herculex';
Component(createSubscribe({
  onShow() {
    this.fetchList();
  }

})(connect({
  mapStateToProps: {
    globalAddress: state => state.globalAddress
  }
})({
  props: {
    componentData: {}
  },
  data: {
    addressList: null,
    isChoose: false
  },

  didMount() {
    const {
      componentData: {
        isChoose = false,
        deliverType
      }
    } = this.props;
    const titleInfo = deliverType === 'DELIVERY_LOGISTICS' ? '选择收货人' : '选择提货人';
    my.setNavigationBar({
      title: isChoose ? titleInfo : '我的地址'
    });
    this.setData({
      isChoose,
      deliverType
    });
    this.fetchList();
  },

  methods: {
    // 将组件传出的格式转为addressList
    mapListToAddressList(list) {
      if (!list.length) {
        return [];
      }

      return list.map(v => ({
        name: v.userName,
        mobile: v.userPhone,
        region: [v.provinceName, v.cityName, v.districtName],
        regionCode: [v.provinceCode, v.cityCode, v.districtCode],
        latitude: v.latitude,
        longitude: v.longitude,
        address: v.detail,
        addressNumber: v.addressNumber,
        isDefault: v.status,
        id: v.id
      }));
    },

    async fetchList() {
      my.showLoading();

      try {
        const {
          list = []
        } = await Api.queryForAddressList();
        this.setData({
          addressList: null
        }, () => {
          this.setData({
            addressList: this.mapListToAddressList(list)
          });
        });
        my.hideLoading();
      } catch (err) {
        my.hideLoading();
        my.showToast({
          content: err.msg || '查询列表失败'
        });
      }
    },

    // 选择某个地址
    handleItemTap(e) {
      const {
        isChoose
      } = this.props.componentData;

      if (isChoose) {
        const {
          data
        } = e;
        this.commitGlobal && this.commitGlobal({
          globalAddress: [data]
        });
        my.navigateBack();
      } else {
        this.onEditItem(e);
      }
    },

    onDelItem(e) {
      my.confirm({
        title: '提示',
        content: '确认删除吗？',
        success: async res => {
          if (res.confirm) {
            console.log(e, '3eeeee');
            const {
              data = {}
            } = e || {};

            try {
              await Api.deleteAddressApi(data.id);
              this.fetchList();
            } catch (err) {
              my.showToast({
                content: '删除失败'
              });
            }
          }
        }
      });
    },

    handleAddAddress() {
      history.push({
        title: '新增地址',
        pageType: 'receive-address-detail',
        header: 'show'
      });
    },

    onEditItem(e) {
      const {
        data
      } = e;
      history.push({
        title: '编辑地址',
        pageType: 'receive-address-detail',
        header: 'show',
        componentData: { ...data,
          type: 'edit'
        }
      });
    }

  }
})));