// packageI/offline_investment/offlineInvestmentIndex/offlineInvestmentIndex.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activity_id: 0,
    hand_goods_obj: {},
    hand_goods_id: 0,
    specsShow: false,
    goodsPopupShow: false,
    currentTime: 0,
    timeData: {},
    carList: [],
    language: {},
    networkLoading: false, //网络请求
    end_time: "",

    info: {},
    orderParams: {},
    store_id: null //如为门店商品，下单需要门店id
  },

  onChangeTime(e) {
    this.setData({
      timeData: e.detail,
    });
  },

  close_yz_specs_popup(e) {
    console.log(e);
    if (e.detail.clicktype == 1) {
      //点击确认按钮
      let orderParams = {
        id: e.detail.goodsId,
        total: e.detail.goodsCount,
        optionsId: e.detail.optionsId
      };
      if (this.data.store_id) {
        orderParams.store_id = this.data.store_id;
      }
      this.setData({
        orderParams: orderParams,
        specsShow: false
      });
      this.verifyFrom();
    } else {
      this.setData({
        specsShow: false,
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let activity_id = options.id;
    let scene = decodeURIComponent(options.scene);
    if (scene) {
      var info_arr = [];
      info_arr = scene.split("&");
      console.log(info_arr);
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split("=");
        console.log(chil_arr);
        if (chil_arr[0] == "id") {
          activity_id = chil_arr[1];
        }
        if (chil_arr[0] == "mid") {
          app._setMid(chil_arr[1]);
        }
      }
      console.log(info_arr);
    }

    if (options.mid) {
      app._setMid(options.mid);
    }

    if (activity_id && activity_id != 0) {
      this.setData({
        activity_id,
        info:{},
        timeData: {}
      });
      this.getData(activity_id);
    } else this.tips("参数错误");
  },

  getData(activity_id) {
    let urlStr = app.getNetAddresss(
      "plugin.merchant-meeting.frontend.index.activity"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        activity_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);
        wx.setNavigationBarTitle({
          title: res.data.plugin_name
        });
        this.setData({
          //status 0未开始，1进行中，-1已失效
          currentTime: parseInt(new Date().getTime() / 1000),
          info: res.data.activity
        });
      },
    });
  },

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

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

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

  openGoodsDetail(e) {
    let item = e.currentTarget.dataset.item;
    if (item.id == this.data.hand_goods_id) {
      this.setData({
        goodsPopupShow: true,
      });
    } else {
      this.data.hand_goods_id = item.id;
      this.getGoodsDetails(item.id, "goodsDetails");
    }
  },


  openSpecs(e) {
    let item = e.currentTarget.dataset.item;
    if (item.id == this.data.hand_goods_id) {
      this.setData({
        specsShow: true,
      });
    } else {
      this.data.hand_goods_id = item.id;
      this.getGoodsDetails(item.id, "specs");
    }
  },

  getGoodsDetails(id, type) {
    let urlStr = app.getNetAddresss("goods.goods.get-goods-page");
    app._postNetWork({
      url: urlStr,
      data: {
        id: Number(id)
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return this.tips(res.msg);
        let name = "";
        if (type == "goodsDetails") {
          name = "goodsPopupShow";
        } else if (type == "specs") {
          name = "specsShow";
        }
        res.data.get_goods.end_time = (this.data.info.end_time - this.data.currentTime) * 1000;
        this.data.store_id = res.data.goods_type == "store_goods" ? res.data.get_store_info.store_id : 0;
        this.setData({
          [name]: true,
          hand_goods_obj: res.data.get_goods,
        });
      },
    });
  },

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


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

  goodsDetailCartAdd(evt) {
    this.setData({
      goodsPopupShow: false,
      specsShow: true,
    });
  },
  //下单前验证
  verifyFrom() {
    let that = this;
    let goods = [];
    goods.push(that.data.orderParams.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) {
          var _data = res.data;
          console.log("xxxx", _data);
          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=delta",
                  });
                } else if (res.cancel) {
                  console.log("用户点击取消");
                }
              },
            });
          } else {
            let url = "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2";
            let _urlParam = "&goodsId=" + that.data.orderParams.id + "&optionsId=" + that.data.orderParams.optionsId + "&total=" + that.data.orderParams.total + "&merchant_meeting=" + that.data.activity_id;
            if(that.data.orderParams.store_id){
              _urlParam = _urlParam + "&store_id=" + that.data.orderParams.store_id;
            }
            wx.navigateTo({
              url: url + _urlParam
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  tips(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
    });
    return false;
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    return {
      title: this.data.info.title,
      path: "/packageI/offline_investment/offlineInvestmentIndex/offlineInvestmentIndex?id=" +
        this.data.activity_id + '&mid=' + mid,
    };
  },
});