var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refundMoney: '',
    orderGoods: [],
    orderId: '',
    merchOrderSn: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.orderId,
      merchOrderSn: options.merchOrderSn,
      refundMoney: options.refundMoney
    });
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    // 页面显示
    if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
      if (wx.getStorageSync('storeId')) {
        util.request(api.ChooseStoreId, {
          storeId: wx.getStorageSync('storeId'),
          merchSn: wx.getStorageSync('merchSn')
        }, 'POST').then(function (res) {
          if (res.errno === 0) {
            wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
            wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
          }
        });
        util.request(api.OrderDetail, {
          orderId: that.data.orderId
        }).then(function (res) {
          if (res.errno === 0) {
            that.setData({
              orderGoods: res.data.orderGoods
            });
          }
        });
      }
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  saveApplyRefund: function (e) {
    if (!e.detail.value.refundReason) {
      util.showErrorToast('退款原因不能为空');
      return false;
    }

    let that = this; console.log(that.data.merchOrderSn);
    util.request(api.saveApplyRefund, {
      refundReason: e.detail.value.refundReason,
      orderId: that.data.orderId,
      merchOrderSn: that.data.merchOrderSn
    }, 'POST').then(function (res) {
      if (res.errno === 0) {
        //成功提示
        wx.showModal({
          title: '',
          content: res.errmsg,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {

              wx.navigateBack()
            }
          }
        });
      } else {
        wx.showModal({
          title: '',
          content: res.errmsg,
          showCancel: false
        });
      }
    });
  },
})