// pages/logistics/logistics.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    logisData:[],
    orderId:'',
    orderCode:'',
    orderLogisticsNo: '',
    params: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { params } = this.options
    let detail = JSON.parse(params)

    // supplierCode code
    if(detail.id){
      this.setData({
        params,
        orderId: detail.id,
        orderCode: detail.logistic.supplierCode,
        orderLogisticsNo: detail.logistic.code
      },()=>{
        const {orderCode, orderLogisticsNo} = this.data
        this.getLogisticsInfo(orderCode, orderLogisticsNo)
      })
    }
  },
  handelReturnGood() {
    const {params} = this.data
    // 退款申请
    wxService.router(`/pages/refund/refund`).search({params})
  },
  // 确认收货
  handelConfirmReceipt() {
    const { orderId } = this.data
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post(`/sale/trade/buyer/confirm/${orderId}`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          wx.showToast({
            title: `您已确认收货!`,
            icon: 'none'
          })
          setTimeout(() => {
            this.initOrderList(this.data.pageNo, this.data.pageSize, this.data.trade)
          }, 200)
        } else {
          wx.showToast({
            title: `确认收货失败!`,
            icon: 'none'
          })
        }
      }
    })
  },
  // 获取物流信息
  getLogisticsInfo(code,num){
    wxService.get(`/sale/logistics/getLogisticsDetail?companyCode=${code}&logisticsNo=${num}`).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        console.log(data.list)
        for (var i in data.list){
          if (data.list[i].datetime){
            data.list[i].date = data.list[i].datetime.substring(0,10)
            data.list[i].time = data.list[i].datetime.substring(0, 10)
          }
        }
        this.setData({
          logisData: data
        })
        
        
      }

    }).finally(() => {
    })
  },

  copy(e) {
    wx.setClipboardData({
      //准备复制的数据
      data: e.currentTarget.dataset.content,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        });
      }
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})