// 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
    },
    tempFilePaths: [], // 照片
    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:[]
  },
  submitRefund() {
    if (this.data.curType.value == 0) {
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none'
      })
      return false
    }
    var selectGoods = this.data.cartList.filter(item => item.checked)

    if (selectGoods.length == 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)
        }
      }
    })

  },
  chooseImage: function (event) { // 选择图片
    // wx.chooseImage({
    //   count: 1,
    //   success: res => {
    //     var tempFilePaths = res.tempFilePaths
    //     wx.uploadFile({
    //       url: app.globalData.ctx + 'returnOrder/uploadReturnGoodsImg',
    //       filePath: tempFilePaths[0],
    //       header: { token: wx.getStorageSync('token') },
    //       name: 'file',
    //       success: res_ => {
    //         this.setData({
    //           imageDomain: JSON.parse(res_.data).data.imageDomain,
    //           tempFilePaths: this.data.tempFilePaths.concat(JSON.parse(res_.data).data.imageDomain + JSON.parse(res_.data).data.imgPath)
    //         })
    //       }
    //     })
    //   }
    // })
    let that = this;
    let tempFilePaths = this.data.tempFilePaths;
    wx.chooseImage({
      count: 5 - tempFilePaths.length, // 最多可以选择的图片5张，
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        tempFilePaths = tempFilePaths.concat(imgsrc);
        that.setData({
          tempFilePaths: tempFilePaths
        });
        console.log(that.data.tempFilePaths)
        // that.moreImgUpload({
        //   url: 'https://........',//这里是你图片上传的接口
        //   path: that.data.pics//这里是选取的图片的地址数组        
        // });
      },
      fail: function () {
        // fail      
      },
      complete: function () {
        // complete      
      }
    })
  },
  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) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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({
      cartList: detail.skuVOList,
      refundAmount: detail.payAmount,
      'refundCreateDto.tradeId': detail.id
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})