// packageD/team_sell/getCouponShare/getCouponShare.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coupon_id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      coupon_id: options.id,
    });
    this.getData();
  },
  fuzhi() {
    //     let value =  this.goods[0].title +'\r日常价：' +this.goods[0].price +'\r券后价：' +this.goods[0].deduct_price +'\r' +
    // _link
    //     wx.setClipboardData({
    //       data: ,
    //       success (res) {
    //         wx.getClipboardData({
    //           success (res) {
    //             console.log(res.data) // data
    //           }
    //         })
    //       }
    //     })
  },
  selectedcoupon(e) {
    let item = e.currentTarget.dataset.item;
    // let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    //领取
    let that = this;
    let urlStr = app.getNetAddresss("coupon.member-coupon.get-coupon");
    urlStr += "&coupon_id=" + item.id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          // var temp = res.data;
          // that.setData({
          // 	['coupon_list[' + index + ']']: temp
          // })
          wx.showToast({
            title: "领取成功",
            icon: "none",
            duration: 1000,
            success: function () {
              if (that.data.goods.length == 1) {
                setTimeout(() => {
                  wx.navigateTo({
                    url: "/packageA/detail_v2/detail_v2?id=" + id,
                  });
                }, 500);
              }
            },
          });
        } else {
          if(res.data.reason){
            wx.showModal({
              title:res.msg,
              content:res.data.reason,
              showCancel:false
            });
          }else{
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1000
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    // let value = wx.getStorageSync("yz_uid");
    // let mid = "";
    // if (value) {
    //   mid = value;
    // }
    return {
      title:
        this.data.goods[0].title +
        "\r日常价：" +
        this.data.goods[0].price +
        "\r券后价：" +
        this.data.goods[0].deduct_price,
      query: `id=${this.data.coupon_id}`,
    };
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:
        this.data.goods[0].title +
        "\r日常价：" +
        this.data.goods[0].price +
        "\r券后价：" +
        this.data.goods[0].deduct_price,
      path:
        "/packageE/team_sell/getCouponShare/getCouponShare?id=" +
        this.data.coupon_id,
    };
  },
  toDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + id,
    });
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "coupon.member-coupon.getGoodsCouponByCouponId"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        coupon_id: that.data.coupon_id,
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            datas: res.data,
            goods: res.data.goods,
            icon: res.data.icon,
            time_start: res.data.time_start.split(" ")[0],
            time_end: res.data.time_end.split(" ")[0],
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000,
        });
      },
    });
  },
});
