// pages/comment/comment.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    has_many_order_goods: [],
    comments: [],
    btn_class: [],
    disabled: [],
    level: 5,
    iscomment: true,
    order: {},
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.order) {
      this.setData({
        order: JSON.parse(options.order ? options.order :
          '{}')
      });
      if (this.data.order.id) {
        this.setData({
          has_many_order_goods: this.data.order.order.has_many_order_goods
        });
        this.getData();
      }
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  getData() {
    for (var i = 0; i < this.data.has_many_order_goods.length; i++) {
      //初始化
      this.setData({
        ['comments[' + i + ']']: '',
        ['btn_class[' + i + ']']: 'mod_btn bg_2',
        ['disabled[' + i + ']']: false,
        ['has_many_order_goods[' + i + '].evaluatebol']: false
      });
    }
  },
  goEvaluate(e) {
    let evaluatebol = e.currentTarget.dataset.evaluatebol;
    let index = e.currentTarget.dataset.index;
    console.log(index);
    if (evaluatebol) {
      this.setData({
        ['has_many_order_goods[' + index + '].evaluatebol']: false
      });
    } else {
      this.setData({
        ['has_many_order_goods[' + index + '].evaluatebol']: true
      });
    }
  },
  getStar(event) {
    this.setData({
      level: event.detail
    });
  },
  commentsinp(e) {
    let val = e.detail.value;
    let index = e.currentTarget.dataset.index;
    this.setData({
      ['comments[' + index + ']']: val
    });
  },
  toComment(e) {
    let item = e.currentTarget.dataset.good;
    let index = e.currentTarget.dataset.goodindex;
    let that = this;
    if (this.data.comments[index].length == 0) {
      wx.showModal({
        title: '提示',
        content: '您还没有输入相关的评论内容',
        success(res) {
          if (res.confirm) {
            that.submitData(item, index);
          }
        }
      });
    } else {
      that.submitData(item, index);
    }
  },
  //提交数据到服务器
  submitData(item, index) {
    let urlStr = app.getNetAddresss('goods.comment.create-comment');
    urlStr += '&order_id=' + this.data.order.id;
    urlStr += '&goods_id=' + item.goods_id;
    urlStr += '&content=' + this.data.comments[index];
    urlStr += '&level=' + this.data.level;
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          this.setData({
            ['disabled[' + index + ']']: true,
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
});
