import { doNavigateWithUser } from '../../../utils/helper';
import { headerStorage, postUrl } from '../../../utils/util';
import { apiArr } from '../../../api/user';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '', // 订单id
    addAress: {}, // 收货地址
    item: {}, // 商品详情
    shopInfo: {},
    isOnShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('生命周期函数', options);
    this.setData({
      orderId: options.orderId,
    });
    this.init(options.orderId);
  },
  init (orderId) {
    wx.showLoading({
      title: '加载中',
    })
    postUrl(apiArr.getOne, {
      order_id: Number(orderId)
    },
     res => {
       wx.hideLoading();
      console.log('订单详情res', res);
      const newAddAress = {
        real_name: res.name,
        mobile: res.tel,
        optionAddr: `${res.region}${res.addr}`,
      }
      this.setData({
        addAress: newAddAress,
        item: res,
        shopInfo: res.shop_name
      })
    })
  },

  headerPositionClick() {
    console.log('跳转管理地址页');
    this.setData({
      isOnShow: false,
    });
    doNavigateWithUser({ url: `/packages/user/addressAdministration/index`});
  },

  // 去付款
  handleGoPayClick() {
    const {addAress, orderId, item} = this.data;
    console.log('addAress', addAress);

    if(addAress.real_name === '' || addAress.mobile == '' || addAress.optionAddr === '') {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none',
      })
      return;
    }
    if (item.status !== 1) {
      wx.showToast({
        title: '当前订单状态无法进行支付',
        icon: 'none',
      })
      return;
    }
    wx.setStorageSync('newAddress', addAress);
    // 跳转到去付款页后再回上一页禁止执行onshow
    this.setData({
      isOnShow: false,
    });
    doNavigateWithUser({ url: `/packages/user/goPay/index?orderId=${orderId}`});
  },

  // 取消订单
  handleCancelOrderClick() {
    const { orderId } = this.data;
    console.log('取消订单');
    wx.showLoading({
      title: '加载中',
    })
    postUrl(apiArr.revoke, {
      order_id: Number(orderId)
    },
     res => {
       wx.hideLoading();
        console.log('取消订单res', res);
        if (res.error) {
          wx.showToast({
            title: '取消订单失败',
            icon: 'none',
          })
          return;
        }
        wx.showToast({
          title: res.message,
          icon: 'none',
        })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    const { isOnShow } = this.data;
    const isNewAddress = wx.getStorageSync('isNewAddress');

    if (isOnShow) {
      headerStorage(_this, 'newAddress', 'addAress')
    }
    // 跳转地址管理页后 选择新地址 则展示新地址
    if (isNewAddress) {
      headerStorage(_this, 'newAddress', 'addAress');
      wx.removeStorageSync('isNewAddress')
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})