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

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    imageDomain: '',
    refundCreateDto: {
      reason: "", // 原因
      skuList: [],
      tradeId: 0,
      proofImgUrls: [] //凭证
    },
    refundMark: '',
    cartList: [],
    curType: { // 当前选择原因
      value: 0,
      name: '请选择'
    },
    resonType: [{ // 所有退货原因
      value: 0,
      name: '请选择'
    }, {
      value: 1,
      name: '质量问题'
    }, {
      value: 2,
      name: '大小尺码问题'
    }, {
      value: 3,
      name: '不想要了'
    }, {
      value: 4,
      name: '卖家发错漏发'
    }, {
      value: 5,
      name: '拍错商品'
    }, {
      value: 6,
      name: '商品实物与描述不同'
    }],
    refundAmount: 0,
    checkedColor: 'rgb(203, 60, 60)',
    pics: [],
    id: ''
  },
  submitRefund() {
    var selectGoods = this.data.cartList.filter(item => item.checked)

    if (selectGoods.length == 0) {
      wx.showToast({
        title: '请选择退款商品',
        icon: 'none'
      })
      return false
    }

    if (this.data.curType.value == 0) {
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none'
      })
      return false
    }

    console.log('selectGoods', selectGoods)

    var refundCreateDto = this.data.refundCreateDto
    // refundCreateDto.reason = this.data.curType.value // 退款原因
    refundCreateDto.reason = this.data.curType.name // 退款原因

    refundCreateDto.skuList = selectGoods.map(function (item) {
      return {
        amount: item.amount,
        count: item.count,
        skuId: item.skuId
      }
    })


    console.log('refundCreateDto', refundCreateDto)

    this.handelRequestRefund(refundCreateDto)

  },
  handelRequestRefund(params) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post('/sale/refund/buyer/create', params).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          wx.showToast({
            title: '申请退款成功',
            icon: 'none'
          });
          // 订单列表
          setTimeout(() => {
            // wxService.router(`/pages/order/order`).search({params})
            wxService.router(`/pages/order/order`)
          }, 2000)
        }
      }
    })

  },
  getReasonList() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const refundReason = {}
    const { resonType } = this.data
    wxService.post('/sale/refund/reason/buyer/sortAll', refundReason).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let newReasonList = []
          data.forEach((item, index) => {
            let params = {
              value: '',
              name: ''
            }
            params.name = item.reason
            params.value = index
            newReasonList.push(params)
          })
          this.setData({
            resonType: newReasonList
          })
        }
      }
    })
  },
  // 上传图片
  _uploadImg(e) {
    if (e.detail) {
      this.data.refundCreateDto.proofImgUrls.push(e.detail)
      this.setData({
        'refundCreateDto.proofImgUrls': this.data.refundCreateDto.proofImgUrls
      })
    }
  },

  checkPro(e) {
    var index = e.currentTarget.dataset.index
    const { cartList } = this.data
    console.log('index', index, cartList[index].checked)
    if (cartList[index].checked) {
      cartList[index].checked = false
    } else {
      cartList[index].checked = true
    }
    this.setData({
      cartList
    })
  },
  changeRemark(event) { // 备注
    this.setData({
      refundMark: event.detail.value
    })
  },
  selectType: function (event) { // 选择退货类型
    this.setData({
      curType: this.data.resonType[event.detail.value]
    })
  },
  minusNum: function (e) { // 减少数量
    var cartList = this.data.cartList
    var index = e.currentTarget.dataset.index
    let proNum = cartList[index].count
    console.log('减少数量', proNum)

    if (proNum > 1) {
      proNum--
    } else {
      wx.showToast({
        title: '数量不能为0',
        icon: 'none'
      })
      return false
    }
    cartList[index].count = proNum
    this.setData({
      cartList: cartList
    }, () => {
      // this.getReturnPrice()
      // this.editOk(cartList[e]) //调用完成
    })
  },
  addNum: function (e) { // 增加数量
    var index = e.currentTarget.dataset.index
    var cartList = this.data.cartList
    var proNum = cartList[index].count
    console.log('增加数量', proNum)

    proNum++

    cartList[index].count = proNum
    this.setData({
      cartList: cartList
    }, () => {

    })

  },
  inputNum: function (e) { // 输入数量
    var newNum = e.detail.value
    var order = this.data.cartList

    var index = e.currentTarget.dataset.index

    var refundNum = order[index].count
    console.log('inputNum', index, newNum, refundNum)
    if (newNum === '' || newNum === '0') {
      newNum = 1
    } else {
      newNum = parseInt(newNum)
      if (newNum > refundNum) {
        newNum = refundNum
      }
    }
    order[index].count = refundNum
    this.setData({
      order: order
    }, () => {
      // this.getReturnPrice()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { params } = this.options
    let detail = JSON.parse(params)
console.log("detail", detail)
    // item.checked
    detail.skuVOList.forEach(item => {
      item.checked = false
    })
    this.setData({
      id: detail.id,
      cartList: detail.skuVOList,
      refundAmount: detail.payAmount,
      'refundCreateDto.tradeId': detail.id
    }, () => {
      // 退款原因列表 /refund/reason/buyer/sortAll
      this.getReasonList()
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})