/*
 * 手太欠
 * 企获客商城
 */

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderId     : '',     //商品id
        orderData   : '',     //退货商品
        refundTitle : [],     //退货理由
        refundType  : '',     //退货类型
        refundIndex : 0,      //退货理由index
        title       : '',     //退款原因
        typeIndex   : 0,      //退货类型index
        imgs        : [],     //图片页面显示
        paths       : [],     //图片上传服务器
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            orderId: options.orderId
        })
        wx.$api.order.goosRefund(options.orderId).then(res => {
            console.log(res)
            this.setData({
                orderData   :  res.order,
                refundTitle :  res.refund_title,
                refundType  :  res.refund_type
            })
        })

    },

     /**
     *选择退款原因
     */
    refundChange(e) {
        this.setData({
            refundIndex : e.detail.value
        })
    },

    /**
     * 上传图片
     */

    formUploadFile(){
        let count = 9 - this.data.imgs.length
        wx.chooseImage({
            count   : count,
            success : res=>{
                // 上传图片
                if (res.tempFilePaths){
                    let pathArr = res.tempFilePaths
                    wx.showLoading({
                        title: '上传中',
                    })
                    for (let i = 0; i < pathArr.length; i++){
                        wx.$api.file.uploadImg(pathArr[i], {}).then(res=>{
                            let imgArr  = this.data.imgs,
                                pathArr = this.data.paths
                            imgArr.push(res.showpath)
                            pathArr.push(res.path)
                            this.setData({
                                imgs : imgArr,
                                paths: pathArr
                            })
                        })

                        if (i == pathArr.length - 1) {
                            wx.hideLoading()
                        }
                    }
                }else{
                    wx.showToast({
                        title: '上传图片失败',
                        icon : 'none'
                    })
                }
            }
        })
    },

    /**
     * 删除图片
     */
    removeImg(e){
        let imgArr  = this.data.imgs,
            pathArr = this.data.paths,
            index   = e.currentTarget.dataset.index
        
            imgArr.splice(index, 1)
            pathArr.splice(index, 1)

        this.setData({
            imgs  : imgArr,
            paths : pathArr
        })
    },

    /**
     * 申请退货
     */
    refundSign(e) {
        let refundTitle   = this.data.refundTitle,
            refundIndex   = this.data.refundIndex,
            remark        = e.detail.value.remark,
            paths         = this.data.paths
        
            console.log(paths)

        wx.$api.order.formRefund(this.data.orderId,{
            title	: refundTitle[refundIndex].title,
            remark	: remark,
            type	: 1,
            pictures: paths
        }).then(res => {
            wx.redirectTo({
              url: '/pages/mall/mall_order/mall_order',
            })
        })
    },
})