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

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

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myComment: [],
    loading: false,
    currentPage: 1,
    full_star_img: "../../../imgs/icon/full.png",
    empty_star_img: "../../../imgs/icon/empty.png",
    half_star_img: "../../../imgs/icon/half.png",
    imgServer
  },

  tapTodetail: function(e) {
    let {
      id,
      product,
      link
    } = e.currentTarget.dataset;
    console.log(e.currentTarget)
    if (product){
      wx.navigateTo({
        url: `../../../pages/wv_page/index?link=${escape(link)}&id=${id}`,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
    else{
      wx.navigateTo({
        url: `../../zl_detail/index?id=${id}`,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },

  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({
      serverName,
      noExhibitionImg,
      defaultImg,
      noCollectionImg 
    })
    this.getComment(1)
  },
  onPullDownRefresh: function() {
    this.setData({
      myComment: [],
    });
    this.getComment(1)
  },

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

  },

  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');
    }

  },

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

          // 判断是否为最后一页（返回空数组表示没有更多数据）
          let isLastPage = !myComment || myComment.length === 0;

          if (isLastPage) {
            this.setData({
              loading: false,
              lastPage: true // 标记为最后一页
            });
            wx.stopPullDownRefresh();
            return;
          }

          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 newMyComment = tempContent.concat(myComment);

          console.log('myComment', myComment)
          this.setData({
            currentPage: page,
            lastPage: false, // 有数据时设为false
            loading: false,
            myComment: newMyComment,
          });
          console.log(myComment)
          wx.stopPullDownRefresh();
        }

      }, err => {

      },
      complete => {

      })
  },

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

  },

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

  },

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

  },




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

  },

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

  }
})