// packageE/community_buying/buying_details/buying_details.js
var WxParse = require("../../../wxParse/wxParse");
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    timeData: {},
    checked: false,
    time: 24 * 60 * 60 * 1000,
    buyingid: 0,
    detailData: {},
    goods: [],
    explainFlag: false,
    deliver_id: 0,
    shareShow: false,
    shareOptions: [
      { name: "生成海报", icon: "poster" },
      { name: "分享给好友", icon: "wechat", openType: "share" },
    ],
  },

  onSelectShare(evt) {
    this.onCloseShare();
    if (evt.detail.index == 0) {
      this.setData({
        posterShow: true,
      });
    }
    console.log(evt.detail.index);
  },
  onCloseShare() {
    this.setData({ shareShow: false });
  },
  openShareShow() {
    this.setData({
      shareShow: true,
    });
  },
  gotoBuyingOrder() {
    wx.navigateTo({
      url:
        "/packageE/community_buying/buying_order/buying_order?buyingid=" +
        this.data.buyingid,
    });
  },
  gotoBuyingData() {
    wx.navigateTo({
      url:
        "/packageE/community_buying/buying_data/buying_data?buyingid=" +
        this.data.buyingid,
    });
  },
  setExplainFlag() {
    this.setData({
      explainFlag: !this.data.explainFlag,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      buyingid: options.buyingid,
    });
    this.getDetails();
  },
  onChangeTime(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  onChangeSwitch(evt) {
    console.log(evt);
    // 需要手动对 checked 状态进行更新
    let goods_id = evt.target.dataset.goods_id;
    let index = evt.target.dataset.index;
    let target = "goods[" + index + "].checked";
    this.setData({ [target]: evt.detail });
    let urlStr = app.getNetAddresss(
      "plugin.package-deliver.frontend.communityGroups.banGoods"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        group_id: this.data.buyingid,
        goods_id,
      },
      success: (resdata) => {
        var res = resdata.data;
        console.log(res);
        if (res.result != 1) {
          return;
        }
        console.log("操作成功");
      },
    });
  },

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

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

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

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

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

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

  getDetails() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.package-deliver.frontend.communityGroups.groupGoods"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.buyingid,
      },
      success: (resdata) => {
        var res = resdata.data;
        console.log(res);
        if (res.result != 1) {
          return;
        }
        let detailData = res.data.group;
        let time = 0;
        if (detailData.group_status == 0) {
          time =
            new Date(detailData.start_time.replace(/-/g, "/")).getTime() -
            new Date().getTime();
        } else if (detailData.group_status == 1) {
          time =
            new Date(detailData.end_time.replace(/-/g, "/")).getTime() -
            new Date().getTime();
        }
        let goods = detailData.goods;
        goods.forEach((item) => {
          item.checked = item.status == 0 ? false : true;
        });
        this.setData({
          deliver_id: res.data.deliver_id,
          detailData,
          time,
          goods,
        });
        let article = detailData.description;
        try {
          if (article.indexOf("&#") <= -1) {
            WxParse.wxParse("article", "html", article, that);
          }
        } catch (e) {
          console.log(e);
        }
      },
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var mid = wx.getStorageSync('yz_uid');
    
    let path =
      "/packageE/community_buying/buying_personal/buying_personal?buyingid=" +
      this.data.buyingid +
      "&deliver_id=" +
      this.data.deliver_id;
    if (mid) {
      path = path + '&mid=' + mid
    }
    let obj = { path };
    if (this.data.detailData.share_img) {
      obj.imageUrl = this.data.detailData.share_img;
    }
    return obj;
  },
});
