const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    imgUrl: [],
    uploadImg: [],
    fcontent: '',
    wordsLength: 0,//评价字数
    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 wordsLength = fcontent.length;//评价字数
        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,
          wordsLength,
          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 - uploadImg.length,
      img = [];
    wx.chooseMedia({
      count: count,
      mediaType: ['image'],
      sizeType: ['compressed', 'compressed'], //可以指定是原图还是压缩图
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        let imgUrl = res.tempFiles
        if ((imgUrl.length + that.data.uploadImg.length) > 8) {
          util.showTips("最多只能上传8张照片");
          return false;
        }
        res.tempFiles.forEach(item => {
          let avatarUrl = item.tempFilePath
          wx.showLoading({
            title: '上传中…'
          });
          util.uploadFile("oss/policy", avatarUrl, {
            type: 'baoBeiEvaluate'
          }, true).then((res) => {
            if (res.code == 0) {
              uploadImg.push(res.url);
              img.push(res.url);
              if (img.length == imgUrl.length) {
                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,
      wordsLength: e.detail.value.length
    })
  }
})