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

  /**
   * 页面的初始数据
   */
  data: {
    heightTop: '',
    show: '',
    loading: false,
    service_mobile: '',
    service_QRcode: '',
    severShow: false,
    page: 1,
    startMove: false,
    touchind: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      this.setData({
        id: options.id,
        ind: options.ind
      });
    }
    if (options.ind == 0) {
      this.setData({
        touchind: '-1'
      });
    } else {
      this.setData({
        touchind: ''
      });
    }
    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]
            });
          }
          if (chil_arr[0] == "ind") {
            this.setData({
              ind: chil_arr[1]
            });
          }
        }
      }
    }
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  toAdv: function (e) {
    console.log('跳转到广告', e);
    let link = e.currentTarget.dataset.link;
    if (!app._isTextEmpty(link) && app._isTextEmpty(link)) {
      return;
    }
    try {
      wx.navigateTo({
        url: link,
      });
    } catch (error) {
      wx.reLaunch({
        url: '/packageG/index/index',
      });
    }
  },
  touchend(e) {
    console.log('结束111111111111111111', this.data.ind, e);
    let clientX_end = e.changedTouches[0].clientX;
    let clientX_start = this.data.clientX_start;
    if (!this.data.touchind) {
      this.setData({
        touchind: '-1'
      });
      console.log('到达第一个轮播');
      return;
    } else {
      if (this.data.touchind == '-1') {
        if (this.data.ind == 0) {
          if (clientX_start < clientX_end) {
            console.log("左滑了");
            wx.navigateTo({
              url: '/packageH/article/periodicalDetail/periodicalDetail?id=' + this.data.id,
            });
          }
        }

      }
    }


  },
  touchstart(e) {
    this.setData({
      clientX_start: e.changedTouches[0].clientX
    });

  },
  bindchangeTap(e) {
    let that = this;
    let inds = e.detail.current;
    this.setData({
      ind: inds,
      // touchind:''
    });
    wx.createSelectorQuery().selectAll('.articleBox').boundingClientRect(function (rect) {
      that.setData({
        heightTop: rect[inds].height + 'px'
      });
    }).exec();
  },
  onReady: function () {},
  readyAll() {
    let that = this;
    setTimeout(() => {
      wx.createSelectorQuery().selectAll('.articleBox').boundingClientRect(function (rect) {
        console.log(rect, '111111111');
        that.setData({
          heightTop: rect[that.data.ind].height + 'px'
        });
        console.log(rect[that.data.ind].height);
      }).exec();
    }, 1000);
  },
  shareTap(e) {
    this.setData({
      show: true
    });
  },
  shareTapCancel(e) {
    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: this.data.article_data[this.data.ind].has_one_article.title,
      path: "/packageH/article/periodicalWatch/periodicalWatch?id=" + this.data.id + "&ind=" + this.data.ind + "&mid=" + mid,
      imageUrl: this.data.article_data[this.data.ind].has_one_article.thumb ? this.data.article_data[this.data.ind].has_one_article.thumb : "",
    };
  },
  tapLIst(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageH/article/periodicalDetail/periodicalDetail?id=' + id,
    });
  },
  toGoBtn(e) {
    console.log(e);
    let key = e.currentTarget.dataset.key;
    if (key == "home") {
      wx.reLaunch({
        url: '/packageH/article/periodicalIndex/periodicalIndex',
      });
    } else if (key == "list") {
      wx.redirectTo({
        url: '/packageH/article/periodicalDetail/periodicalDetail?id=' + this.data.id,
      });
    }
  },
  severShowTap() {
    this.setData({
      severShow: true
    });
  },
  closePop() {
    this.setData({
      severShow: false
    });
  },
  getData() {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    let urlStr = app.getNetAddresss('plugin.journal.frontend.journal.detail');
    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({
            article_data: res.data.article_data,
            before_journal: res.data.before_journal.data,
            last_page: res.data.before_journal.last_page,
            current_page: res.data.before_journal.current_page,
            code: res.data.customer_service.code
          });
          if (res.data.customer_service.code == '1') {
            that.setData({
              service_QRcode: res.data.customer_service.data.service_QRcode,
              service_mobile: res.data.customer_service.data.service_mobile,
            });
            console.log(that.data.service_QRcode, that.data.service_mobile);
          }
          if (res.data.before_journal.current_page >= res.data.before_journal.last_page) {
            that.setData({
              loading: false
            });
          } else {
            that.setData({
              loading: true
            });
          }
          // let article_data = that.data.article_data;
          // for (let i = 0; i < article_data.length; i++) {
          //   try {
          //     article_data[i].has_one_article.content = article_data[i].has_one_article.content.replace(RegExp('<img style=', 'g'), '<img style="width:100%;height:auto !important;"');
          //   } catch (error) {
          //     console.log('1111111111111111');
          //   }
          // }
          // console.log(article_data);
          // that.setData({
          //   article_data: article_data
          // });
          that.readyAll();
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  taploadMore() {
    console.log(this.data.current_page, this.data.last_page);
    if (this.data.current_page >= this.data.last_page) {
      console.log('没有更多');
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages
      });
    }
    console.log(this.data.page);
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    let urlStr = app.getNetAddresss('plugin.journal.frontend.journal.detail');
    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({
            before_journal: that.data.before_journal.concat(res.data.before_journal.data),
            last_page: res.data.before_journal.last_page,
            current_page: res.data.before_journal.current_page,
          });
          if (res.data.before_journal.current_page >= res.data.before_journal.last_page) {
            that.setData({
              loading: false
            });
          } else {
            that.setData({
              loading: true
            });
          }
          that.readyAll();
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getPoste() {
    wx.showLoading({
      title: '加载中',
    });
    let that = this;
    let id = this.data.article_data[this.data.ind].id;
    let urlStr = app.getNetAddresss('plugin.journal.frontend.create-new-poster.createPoster');
    app._postNetWork({
      url: urlStr,
      data: {
        journal_article_id: id,
        article_key: this.data.ind
      },
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          that.setData({
            show: false,
            posterImg: res.data,
            postShow: true
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  checkImg() {
    let arr = [];
    arr.push(this.data.posterImg);
    wx.previewImage({
      urls: arr,
    });
  },
  onClose() {
    this.setData({
      postShow: false
    });
  }
});