// pages/Information/information_article/information_article.js
const app = getApp(),
  { hostname, business_id, page, openid } = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    details: {},
    inputValue: '',
    commentList: [],
    num: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //接收上个页面传来的id
    var id = options.id;
    //对id进行更新
    this.setData({
      id: id
    })
    var that = this;
    this.getDetails(that);
    this.getCommentList(that);
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/index/index?u_openid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid;  //当前页面url
        console.log(url, '/pages/index/index?u_openid=' + userid);
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },
  //获取页面信息
  getDetails(that) {
    var id = that.data.id;
    wx.request({
      url: hostname + 'Zixun/detail',
      data: { id, business_id },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: '',
      success: function (res) {
        if (res.statusCode == 200) {
          let details = {};
          details = res.data.data;
          details.content = details.content.replace(/<img src="/g, `<img style="width:90vw;" src="https://jisu.shenmikj.com/`)
          that.setData({
            details: details
          })

        } else {
          that.setData({
            details: {}
          })
        }
      }
    })
  },
  //获取评论列表
  getCommentList(that) {
    var zixun_id = that.data.id;
    wx.request({
      url: hostname + 'Zixun/comment_list',
      data: { zixun_id, business_id, page },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success(res) {
        console.log(res);
        if (res.data.status == 1) {
          var commentList = [];
          var commentListData = res.data.data;
          // console.log(InformationListData);
          if (commentListData.length != undefined) {
            for (var i = 0; i < commentListData.length; i++) {
              commentList.push(commentListData[i]);
            }
          }
          // console.log(InformationList,11);
          that.setData({
            commentList: commentList,
            num: commentListData.length
          })
        } else {
          that.setData({
            commentList: []
          })
        }
      }
    })
  },
  //得到输入框的值
  getValue(e) {
    var inputValue = e.detail.value;
    this.setData({
      inputValue
    })
  },

  //发送评论
  submit(e) {
    var that = this;
    var zixun_id = this.data.id;
    var content = this.data.inputValue;
    var openid = wx.getStorageSync('openId');
    console.log(zixun_id, content, openid)
    wx.request({
      url: hostname + 'Zixun/comment',
      data: { business_id, zixun_id, content, openid },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: (res) => {
        that.setData({
          inputValue: ''
        })
        that.getCommentList(that);
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 1000,
          mask: true,
        })
      }
    })
  }
})