var http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:false,
    orderNumber:'',
    prodCommInfo: {
      orderNumber:'',
      pics: [],
      content: "",
      isAnonymous: 0,
      score: 5,
      deliveryPack: 5,
      deliveryService: 5,
      deliverySpeed: 5,
    },  //订单列表页参数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      orderNumber:options.orderNumber
    })
  },
  
  /**
   * 发表评论
   */
  submitComm: function (e) {

      let that = this
      var prodCommInfo = this.data.prodCommInfo;
      prodCommInfo.orderNumber = this.data.orderNumber
        if(prodCommInfo.content.trim()==""){
         return wx.showToast({
            title: '评价不能为空',
            icon:"none"
          })
        }else if(this.loading){
          return
        }
        this.loading = true
        wx.showLoading();
        var pics = '';
        prodCommInfo.pics.forEach(function(item){
          pics += item.path + ',';
        });
        if(pics!=''){
          pics = pics.substring(0,pics.length-1)
        }
        prodCommInfo.pics = pics

        // 发布评论
        var params = {
          url: "/p/prodComm",
          method: "POST",
          data: prodCommInfo,
          callBack:  (res) => {
            that.loading = true
            wx.hideLoading();
            if(res.code == 500){
              wx.showToast({
                title:res.msg,
                icon:'none'
              })
            }else{
              wx.navigateTo({
                url: '/pages/commResult/commResult',
              })
             
            }
          }
        };
        http.request(params);
      
  },


  /**
   * 上传图片
   */
  getUploadImg: function(e) {
    var ths = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({  
          mask: true
        })
        var params = {
          url: "/p/file/upload",
          filePath: tempFilePaths[0],
          name: 'file',
          callBack: function (res2) {
            wx.hideLoading();
            var img = {};
            img.path = JSON.parse(res2).filePath;
            img.url = JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath;
            var prodCommInfo = ths.data.prodCommInfo;
            prodCommInfo.pics.push(img);
            ths.setData({
              prodCommInfo: prodCommInfo
            })
          }
        };
        http.upload(params);
      }
    })
  },

  /**
   * 删除图片
   */
  removeImage(e) {
    const idx = e.target.dataset.idx
    var prodCommInfo = this.data.prodCommInfo;
    prodCommInfo.pics.splice(idx, 1)
    this.setData({
      prodCommInfo: prodCommInfo
    });
  },

  onContentInput:function(e){
    const index = e.target.dataset.index
    var prodCommInfo = this.data.prodCommInfo;
    prodCommInfo.content = e.detail.value;
    this.setData({
      prodCommInfo: prodCommInfo
    });
  },


  /**
  * 匿名评价
  * 每一项的选择事件
  */
  onSelectedItem: function (e) {
    var prodCommInfo = this.data.prodCommInfo;// 获取评论项
    var isAnonymous = prodCommInfo.isAnonymous; // 获取当前评价的选中状态
    if (isAnonymous==1){
      isAnonymous = 0;
    }else{
      isAnonymous = 1;
    }
    prodCommInfo.isAnonymous = isAnonymous; // 改变状态
    this.setData({
      prodCommInfo: prodCommInfo
    });
  },

  onStarChange:function(e){
    var val = e.detail.val;
    let key = e.currentTarget.dataset.key
    console.log(e);
    var prodCommInfo = this.data.prodCommInfo;
    prodCommInfo[key] = val;
    this.setData({
      prodCommInfo: prodCommInfo
    });
  },


  /**
   * 评价图片预览
   */
  comPicPreView(e){
    var idx = e.currentTarget.dataset.idx
    var urls = []
    this.data.prodCommInfo.pics.forEach(el => {
      urls.push(el.url)
    })
    wx.previewImage({
      current: urls[idx],
      urls: urls
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }



})