// packageH/article/periodicalDetail/periodicalDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    contenTapMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == "mid") {
            app._setMid(chil_arr[1]);
          }
          if (chil_arr[0] == "id") {
            this.setData({
              id: chil_arr[1]
            });
          }
        }
      }
    }
    wx.setNavigationBarTitle({
      title: '期刊杂志'
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages
      });
      this.getMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: this.data.dinfo.title ,
      path: "/packageH/article/periodicalDetail/periodicalDetail?id=" + this.data.id + "&mid=" + mid,
      imageUrl: this.data.dinfo.cover,
    };
  },
  tapMoreIcon(e) {
    let key = e.currentTarget.dataset.key;
    console.log(key);

    this.setData({
      contenTapMore: key == '1' ? false : true
    });
  },
  goHome(e) {
    wx.reLaunch({
      url: '/packageH/article/periodicalIndex/periodicalIndex',
    });
  },
  goWatch(e) {
    console.log(e.currentTarget.dataset.ind);
    let ind = e.currentTarget.dataset.ind;
    wx.navigateTo({
      url: '/packageH/article/periodicalWatch/periodicalWatch?id=' + this.data.id + '&ind=' + ind,
    });
  },
  getData() {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    let urlStr = app.getNetAddresss('plugin.journal.frontend.journal.directory');
    app._postNetWork({
      url: urlStr,
      data: {
        journal_id: that.data.id,
        page: that.data.page
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          console.log(res);
          that.setData({
            datas: res.data.directory.data,
            info: res.data,
            dinfo: res.data.info,
            current_page: res.data.directory.current_page,
            last_page: res.data.directory.last_page,
            contenTapMore: res.data.info.overview.length >= 100 ? false : true
          });
        } else {
          console.log(res);
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getMore() {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    let urlStr = app.getNetAddresss('plugin.journal.frontend.journal.directory');
    app._postNetWork({
      url: urlStr,
      data: {
        journal_id: that.data.id,
        page: that.data.page
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          that.setData({
            datas: that.data.datas.concat(res.data.directory.data),
            current_page: res.data.directory.current_page,
            last_page: res.data.directory.last_page
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
});