var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    countDownList: [],
    actEndTimeList: [],
    groupDetail: {}, // 总的信息
    goodsDetail: {}, // 商品信息
    has_many_success_member: '', // 参团会员信息
    has_many_success_member_count: '', // 已参团的成员数
    member_num: '', // 总的参团人数
    info: {},
    id: '',
    showOpenGroup: false, // 规格弹窗
    goodsInfo: {},
    goodsDescription: '',
    popPrice: 0,
    level_id: 0, // 当前拼团的层级id
    goodsCount: 1, // 购买数量
    goodsId: '',
    activityId: '', // 活动id

    isOpen: 0, // 活动是否开启
    status: 0,
    action: 0,
    activityTest: '', // 当前活动状态
    btnTest: '', // 按钮的文字
    btnStatus: false,// 按钮是否禁用
    option_level_id: "", //规格层级id
    nowTime:new Date().getTime() / 1000,
    isCheckActivity: true,//下单前检查客户今天是否超出限制
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.mid) {
      app._setMid(options.mid);
    }
    console.log(options,'参数');
    this._getData();
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
      withShareTicket: false,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareTimeline(){
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    return{
      query:`id=${this.options.id}&mid=${mid}`
    };
  },
  onShareAppMessage: function (res) {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    return {
      title: '向您分享一个拼购商品',
      path:'/packageB/member/group_buy/group_buy_detail/group_buy_detail?id='+this.options.id+ '&mid=' + mid
    };

  },
  _getData() {
    let urlStr = app.getNetAddresss('plugin.together-purchase.api.index.getTogetherData');
    urlStr += '&o_id=' + this.options.id + '&art=purchaseDetail';
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info: res.data.purchaseDetail,
          });
          this.checkActivity();
          let _name = '';
          switch (res.data.purchaseDetail.state) {
          case 0:
            _name = '待支付';
            break;
          case 1:
            _name = '拼购中';
            break;
          case 2:
            _name = '拼购成功';
            break;
          case 3:
            _name = '拼购失败';
            break;
          case 4:
            _name = '成团中';
            break;
          case 5:
            _name = '已退款';
            break;
          case 6:
            _name = '已返还爱心值';
            break;
          case 7:
            _name = '无效';
            break;
          }
          this.setData({
            'info.statename': _name
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  // 获取数据
  checkActivity() {
    let urlStr = app.getNetAddresss('plugin.together-purchase.api.index.getTogetherData');
    urlStr += '&at_id=' + this.data.info.activity_id + '&art=checkActivity';

    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            isCheckActivity: true
          });
        } else {
          this.setData({
            isCheckActivity: false
          });
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  joinTeam() {
    let that = this;
    if (!this.data.isCheckActivity) {
      wx.showToast({
        title: '抱歉，您已超出限制条件',
        icon:'none'
      });
      return;
    }
    if (this.data.info.status == 0) {
      wx.showToast({
        title: '很抱歉，该活动已结束',
        icon:'none'
      });
      return;
    }
    let goods = [];
    goods.push(this.data.info.goods_id);
    let urlStr = app.getNetAddresss("from.div-from.isDisplay");
    urlStr += "&goods_ids=" + JSON.stringify(goods);
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.status && !res.data.member_status) {
            wx.showModal({
              title: '提示',
              content: '购买此商品,请补充您的个人信息',
              success(res) {
                if (res.confirm) {
                  //保税真实信息添加
                  wx.navigateTo({
                    url: '/packageB/member/address/myinfo/myinfo?tag=groupBuy_join' + '&activityId=' + that.data.info.activity_id +'&goodsId=' + that.data.info.goods_id + '&leader_id='+that.data.info.leader_id
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          } else {
            wx.navigateTo({
              url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-2' + '&optionsId=0'+'&goodsId=' + that.data.info.goods_id + '&total=1'+ '&type=groupBuy_join'+ '&at_id='+that.data.info.activity_id + '&leader_id='+that.data.info.leader_id
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function (res) {}
    });
  },
  openGroup(){
    if (!this.data.isCheckActivity) {
      wx.showToast({
        title: '抱歉，您已超出限制条件',
        icon:'none'
      });
      return;
    }
    if (this.data.info.status == 0) {
      wx.showToast({
        title: '很抱歉，该活动已结束',
        icon:'none'
      });
      return;
    }
    let goods = [];
    goods.push(this.data.info.goods_id);
    let urlStr = app.getNetAddresss("from.div-from.isDisplay");
    urlStr += "&goods_ids=" + JSON.stringify(goods);
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.status && !res.data.member_status) {
            wx.showModal({
              title: '提示',
              content: '购买此商品,请补充您的个人信息',
              success(res) {
                if (res.confirm) {
                  //保税真实信息添加
                  wx.navigateTo({
                    url: '/packageB/member/address/myinfo/myinfo?tag=groupBuy_open' + '&activityId=' + this.data.info.activity_id +'&goodsId=' + this.data.info.goods_id
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          } else {
            wx.navigateTo({
              url: '/packageD/buy/myOrder_v2/myOrder_v2?tag=-2' + '&optionsId=0'+'&goodsId=' + this.data.info.goods_id + '&total=1'+ '&type=groupBuy_open'+ '&at_id='+this.data.info.activity_id
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function (res) {}
    });
  },

  // 去拼购聚合页
  gotoList() {
    wx.navigateTo({
      url: '/packageB/member/group_buy/group_buy_list/group_buy_list'
    });
  },
  gotoGoods() {
    let that = this;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + that.data.info.goods_id  + '&activity_id='+that.data.info.activity_id+'&name=group_buy'
    });
  },
  gotoOrder() {
    let order_id = this.options.id;
    wx.navigateTo({
      url: '/packageA/member/orderdetail/orderdetail?order_id=' + order_id + '&orderType=groupsBuy'
    });
  },
});
