// pages/comments/comments.js
const getReq = require('../../config.js').getReq
import recordSource from '../../utils/recordSource'
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comments: [],
    hasmore: '',
    curpage: 1,
    common_id: '',
    addtimes: [],
    show_carousel: false,
    carouselImgs: [],
    current: 0,
    basicStar: './../image/basic_star.png',
    activeStar: '../../image/star.png',
    halfStar: '../../image/half_star.png',
    fromSource: '',
    firstLoad: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      common_id: options.common_id
    })
    this.getDatas(options.common_id, this.data.curpage)
  },
  getDatas(common_id, curpage) {
    let fromSource = recordSource(app, `act=goods_common&op=comments&goods_commonid=${common_id}&curpage=${curpage}&page=10&client_type=mini`)
    this.setData({
      fromSource: app.globalData.fromSource
    })
    wx.showLoading({
      title: '加载中'
    })
    var self = this
    getReq(app,{
      act: 'goods_common',
      op: 'comments',
      goods_commonid: common_id,
      curpage,
      page: 10,
      from: fromSource
    }, function (res) {
      if (res.code == 200) {
        wx.hideLoading()
        let comments = self.createComments(res.datas.comments)
        self.setData({
          comments: self.data.comments.concat(comments),
          hasmore: res.datas.mobile_page.hasmore,
          firstLoad: false
        })
      }
      else {
        wx.showToast({
          icon: 'none',
          title: res.message,
          duration: 2000
        })
      }
    })
  },
  getAddTimes(comments) {
    let arr = []
    comments.map(item => {
      let addtime = item.addtime || ''
      if (addtime) {
        let date = new Date(addtime * 1000);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        addtime = `${year}-${month}-${day}`
      }
      arr.push(addtime)
    })
    this.setData({
      addtimes: this.data.addtimes.concat(arr)
    })
  },
  createComments(basicComments) {
    let self = this
    let comments = []
    comments = basicComments.map(item => {
      let addtime = item.addtime || ''
      if (addtime) {
        let date = new Date(addtime * 1000);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let transform_addtime = `${year}-${month}-${day}`
        item['transform_addtime'] = transform_addtime
      }
      let starSrcs = self.createStars(item.scores)
      item['starSrcs'] = starSrcs
      return item
    })
    return comments
  },
  createStars(rate) {
    let starSrcs = []
    for(let i = 0; i < 5; i++) {
      if(i < Math.floor(rate)) {
        starSrcs.push(this.data.activeStar)
        continue
      }
      if(i < Math.round(rate)) {
        starSrcs.push(this.data.halfStar)
        continue
      }
      starSrcs.push(this.data.basicStar)
    } 
    return starSrcs
  },
  showCarousel(e) {
    let carouselImgs = e.currentTarget.dataset.images
    let current = e.currentTarget.dataset.current
    this.setData({
      show_carousel: true,
      carouselImgs,
      current
    })
  },
  hideCarousel() {
    this.setData({
      show_carousel: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!this.data.firstLoad) {
      app.setFromSource(this.data.fromSource)
    }
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasmore) {
      return
    }
    this.data.curpage++
    this.getDatas(this.data.common_id, this.data.curpage) 
  }
})