var t = getApp(), e = t.globalData.setting,os=e,oo=t.globalData, a = t.request, s = require("../../../../utils/common.js"), i = require("../../../../utils/selectFiles.js");
var ut = require("../../../../utils/util.js");

Page({
    data: {
        url: e.url,
        resourceUrl: e.imghost,
        defaultPhoto: e.resourceUrl + "/static/images/camera.png",
        filePaths: [],
        uploadPath: [],
        order: null,
        recId: 0,
        config: null,
        maxWord: 0,
        isLongPress: !1,
        checkBtns: ["退货退款"],
        typeBtnIndex: 0,
        applyNum: 1,
        isReceive: !1,
        causeCont: "注意保持商品的完好,建议您先与卖家沟通",
        causeList: [ "订单不能按预计时间送达", "操作有误（商品、地址等选错）", "重复下单/误下单", "其他渠道价格更低", "该商品降价了", "不想买了" ],
        checkCauseIndex: -1,
        popState: !0,
        description: "",
        refund_type:0,
		isloading:0,
    },
    onLoad: function(e) {
        var a = this;
       
        getApp().getConfig2(function (em) {
           a.data.refund_type = em.refund_type;
        });

        this.setData({
            recId: e.rec_id
        }), this.requestReturnGoods(e), t.getConfig(function(t) {
            a.setData({
                "config.address": t.store_address,
                "config.phone": t.store_tel,
            });
        });
      
    },
    requestReturnGoods: function(t) {
        var e = this;
		wx.showLoading();
        a.get("/api/weshop/ordergoods/list", {
            failRollback: !0,
            data: {
               goods_id: t.rec_id,
               order_id: t.order_id,
               store_id:os.stoid
            },
            success: function(t) {
                console.log('requestReturnGoods');
                console.log(t);
                e.setData({
                    order: t.data.data.pageData[0],
                    applyNum: t.data.data.pageData[0].goods_num
                });
				wx.hideLoading();
            }
        });
    },
    InputDescription: function(t) {
        this.setData({
            maxWord: t.detail.value.length
        }), this.data.description = t.detail.value;
    },
    selectGoodsStatus: function(t) {
        this.setData({
            isReceive: t.currentTarget.dataset.status
        });
    },
    selectServiceType: function(t) {
        this.setData({
            typeBtnIndex: t.currentTarget.dataset.i
        });
    },
    setNum: function(t) {
        this.checkApplyNum(Number(t.detail.value));
    },
    subNum: function() {
        this.checkApplyNum(this.data.applyNum - 1);
    },
    addNum: function() {
        this.checkApplyNum(this.data.applyNum + 1);
    },
    checkApplyNum: function(t) {
        isNaN(t) ? t = this.data.order.goods_num : t < 1 ? t = 1 : t > this.data.order.goods_num && (t = this.data.order.goods_num), 
        this.setData({
            applyNum: t
        });
    },
    openPop: function() {
        this.setData({
            popState: !1
        });
    },
    closePop: function() {
        this.setData({
            popState: !0
        });
    },
    selectCause: function(t) {
        this.setData({
            checkCauseIndex: t.currentTarget.dataset.i,
            causeCont: this.data.causeList[t.currentTarget.dataset.i]
        }), this.closePop();
    },
    selectPhotos: function(t) {
        if (this.isLongPress) this.isLongPress = !1; else {
            var e = this;
            i.selectPhotos(this.data.filePaths, t.currentTarget.dataset.idx, function(t) {
                e.setData({
                    filePaths: t
                });
            });
        }
    },
    removePhoto: function(t) {
        var e = this;
        this.isLongPress = !0, i.removePhoto(this.data.filePaths, t.currentTarget.dataset.idx, function(t) {
            e.setData({
                filePaths: t
            });
        });
    },

    
    /*-------------提交退款申请--------------*/
    submiApply: function() {
		
		if(!this.data.order) return false;
		
        var t = this,th=t;
        this.checkSubmit(function() {
			
			if(th.data.isloading) return false;
			th.data.isloading=1;		
				
            t.uploadPhotos(function() {
              a.post("/api/weshop/order/returngoods/saveReturnGoods", {
                   /*---
                    data: {
                        rec_id: t.data.recId,
                        type: t.data.typeBtnIndex,
                        goods_num: t.data.applyNum,
                        reason: t.data.causeCont,
                        describe: t.data.description,
                        goods_id: t.data.order.goods_id,
                        order_id: t.data.order.order_id,
                        order_sn: t.data.order.order_sn,
                        spec_key: t.data.order.spec_key,
                        is_receive: Number(t.data.isReceive),
                        imgs: t.data.uploadPath.join(",")
                    },---*/
                    data: {
                      order_id: th.data.order.order_id,
                      order_sn: th.data.order.order_sn,
                      store_id: os.stoid,
                      user_id: oo.user_id,
                      back_money: th.data.order.goods_price * th.data.order.goods_num,
                      reason: th.data.causeList[th.data.checkCauseIndex],
                      goods_id: th.data.order.goods_id,
                      addtime: ut.gettimestamp(),
                      type:2,
                      refund_type: th.data.refund_type,
					  remark:th.data.description
                    },
					
                    success: function(t) {
                        wx.showToast({
                            title: "提交成功",
                            mask: !0,
                            duration: 1e3,
                            complete: function() {
                                setTimeout(function() {
                                    wx.navigateBack();
                                }, 1e3);
                            }
                        });
                    }
                });
            });
        });
    },

    /*----上传图片-----*/
    uploadPhotos: function(t, e) {
        if (0 == this.data.filePaths.length) return t();
        if ("number" != typeof e) e = 0; else if (e >= this.data.filePaths.length) return t();
        var s = this;
        a.uploadFile("/api/order/upload_return_goods_img", {
            filePath: s.data.filePaths[e],
            name: "return_imgs",
            success: function(a) {
                s.data.uploadPath[e] = a.data.result, s.uploadPhotos(t, e + 1);
            }
        });
    },

    //----检查提交-----
    checkSubmit: function(e) {
        return this.data.checkCauseIndex < 0 ? t.showWarning("请先选择提交原因") : 0 == this.data.description.length ? t.showWarning("请先填写问题描述") : void e();
    },
    //------图片失败，默认图片---------
    bind_bnerr: function (e) {
    var _errImg = e.target.dataset.errorimg;
    var _errObj = {};
    _errObj[_errImg] = "public/images/default_goods_image_240.gif";
    this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;       
   },
});