const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    imgUrl:[],
    uploadImg:[],
    fcontent:'',
    datas:{},
    submitData:{},
    act:'',
    atype:'',
  },
  onLoad(options) {
    let orderid = options.orderid;
    let type = options.otype;
    let act = options.act || '';
    this.getData(options.orderid,options.type,options.act)
  },
  getData(orderid,type,act){
    let userInfo = app.globalData.userInfo;
    let that = this;
    util.ajax({
      func:"v2/order/detail",
      data:{"orderid":orderid,"type":(type != undefined ? type : ""),}
    },function(res){
      if(res.code == 0){
        let atype = res.data.atype;
        let datas = res.data.comment;
        let uploadImg = datas.additionalComment.images || [];
        let fcontent = datas.additionalComment.content ? datas.additionalComment.content.replace(/<br\/>/g,'\n') : '';
        let submitData = {};
        submitData.aid = res.data.aid;
        submitData.orderid = res.data.orderid;
        submitData.cid = act == "edit" ? datas.additionalComment.cid : datas.cid;
        submitData.rid = userInfo ? userInfo['rid']:"";
        that.setData({uploadImg,fcontent,submitData,act,datas,atype})
      }else
        util.showTips(res.reason);
    })
  },
  submit(){
    var submitData = this.data.submitData,that = this;
    submitData['image'] = this.data.uploadImg.join(",");
    submitData['content'] = this.data.fcontent.replace(/\n/g,'<br/>');
    if(util.isEmpty(submitData.content)){
        util.showTips("请填写追加的评价。");
        return false;
    }
    util.ajax({
      func: (that.data.act == 'edit' ? "v2/comments/order/update" :"v2/comments/order/additional"),
        data:submitData,
        method: "POST",
        load:true,
        title:'提交中'
    },function(res){
      if(res.code == 0){
        util.showTips('评价提交成功');
        setTimeout(function(){
          wx.navigateBack();
        },2000)
      }else{
        util.showTips(res.reason);
      }
    });
  },
  chooseImg(){
    let that = this, uploadImg = that.data.uploadImg, count = 8;
    wx.chooseImage({
      count:count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'], 
      success: function (res) {
        var imgUrl = res.tempFilePaths;
        if (count - (imgUrl.length + uploadImg.length) < 0) {
          util.showTips("最多只能上传" + count + "张图片");
          return false;
        }
        wx.showLoading({ title: '上传中…' });
        let img = [];
        that.data.upImg = [];
        for(let i in imgUrl){//上传图片
          wx.uploadFile({
            url: util.config.apiServer + 'user/upload_img_app.do',
            filePath: imgUrl[i],
            name: 'avatarFile',
            formData:{"rid":app.globalData.userInfo['rid']},
            success: function(reason){
              let datas = JSON.parse(reason.data);
              if (reason.statusCode==200&&datas.code == 0) {
                img.push(datas.data.image);
                that.setData({ uploadCount: imgUrl.length, uploadImg, upImg: img });
                if (that.data.upImg.length == that.data.uploadCount) {
                  that.data.uploadImg = [...that.data.upImg, ...that.data.uploadImg];
                  that.setData({ uploadImg: that.data.uploadImg });
                  wx.hideLoading();
                }
              }
            }
          })
        }
      }
    })
  },
  delImg(e){
    let index = e.currentTarget.dataset.index,uploadImg = this.data.uploadImg;
    uploadImg.splice(index,1);
    this.setData({uploadImg})
  },
  bindblur(e){
    this.setData({fcontent:e.detail.value})
  }
})