// pages/member/myOrder/Aftersales/Aftersales.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    //退款
    refund_id: "",
    button_models: "",
    price: "",
    //退款说明
    content: "",
    status_name: "",
    //退款原因
    reason: "",
    refund_time: "",
    refund_type_name: "",
    alipay_batch_sn: "",
    create_time: "",
    refund_sn: "",
    reject_reason: "",
    is_plugin: "",
    store_id: "",
    supplier_id: "",
    plugin_id: "",
    _options: {},
    remark: "",
    images: [],

    showID: "",

    refund_order_goods:[],
    return_express:null,
    myData:{},
    freight_price: "", //可退运费
    other_price: "" //其他费用
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.refund_id) {
      this.setData({
        refund_id: options.refund_id,
      });
    }
    this.setData({
      _options: options,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._initData();
    this._getNetData();
  },

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

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

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

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

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

  copyexpress_sn({currentTarget:{dataset:{data,msg}}}){
    wx.setClipboardData({
      data:data,
      success(){
        wx.showToast({
          title:msg + "已复制"
        })
      },
      fail(){
        wx.showToast({
          title:msg + '已复制',
          icon:  "none"
        })
      }
    })
  },

  showMore(e) {
    let order_sn = e.currentTarget.dataset.item.value;
    if (this.data.showID == order_sn) {
      this.setData({
        showID: "",
      });
    } else {
      this.setData({
        showID: order_sn,
      });
    }
  },
  _initData() {
    this.setData({
      //退款
      button_models: "",
      price: "",
      //退款说明
      content: "",
      status_name: "",
      //退款原因
      reason: "",
      refund_time: "",
      refund_type_name: "",
      alipay_batch_sn: "",
      create_time: "",
      remark: "",
      images: [],
      refund_sn: "",
      reject_reason: "",
      is_plugin: "",
      store_id: "",
      supplier_id: "",
      plugin_id: "",
    });
  },
  _getNetData() {
    let urlStr = app.getNetAddresss("refund.detail");
    if (
      this.data._options &&
      this.data._options.pluginid &&
      this.data._options.pluginid == "69"
    ) {
      urlStr = app.getNetAddresss("plugin.snatch-regiment.api.refund.detail");
    }
    if( this.data._options && this.data._options.debt ){
      urlStr = app.getNetAddresss("plugin.debt-shop.frontend.refund-detail.index");
      urlStr += "&member_type="+this.data._options.debt;
    }
    urlStr += "&refund_id=" + this.data.refund_id;
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            button_models: res.data.button_models,
            price: res.data.price,
            content: res.data.content,
            status_name: res.data.status_name,
            reason: res.data.reason,
            refund_time: res.data.refund_time,
            refund_type_name: res.data.refund_type_name,
            alipay_batch_sn: res.data.alipay_batch_sn,
            create_time: res.data.create_time,
            remark: res.data.remark,
            images: res.data.images || [],
            refund_sn: res.data.refund_sn,
            reject_reason: res.data.reject_reason,
            is_plugin: res.data.is_plugin ? res.data.is_plugin : null,
            store_id: res.data.store_id ? res.data.store_id : null,
            supplier_id: res.data.supplier_id ? res.data.supplier_id : null,
            plugin_id: res.data.plugin_id ? res.data.plugin_id : null,
            refund_order_goods:res.data.refund_order_goods,
            return_express:res.data.return_express,
            freight_price : Number(res.data.freight_price) || 0, //可退运费
            other_price : Number(res.data.other_price) || 0, //其他费用
            myData:res.data
          });
          if(res.data.refund_address!=""){
            this._getaddress();
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  gotoNegotiationRecord(){
    wx.navigateTo({
      url: '/packageD/member/myOrder/negotiationRecord/negotiationRecord?refund_id='+this.data.refund_id,
    });
  },
  gotoLogistics(){
    wx.navigateTo({
      url: '/packageA/member/order/logistics/logistics?from=userReturn&refund_id='+this.data.refund_id,
    });
  },
  _getaddress() {
    let that = this;
    let urlStr = app.getNetAddresss("order.refund.return-address");
    urlStr +=
      "&plugins_id=" +
      (this.data.is_plugin == 1 ? this.data.is_plugin : this.data.plugin_id);
    urlStr += "&store_id=" + this.data.store_id;
    urlStr += "&supplier_id=" + this.data.supplier_id;
    urlStr += "&refund_id=" + this.data.refund_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            address: res.data,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  operation(e) {
    let btn = e.currentTarget.dataset.btn;
    if (btn.value == 2) {
      //退款快递信息
      wx.navigateTo({
        url: "../express/express?refund_id=" + this.data.refund_id,
      });
    } else if (btn.value == 1) {
      //申请售后
      wx.navigateTo({
        url:
          "/packageD/member/myOrder/refund/refund?order_id=" +
          this.data.refund_id +
          "&type=2",
      });
    } else if (btn.value == 3) {
      //确认收货
      this._confirm();
    }  else if(btn.value == 6){
      if(this.data.myData.resend_express_id == 0){
        wx.navigateTo({
          url: '/packageE/multiplePackages/multiplePackages?from=resend&id='+this.data.refund_id,
        });
      }else {
        wx.navigateTo({
          url: '/packageA/member/order/logistics/logistics?from=resend&refund_id='+this.data.refund_id+ "&express_id="+this.data.myData.resend_express_id,
        });
      }
    }else {
      //取消申请
      this._cancleRefund();
    }
  },
  //换货确认收货
  _confirm() {
    let urlStr = app.getNetAddresss("refund.operation.complete");
    urlStr += "&refund_id=" + this.data.refund_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  _cancleRefund() {
    let urlStr = app.getNetAddresss("refund.operation.cancel");
    urlStr += "&refund_id=" + this.data.refund_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //预览图片，放大预览
  preview(e) {
    let currentUrl = e.currentTarget.dataset.src;
    wx.previewImage({
      current: this.data.images[currentUrl], // 当前显示图片的http链接
      urls: this.data.images // 需要预览的图片http链接列表
    });
  },
});
