/* eslint-disable no-param-reassign */
import {
  fetchDeliveryAddressList
} from '~/services/address/fetchAddress';
import Toast from 'tdesign-miniprogram/toast/index';
import {
  resolveAddress,
  rejectAddress
} from './util';
import {
  deletAddress
} from '~/services/address/deletAddress'


Page({
  data: {
    addressList: [],
    deleteID: '',
    showDeleteConfirm: true,
    isOrderSure: false,
  },

  /** 选择模式 */
  selectMode: false,
  /** 是否已经选择地址，不置为true的话页面离开时会触发取消选择行为 */
  hasSelect: false,

  onLoad(query) {
    const {
      selectMode = '', isOrderSure = '', id = ''
    } = query;
    this.setData({
      isOrderSure: !!isOrderSure,
      id,
    });
    this.selectMode = !!selectMode;
    this.init();
  },
  init() {
    this.getAddressList();
  },
  onUnload() {
    if (this.selectMode && !this.hasSelect) {
      rejectAddress(this.data.addressList);
    }
  },

  /** 获取收货地址列表 */
  getAddressList() {
    const {
      id
    } = this.data;
    fetchDeliveryAddressList().then((addressList) => {
      addressList.forEach((address) => {
        if (address.customerAddressGuid === id) {
          address.checked = true;
        }
      });
      this.setData({
        addressList
      });
    });
  },

  /** 获取微信地址 */
  getWXAddressHandle() {
    wx.chooseAddress({
      success: (res) => {
        if (res.errMsg.indexOf('ok') === -1) {
          Toast({
            context: this,
            selector: '#t-toast',
            message: res.errMsg,
            icon: '',
            duration: 1000,
          });
          return;
        }
        Toast({
          context: this,
          selector: '#t-toast',
          message: '添加成功',
          icon: '',
          duration: 1000,
        });
        const {
          length: len
        } = this.data.addressList;
        this.setData({
          [`addressList[${len}]`]: {
            name: res.userName,
            phoneNumber: res.telNumber,
            address: `${res.provinceName}${res.cityName}${res.countryName}${res.detailInfo}`,
            isDefault: 0,
            tag: '微信地址',
            id: len,
          },
        });
      },
    });
  },

  /** 删除地址操作 */
  confirmDeleteHandle({
    detail
  }) {
    const {
      customerAddressId
    } = detail || {};

    deletAddress(customerAddressId).then((res) => {
      if (res.code === 200) {
        this.init()
        this.setData({
          deleteID: customerAddressId,
          showDeleteConfirm: true
        });
        Toast({
          context: this,
          selector: '#t-toast',
          message: '地址删除成功',
          theme: 'success',
          duration: 1000,
        });
      }
    })
  },

  /** 跳转编辑 */
  editAddressHandle({
    detail
  }) {
    const {
      customerAddressId
    } = detail || {};
    wx.navigateTo({
      url: `/pages/usercenter/address/edit/index?id=${customerAddressId}`
    });
  },
  selectHandle({
    detail
  }) {
    if (this.selectMode) {
      this.hasSelect = true;
      resolveAddress(this.data.addressList, detail);
      wx.navigateBack({
        delta: 1
      });
    } else {
      this.editAddressHandle({
        detail
      });
    }
  },

  /** 跳转添加 */
  createHandle() {
    wx.navigateTo({
      url: '/pages/usercenter/address/edit/index'
    });
  },

});