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

Page({
    data: {
        url: t.url,
        resourceUrl: t.resourceUrl,
        iurl: t.imghost,
        user: null,
        openReasonModal: !1,
        orderId: 0,
        order_sn:"",
        back_money: 0,
        back_integral: 0,
        reasonSelect: 0,
        reasonList: [ "没有收到货", "不喜欢/不想要", "个人原因拍错/漏拍", "空包裹", "未按照约定时间发货", "快递/物流未送达", "快递/物流无跟踪记录", "发错快递", "商品破损", "商品尺寸规格不符", "商品质量差，与网上描述不符", "其他原因" ],
        refund_type:0,
    isloading:0,
    template_id:[], 
    },
    onLoad: async function(t) {
       var th=this;
       getApp().getConfig2(function (em) {

          console.log('getConfig2');
          console.log(em);

          th.data.refund_type = em.refund_type;
          e.getUserInfo(function(e) {

              console.log('onload');
              console.log(e);

              th.setData({
                  orderId: t.order_id,
                  user: e,
              }), 
			  wx.showLoading();
              rq.get("/api/weshop/order/get/" + os.stoid + "/" + t.order_id,{
                  success:function(ed){
                    var bm = ed.data.data.order_amount + ed.data.data.user_money
                        + ed.data.data.pt_tail_money
                    th.setData({
                        order_sn: ed.data.data.order_sn,
                        back_money: bm
                    })
                    //如果有积分的话
                    if( ed.data.data.integral>0){
                        th.setData({back_integral:ed.data.data.integral});       
                    }

					wx.hideLoading();
                  }
              })
          });
       })
       try {
        let template_id =await getApp().get_template_id('1025')
        this.setData({
          template_id
        })
      } catch (error) {}
    },
       //订阅消息提醒
       sendsm: function (ee) {
        let th = this;
        var template_id = this.data.template_id;
        console.error('退款');
        console.error(template_id);
        // //授权订阅
        wx.requestSubscribeMessage({
          tmplIds: template_id,
          success(res) {
            th.refundOrder(ee);
          },
          fail(res) {
            th.refundOrder(ee);
          }
        })
      },
    //------提交退款申请-------
    refundOrder: function(e) {	   
	   //if(!this.data.back_money){
	   //	   return false;
	   //}	   
	   if(this.data.isloading) return false;
	   this.data.isloading=1;   		
       var th = this;
       //----获取订单的所有商品-----
       rq.get('/api/weshop/ordergoods/list',{
             data: { order_id: th.data.orderId,store_id:os.stoid},
             success:function(od){
               console.log('ordergoods');
               console.log(od);
               var glist="";
               var olist=od.data.data.pageData;               
			   /*--
			   olist.forEach(function(ie,ind){
                    glist+=ie.goods_id+","
               })--*/
			   for(var i in olist){
				    var ie=olist[i];
				    glist+=ie.goods_id+","
			   }
			     
               glist = glist.substring(0, glist.length-1);  
               console.log(glist);
               var r_data={
                order_id: th.data.orderId,
                order_sn: th.data.order_sn,
                store_id:os.stoid,
                user_id: th.data.user.user_id,
                back_money: th.data.back_money,
                reason: th.data.reasonList[th.data.reasonSelect],
                goods_id_list: glist,
                addtime: ut.gettimestamp(),
                type:2,
                refund_type: th.data.refund_type,
             };

             if(th.data.back_integral){
               r_data.back_integral=th.data.back_integral;   
             }
               rq.post("/api/weshop/order/returngoods/saveReturnGoods", {
                 data: r_data,
                 success: function (e) {
                   wx.showToast({
                     title: "已提交申请",
                     mask: !0,
                     duration: 1e3,
                     complete: function () {
                       wx.setStorageSync("order:order_list:update", !0), wx.setStorageSync("order:order_detail:update", !0),
                         setTimeout(function () {
                           wx.navigateBack();
                         }, 1e3);
                     }
                   });
                 }
               });
             }
       })        
    },
    openReasonModal: function() {
        this.setData({
            openReasonModal: !0
        });
    },
    closeReasonModal: function() {
        this.setData({
            openReasonModal: !1
        });
    },
    reasonChange: function(e) {
        this.setData({
            reasonSelect: e.detail.value
        });
    }
});