// pages/zl_detail/create_evaluation/index.js
const {
  request,
  serverName
} = require('../../../utils/services');
// const WxParse = require('../../../common/component/wxParse/wxParse.js');
const wxStar = require('../../../common/component/wxStar/wxStar/wxStar.js');

const {
  Toast
} = require('../../../utils/util.js');
const {
  defaultImg
} = require('../../../utils/images');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    full_star_img: "../../../imgs/icon/full.png",
    currentStar: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wxStar.wxStar(this, 0, true);

    let currentStar = this.wxStarCont();
    this.setData({
      defaultImg,
      exhibitionId: options.id,
      serverName,
      currentStar
    })

  },
  starChangeCb: function() {
    let currentStar = this.wxStarCont();
    this.setData({
      currentStar
    })
  },
  clearComment: function() {
    this.setData({
      content: ""
    });
  },

  getContent: function(e) {
    this.setData({
      content: e.detail.value || ""
    });
    console.log('change', e.detail.value);

  },
  submit: function() {
    let {
      content,
      exhibitionId
    } = this.data;

    let currentStar = this.wxStarCont() / 2;

    let loginSessionKey = wx.getStorageSync('token') || "";

    if (content && currentStar) {
      request["writtenComments"]({
        comment: content,
        exhibitionId,
        score: currentStar,
        loginSessionKey
      }, "post", res => {
        if (res.data.code > -1) {
          Toast.showToast('tip', "评论成功", success => {
            wx.navigateBack({
              url: `../../../pages/zl_detail/index?id=${exhibitionId}`,
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          })
          // wx.navigateBack({
          //   delta: 1,
          // })
        }
      }, err => {
        // wx.showToast({
        //   title: '评论失败',
        //   icon: 'none',
        //   duration: 2000
        // })
      }, complete => {
        
      })
    } else if (currentStar){
      Toast.showToast('tip', "写几句短评吧", success => {
        
      })
    }else{
      Toast.showToast('tip', "给看展打个分吧", success => {

      })
    }






  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})