// pages/goods/orderList/orderList.js
import { get, post } from '../../../utils/http'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 4,
    list: [],
    total: 0,
    page: 1,
    statusTexts: {
      // 状态-1已取消，0待支付，1已支付待取货，2.已完成
      s_0: '待支付',
      s_1: '待取货',
      s_3: '已取消',
      s_2: '已取货'
    },
    pay_methon: 1,
    payNow: false,

  },

  pay_methonw() {
    this.setData({
      pay_methon: 1
    })
  },

  pay_methonc() {
    this.setData({
      pay_methon: 2
    })
  },


  onClosePay() {
    this.setData({
      payNow: false
    })
  },

  /**
   * 获取我的储值
   * api/user
   */
  getAmount() {
    get('api/user/amount', {}, (res) => {
      if (res.data) {
        this.setData({
          inserllAmount: res.data.amount
        })
      }
    })
  },


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

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAmount()
    this.getOrder(1)
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page * 10 < this.data.total) {
      this.getOrder(++this.data.page)
    }
  },

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

  /**
   * 切换tabs
   */
  onTabsChange(e) {
    let current = e.currentTarget.dataset.id
    if (current == this.data.current) {
      return
    }
    this.setData(
      {
        current,
        page: 1
      },
      () => {
        this.getOrder()
      }
    )
  },

  /**
   * 取消订单
   */
  onCancelOrder(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否取消当前订单',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          get(
            'v2/api/goods_order/cancel',
            {
              order_id: e.currentTarget.dataset.id
            },
            (res) => {
              wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 2000
              })
              that.getOrder(1)
            }
          )
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  onPay: function (e) {
    this.setData({
      order_id: e.currentTarget.dataset.id,
      payMoney: e.currentTarget.dataset.money,
      // payNow: true
    })
    wx.navigateTo({
      url: `/pages/goods/orderDetail/orderDetail?id=${ e.currentTarget.dataset.id}`,
    })
  },

  /**
   * 去支付
   */
  payNow(e) {
    let that = this
    post(
      'v2/api/order/goods/pay',
      {
        pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
        order_id: this.data.order_id
      },
      (res) => {

        that.setData({
          payNow: false
        })

        if (that.data.pay_methon == 2) {
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          })
          setTimeout(() => {
            that.getOrder(1)
          }, 1000)
          return
        }

        wx.requestPayment({
          timeStamp: res.data.pay_data.timeStamp,
          nonceStr: res.data.pay_data.nonceStr,
          package: res.data.pay_data.package,
          signType: res.data.pay_data.signType,
          paySign: res.data.pay_data.paySign,
          success(res) {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '支付成功',
                icon: 'success'
              })
              setTimeout(() => {
                that.getOrder(1)
              }, 1000)
            }
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              icon: 'error'
            })
          }
        })
      }
    )
  },

  /**
   * 获取订单列表
   * api/order
   */
  getOrder(_page) {
    let { list, current, page, statusTexts } = this.data
    get(
      '/v2/api/goods_order/list',
      {
        type: Number(current),
        page: _page || page,
        limit: 10
      },
      (res) => {
        console.log(res);
        if (_page == 1 || page == 1) {
          list = []
          this.data.page = 1
        }
        res.data.list.map((item) => {
          // 状态，-3已退款，-2退款中，-1已取消，0待支付，1已支付，待取货，2已取货
          if(item.status==-1){
            item.status=3
          }
          item['status_text'] = statusTexts['s_' + item.status]
          let sum = 0
          item.good.map(item1=>{
             sum += item1.num
          })
          // 加上加购产品数量
          sum = item.plus.length+sum
          item.sum = sum
        })
        list.push(...res.data.list)
        this.setData({ list, total: res.data.total })
      }
    )
  },

  /**
   * 用户允许消息通知
   * /api/message/agree
   */
  messageAgree(ids, order_id) {
    post(
      'api/message/agree',
      {
        order_id,
        ids
      },
      () => {}
    )
  }
})
