// packageI/warehouseFarm/warehouseFarmGiftDetail/warehouseFarmGiftDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receive_detail: null,
    info: {},
    goods: {},
    received_info: [], //领取人信息
    received_time: null, //领取时间
    isReceived: true, //是否已领取
    showWechatshar: false,
    mode: null, //当前模式（creat：创建礼物赠送||receiving or getgift: 领取别人礼物）
    shareInfo: {} //分享信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.mode) {
      this.setData({
        mode: options.mode
      });
      this.getData();
    }
  },

  getData() {
    let _url = ""; //请求api
    let _json = {}; //请求参数
    let apiConfig = new Map([
      ["creat", "plugin.warehouse.frontend.controllers.gift.give"],
      ["receiving", "plugin.warehouse.frontend.controllers.gift.records.detail"],
      ["getgift", "plugin.warehouse.frontend.controllers.gift.records.detail"],
      ["give", "plugin.warehouse.frontend.controllers.gift.give.receive"]
    ]);

    if (apiConfig.has(this.data.mode)) {
      //先通过has()判断apiConfig是否给key
      _url = apiConfig.get(this.data.mode); //获取key(即this.orderType)设置的api集合
    }

    if (this.data.mode == "creat") {
      //创建礼物赠送页面进入
      _json = {
        goods_id: this.options.gid,
        nums: Number(this.options.nums),
        share_num: this.options.sNum
      };
      this.setData({
        isReceived: false //显示分享按钮
      });
    } else if (this.data.mode == "receiving" || this.data.mode == "getgift") {
      // 从礼物查看记录进入的
      _json = {
        gift_type: this.data.mode == "getgift" ? "get" : "",
        records_id: this.options.records_id //记录id
      };
    } else if (this.data.mode == "give") {
      // 首页红包弹窗进入的
      _json = {
        gift_key: this.options.id //礼包码
      };
    }
    let urlStr = app.getNetAddresss(_url);
    app._postNetWork({
      url: urlStr,
      data: _json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.data.shareInfo.title = (res.data.share_info && res.data.share_info.title) || "赠送礼物";
          this.data.shareInfo.imgUrl = (res.data.share_info && res.data.share_info.thumb) || null;

          if (this.data.mode == "creat") {
            this.setInfo(res.data.info, res.data.goods);
            this.data.shareInfo.otherParam = `gapid=${res.data.mark_id}`;
          } else if (this.data.mode == "receiving"|| this.data.mode == "getgift") {
            this.setInfo(res.data.record.has_one_give, res.data.record.has_one_goods, res.data.record.has_many_receive);
            this.setData({
              receive_detail: res.data.detail,
              received_time: res.data.record.updated_at,
              ['goods.nums']: res.data.record.goods_num
            });
            if (res.data.record.status == 0) {
              this.data.shareInfo.otherParam = `gapid=${res.data.record.mark_id}`;
              this.setData({
                isReceived: false //显示分享按钮
              });
            }
            this.data.shareInfo.otherParam = `records_id=${this.options.records_id}`;
          } else if (this.data.mode == "give") {
            this.setInfo(res.data.record.has_one_give, res.data.record.has_one_goods, res.data.record.has_many_receive);
            this.setData({
              receive_detail: res.data.detail,
              received_time: res.data.updated_at,
              ['goods.nums']: res.data.record.goods_num
            });
            this.data.shareInfo.otherParam = `id=${this.options.id}`;
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1200,
          });
          // setTimeout(() => {
          //   wx.navigateBack({
          //     delta: 1
          //   })
          // }, 1500);
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  setInfo(_info = {}, _goods = {}, _received = {}) {
    this.setData({
      info: _info,
      goods: _goods,
      received_info: _received
    });
  },
  tuUrl(){
    wx.navigateTo({url: '/packageI/warehouseFarm/warehouseFarmGiftLog/warehouseFarmGiftLog?tag=1'});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    let _params = {};
    if(e&&e.from=="button"||this.options.mode == 'creat'){
      _params.title=this.data.shareInfo.title,
      _params.imageUrl= this.data.shareInfo.imgUrl,
      _params.path= '/packageI/warehouseFarm/warehouseFarmIndex/warehouseFarmIndex?' + this.data.shareInfo.otherParam;
    }else{
      _params.path= '/packageI/warehouseFarm/warehouseFarmGiftDetail/warehouseFarmGiftDetail?' + this.data.shareInfo.otherParam;
    }
    return _params;
  }
});