// pages/afterSales/afterSales.js
var http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    current: 1,
    pages: 0,
    startTime: '',  //开始时间
    endTime: '',  //结束时间
    isAll:false
    //orderItems: '',  //订单项
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.myRefundOrder();  //退款订单列表
  },


  /**
   * 我的退款订单列表
   */
  myRefundOrder: function (e) {
    wx:wx.showLoading();
    var params = {
      url: "/p/orderRefund/list",
      method: "GET",
      data: {
        current: this.data.current,
        size: 20,
        startTime: this.data.startTime,
        endTime: this.data.endTime
      },
      callBack: (res) => {
        let img=''
        res.records.forEach(item=>{
          item.orderItems.map(e=>{
            img=e.pic.split(',')
            e.pic=img[0]
          })
        })
        var list = [];
        if (res.current == 1) {
          list = res.records;
        } else {
          list = this.data.list;
          Array.prototype.push.apply(list, res.records);
        }
        if (list.length >= res.total) {
          this.setData({
            isAll: true
          })
        }
        this.setData({
          list: list,
          pages: res.pages,
          current: res.current,
        });
        wx: wx.hideLoading();
      }
    };
    http.request(params);
  },

    /**
   * 撤销退货退款申请
   */
  cancelApplication: function (e) {
    let refundSn = e.currentTarget.dataset.refundsn
    wx.showModal({
      title: '撤销',
      content: '确定要撤销本次申请？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({})
          var params = {
            url: "/p/orderRefund/cancel",
            method: "GET",
            data: {refundSn} , //退款订单编号
            callBack: function (res) {
              wx.hideLoading();
              wx.navigateTo({
                url: '/pages/afterSales/afterSales',
              })
            }
          };
          http.request(params);
        } else {
          console.log('取消')
        }
      }
    })

  },

    /**
   * 再次申请
   */
  applyAgain: function (e) {
    let refundId = e.currentTarget.dataset.refundid
    let ordernum = e.currentTarget.dataset.ordernum
    
    var item = {}
      item.orderNumber = this.data.orderNumber;
      item.actualTotal = this.data.actualTotal;
      item.transfee = this.data.transfee;
      item.status = this.data.status; //订单状态
      item.orderItemDtos = this.data.orderItemDtos;
      item.orderScore = this.data.orderScore,  // 整单积分
      item.userAddrDto = this.data.userAddrDto,  // 用户信息
      //拿到存储在本地的订单项数据
      wx.setStorageSync("refundItem", item);

    wx.redirectTo({ //通过wx.redirectTo实现跳转（关闭当前页面，跳转到应用内某个页面）
      url: `/pages/applyRefund/applyRefund?refundId=${refundId}&orderNumber=${ordernum}` ,
    })

  },
  


  /**
   * 点击查看详情跳转页面
   */
  toRefundDetails: function(e) {
    var refundSn = e.currentTarget.dataset.refundsn;
    console.log(refundSn);
    wx.navigateTo({
      url: '/pages/DetailsOfRefund/DetailsOfRefund?refundSn=' + refundSn
    })
  },



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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

  // 触底加载下一页
  getNextPage() {
    console.log('getNextPage',this.data.current);
    if (this.data.pages > this.data.current) {
      this.setData({
        current: this.data.current + 1
      })
      this.myRefundOrder()
    } else {
      this.setData({
        isAll: true
      })
    }
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getNextPage()
  },

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

  }
})