// packageD/preferential_volume/preferential_volume.js
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    member_coupon: "",
    goods_id: "",
    coupon: "",
    goods: "",
    goodsid: "",
    shop_logo: "",
    start: "",
    end: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene,'ssssssssssssss');
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(",");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          if (chil_arr[0] == "vgid") {
            this.setData({
              goods_id: chil_arr[1],
            });
          } else if (chil_arr[0] == "mid") {
            app._setMid(chil_arr[1]);
          } else if (chil_arr[0] == "vcpn") {
            this.setData({
              member_coupon: chil_arr[1],
            });
          }
        }
      }
    } else {
      if (options.member_coupon) {
        this.setData({
          member_coupon: options.member_coupon,
        });
      }
      if (options.goods_id) {
        this.setData({
          goods_id: options.goods_id,
        });
      }
    }
    this.getData();
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.coupon-qr.api.index.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        goods_id: that.data.goods_id?that.data.goods_id:0,
        member_coupon_id: that.data.member_coupon,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let start = "";
          let end = "";
          let goodsid = "";
          let shop_logo = "";
          if (res.data.coupon.start) {
            start = that.format(res.data.coupon.start);
          }
          if (res.data.coupon.end) {
            end = that.format(res.data.coupon.end);
          }
          if (res.data.goods && res.data.goods.id) {
            goodsid = res.data.goods.id;
          }
          if (res.data.shop_logo) {
            shop_logo = res.data.shop_logo;
          }
          that.setData({
            shop_logo,
            goodsid,
            start,
            end,
            coupon: res.data.coupon,
            goods: res.data.goods,
          });
          if (
            res.data.coupon.coupon_status &&
            res.data.coupon.coupon_status == 1 &&
            res.data.goods.status == 1
          ) {
            wx.showToast({
              icon: "none",
              title: "该优惠券已被领取",
              duration: 1500,
              success: function () {
                setTimeout(function () {
                  //要延时执行的代码
                  that.gogoods();
                }, 1500); //延迟时间
              },
            });
          } else if (that.data.coupon.link) {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
              success: function () {
                setTimeout(function () {
                  //要延时执行的代码
                  wx.navigateTo({
                    url: that.data.coupon.link,
                  });
                }, 1500); //延迟时间
              },
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.reLaunch({
                  url: "/packageG/index/index",
                });
              }, 1500); //延迟时间
            },
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },

  add0(m) {
    return m < 10 ? "0" + m : m;
  },
  format(shijianchuo) {
    //时间数是整数，否则要parseInt转换
    var time = new Date(shijianchuo * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return y + "." + this.add0(m) + "." + this.add0(d);
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  gogoods() {
    wx.redirectTo({
      url: "/packageA/detail_v2/detail_v2?id=" + this.data.goodsid,
    });
  },
});
