// 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
    returnOrder.reason = this.data.curType.value // 退款原因


  },
  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      
      }
    })
  },
  deleteImage: function (event) { // 删除图片
    var tempFilePaths = this.data.tempFilePaths
    tempFilePaths.splice(event.currentTarget.dataset.index, 1)
    this.setData({
      tempFilePaths: tempFilePaths
    })
  },
  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]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { params } = this.options
    let detail = JSON.parse(params)
    // item.checked
    detail.skuVOList.forEach(item => {
      item.checked = false
    })
    this.setData({
      cartList: detail.skuVOList,
      refundAmount: detail.payAmount
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})