// packageA/member/coupon_v2_detail/coupon_v2_detail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: '',
    rechargeNum: 1,
    config_info: {
      switch: 0
    },
    presentershow: false,
    showRechargePay: false,
    multiple_use:{is_open:false,nums:0},  //是否开启多张赠送
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
    console.log(JSON.parse(options.info));
    if (options.info) {
      this.setData({
        info: JSON.parse(options.info)
      });
    }
    if (options.combine) {
      this.setData({
        combine: options.combine
      });
    }

    this.getdetail();
  },
  //赠送优惠券
  presenter(item, index) {
    let that = this;
    console.log(item, this.info);
    // let item = e.currentTarget.dataset.item;
    // let index = e.currentTarget.dataset.index;
    that.setData({
      'usermessage.value': '',
      'usermessage.item': '',
      'usermessage.index': '',
      memberInfo: ''
    });
    that.setData({
      'usermessage.item': item,
      'usermessage.index': index,
      'presentershow': true
    });
  },
  onclose(event) {
    let that = this;
    that.setData({
      'usermessage.value': '',
      'usermessage.item': '',
      'usermessage.index': ''
    });
    that.setData({
      presentershow: false
    });
  },
  usermesbut: function (e) {
    let val = e.detail.value;
    this.setData({
      'usermessage.value': val
    });
  },
  usermesbutInput(e) {
    let val = e.detail.value;
    this.data.usermessage.value = val;
  },
  onconfirm(event) {
    let that = this;
    console.log(this.data.usermessage);
    if (app._isTextEmpty(this.data.usermessage.value)) {
      wx.showToast({
        icon: 'none',
        title: '用户ID不能为空',
        duration: 1000
      });
      this.setData({
        presentershow: false
      });
      return;
    }
    if (this.data.memberInfo == '查无此用户') {
      wx.showToast({
        icon: 'none',
        title: '查询不到该用户ID，请确认无误!',
        duration: 1000
      });
      return;
    }
    if (this.data.config_info.switch) {
      this.setData({
        showRechargePay: true
      });
      return;
    }
    that._presenterPost();
  },
  //赠送post
  _presenterPost() {
    let that = this;
    let urlStr = app.getNetAddresss("coupon.coupon-transfer.index");
    urlStr += "&recipient=" + this.data.usermessage.value;
    urlStr += "&record_id=" + this.data.info.id;
    if (this.data.config_info.switch) {
      urlStr += "&quantity=" + this.data.rechargeNum;
      urlStr += "&coupon_id=" + this.data.info.coupon_id;
    }else{
      urlStr += "&transfer_num=" + this.data.rechargeNum;
    }
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (that.data.config_info.switch) {
            //跳转支付手续费
            wx.navigateTo({
              url: '/packageD/buy/orderPay/orderPay?order_id=' + res.data.order_ids + '&status=2'
            });
            return;
          }
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1000,
            success: function (e) {
              if (that.data.combine) {
                that.setData({
                  'info.num': that.data.info.num - 1
                });
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1,
                  });
                }, 1000);
              } else {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1,
                  });
                }, 1000);
              }

            }
          });
          that.setData({
            presentershow: false
          });

        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          that.setData({
            presentershow: false
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getmemberInfo(e) {
    let val = e.detail.value;
    var that = this;
    let urlStr = app.getNetAddresss("member.member.memberInfo");
    urlStr += '&uid=' + val;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            memberInfo: res.data ? res.data.nickname : '查无此用户'
          });
        } else {
          that.setData({
            memberInfo: '查无此用户'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getdetail() {
    let that = this;
    let urlStr = app.getNetAddresss("coupon.member-coupon.get-detail");
    urlStr += "&record_id=" + that.data.info.id;
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let changeData={};
          changeData.rule=res.data.rule;
          changeData.img_url=res.data.img_url;
          changeData.datainfo=res.data.coupon;
          if (res.data.coupon_fee_plugin) {
            changeData.config_info=res.data.coupon_fee_plugin;
            changeData.config_info.switch = parseInt(changeData.config_info.switch);
          }
          if(res.data.multiple_use){
            changeData.multiple_use = res.data.multiple_use;
          }
          this.setData(changeData);
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  rentDeleteNumber() {
    let that = this;
    if (this.data.rechargeNum - 1 <= 0) {
      wx.showToast({
        icon: 'none',
        title: `转赠张数至少一张`,
        duration: 1000
      });
      that.setData({
        rechargeNum: 1
      });
      return;
    } else {
      that.setData({
        rechargeNum: that.data.rechargeNum - 1
      });
    }
  },
  rentAddNumber() {
    let that = this;
    if(this.data.multiple_use.is_open){
      if (this.data.rechargeNum + 1 > this.data.multiple_use.nums) {
        app.tips(`分享张数不足,当前可转赠数量为${that.data.multiple_use.nums}张`);
        return;
      }else {
        that.setData({
          rechargeNum: that.data.rechargeNum + 1
        });
      }
    }else if (this.data.rechargeNum + 1 > this.data.info.num) {
      app.tips(`分享张数不足,当前可转赠数量为${that.data.info.num}张`);
      return;
    } else {
      that.setData({
        rechargeNum: that.data.rechargeNum + 1
      });
    }
  },
  changeRechargeNum(e) {
    let val = Number(e.detail.value);
    let that = this;
    if(this.data.multiple_use.is_open){
      if (val > this.data.multiple_use.nums){
        app.tips(`分享张数不足,当前可转赠数量为${that.data.multiple_use.nums}张`);
        that.setData({
          rechargeNum: that.data.multiple_use.nums
        });
      }
    }else if (val > this.data.info.num) {
      app.tips(`分享张数不足,当前可转赠数量为${that.data.info.num}张`);
      that.setData({
        rechargeNum: that.data.info.num
      });
      return;
    }
  },
  //立即使用    通过类型去查询可用商品
  goBuy(e) {
    // console.log(item)
    let item = this.data.datainfo;
    console.log(item);
    console.log('packageB/member/category/search_v2/search_v2');
    if (item.use_type == 0) { //0商城通用
      this.goHome();
    } else if (item.use_type == 1) { //1指定分类
      wx.navigateTo({
        url: '/packageB/member/category/catelist/catelist?id=' + JSON.stringify(item.category_ids) +
          '&fromHome=1'
      });
    } else if (item.use_type == 2 || item.use_type == 8) { //2指定商品
      if (item.plugin_id == 32) {
        wx.navigateTo({
          url: '/packageC/o2o/StoreSearch/StoreSearch?store_id=' + item.storeids + "&goods_ids=" + item.goods_ids.join(",")
        });
      } else {
        wx.navigateTo({
          url: '/packageB/member/category/search_v2/search_v2?coupon=' + item.id + '&fromHome=1'
        });
      }

    } else if (
      item.use_type === 4 &&
      Array.isArray(item.storeids)
    ) {
      //4门店列表
      wx.navigateTo({
        url: '/packageC/o2o/o2oHome/o2oHome'
      });
    } else if (
      item.use_type === 5 &&
      typeof item.storeids === "number"
    ) {
      //5单门店
      wx.navigateTo({
        url: '/packageC/o2o/o2oStore/o2oStore?store_id=' + item.storeids
      });
    } else if (
      item.use_type === 7 &&
      Array.isArray(item.hotel_ids)
    ) {
      wx.navigateTo({
        url: '/packageC/hotel/HotelIndex/HotelIndex'
      });
    } else if (
      item.use_type === 6 &&
      typeof item.hotel_ids === "number"
    ) {
      wx.navigateTo({
        url: '/packageC/hotel/HotelHome/HotelHome?id=' + item.hotel_ids
      });
    }  else {
      this.goHome();
    }
  },
  goHome() {
    wx.reLaunch({
      url: '/packageG/index/index',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let name = wx.getStorageSync('nickname');
    return{
      path:"/packageA/member/coupon_v2/coupon_v2",
      title:`${name}向您分享了优惠券`
    };
  },
  onShareTimeline(){
    let name = wx.getStorageSync('nickname');
    return {
      title:`${name}向您分享了优惠券`
    };
  },
});