// pages/user/my_comment/index.js
const {
  request,
  serverName,
  imgServer
} = require('../../../utils/services');
const {
  defaultImg,
  noExhibitionImg
} = require('../../../utils/images');

const {
  Toast
} = require('../../../utils/util.js');

Page({

  /** 
   * 页面的初始数据
   */
  data: {
    myComment: [],
    loading: false,
    currentPage: 1,
    imgServer,
    full_star_img: "../../../imgs/icon/full.png",
    empty_star_img: "../../../imgs/icon/empty.png",
    half_star_img: "../../../imgs/icon/half.png",
    exhibitionId: "",
    sortType: 0,
    tapNav: [{
      name: '热门',
      sortType: 0
    }, {
      name: '时间',
      sortType: 1
    }]
  },
  fix_starImg: function (points) {
    let strPoints = String(Number(points).toFixed(2));
    let show_Arr = [];

    let ten = strPoints.split('.')[0] || 0;
    let unit = strPoints.split('.')[1] || 0;
    let surPlus = 0;
    if (strPoints < 0) {
      return
    }
    if (ten) {
      for (let i = 0; i < Number(ten); i++) {
        show_Arr.push({
          'img': this.data.full_star_img
        })
      }
      surPlus = 5 - Number(ten);
    }

    if (unit && surPlus > 0) {
      let numUnit = Number(unit.substr(0, 1)) || 0
      if (numUnit > 0) {
        // let fix_unit = Math.round(numUnit);
        // switch (true) {
        //   case numUnit > 5:
        //     show_Arr.push({
        //       'img': this.data.half_star_img
        //     })
        //     break;
        //   case numUnit <= 5:
        //     show_Arr.push({
        //       'img': this.data.half_star_img
        //     })
        //     break;
        //   default:
        //     break
        // }
        show_Arr.push({
          'img': this.data.half_star_img
        })
      } else if (numUnit == 0) {
        show_Arr.push({
          'img': this.data.empty_star_img
        })
      } else {
        return
      }
    }
    if (surPlus > 0) {
      for (let i = 0; i < surPlus - 1; i++) {
        show_Arr.push({
          'img': this.data.empty_star_img
        })
      }
    }

    return show_Arr
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      defaultImg,
      exhibitionId: options.id,
      serverName
    })
    this.getComment(1)
  },

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

  },

  addCommentLike: function(e) {
    let {
      type,
      id,
      idx
    } = e.currentTarget.dataset;
    console.log(idx, type, id)
    let {
      myComment
    } = this.data;

    Toast.showToast2('loading');

    let loginSessionKey = wx.getStorageSync('token') || "";
    // if (loginSessionKey){
    request['commentLike']({
      loginSessionKey,
      commentId: id,
      type: Number(type),
    }, "post", res => {
      if (res.data.code > -1) {
        console.log("asdw", myComment)
        myComment[idx].hasLike = res.data.data.hasLike
        if (res.data.data.hasLike) {
          myComment[idx].likeCount += 1;
        } else {
          myComment[idx].likeCount -= 1;

        }
        this.setData({
          myComment: myComment
        })
      }

    }, err => {

    }, complete => {

      Toast.hideLoading();
    })
    // }


  },

  loadMore: function() {
    if (!this.data.lastPage) {
      console.log(this.data.currentPage + 1)
      this.getComment(this.data.currentPage + 1);
    } else {
      return;
    }
  },

  onReachBottom: function() {
    if (!this.data.loading) {
      this.loadMore();
      console.log('reach Bottom');
    }

  },
  tapToComment: function() {
    let {
      exhibitionId
    } = this.data;
    wx.navigateTo({
      url: `../../zl_detail/create_evaluation/index?id=${exhibitionId}`,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },


  getComment: function(page) {
    let loginSessionKey = wx.getStorageSync("token") || "";
    let {
      exhibitionId,
      sortType
    } = this.data
    request["getCommentslist"]({
        exhibitionId,
        loginSessionKey,
        page: page,
        sortType
      }, "", res => {
        if (res.data.code > -1) {
          let tempContent = this.data.myComment ?
            this.data.myComment :
            [];

          let {
            exhibition,
            comments,
            score
          } = res.data.data
          let {
            last: lastPage,
            totalPages,
            content: myComment
          } = comments;

          if (myComment){
            for (let i = 0; i < myComment.length; i++) {
              let comments_star = this.fix_starImg(myComment[i].score || '0.0')
              myComment[i]['imgObj'] = comments_star
            }
          }

            let score_img = this.fix_starImg(score)
            console.log(score_img)
          // console.log(res)
          this.setData({
            currentPage: res.data.data.number + 1,
            lastPage,
            loading: false,
            myComment: tempContent.concat(myComment),
            exhibition,
            score_img,
            score
          });
          console.log(myComment)
          wx.stopPullDownRefresh();
        }

      }, err => {

      },
      complete => {

      })
  },
  changeNav: function(e) {
    let {
      type
    } = e.currentTarget.dataset;
    this.setData({
      sortType: type,
      myComment: []
    })
    this.getComment(1)
    // console.log(e)
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})