const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    datas: {},
    imgUrl: [],
    uploadImg: [],
    star: 0,
    cusStar: 0,// 客服
    leaderStar: 0,// 领队
    pgStar: 0, // 摄影师
    aid: '',
    title: '',
    orderid: '',
    type: '',
    act: '',
    content: '', //评价
    wordsLength: 0,//评价字数
    cid: '',
    one: [{
      txt: '带队技能欠佳',
      cet: false
    }, {
      txt: '与宣传有差距',
      cet: false
    }, {
      txt: '活动不合理',
      cet: false
    }, {
      txt: '孩子体验一般',
      cet: false
    }, {
      txt: '吃住有待升级',
      cet: false
    }, {
      txt: '性价比一般',
      cet: false
    }],
    two: [{
      txt: '正能量的小黄人',
      cet: false
    }, {
      txt: '领队专业细心',
      cet: false
    }, {
      txt: '教育意义明显',
      cet: false
    }, {
      txt: '活动安排有序',
      cet: false
    }, {
      txt: '安全措施到位',
      cet: false
    }, {
      txt: '性价比超值',
      cet: false
    }],
    chooseIndex: [],
    option: [],
    recommendArr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], //推荐值数组
    recomLevel: 0, //推荐值
  },
  onLoad(options) {
    let orderid = options.orderid,
      type = options.otype,
      act = options.act || '';

    this.setData({
      orderid,
      type,
      act
    });
    this.getData(orderid, type, act);
  },
  getData(orderid, type, act) {
    let that = this;
    util.ajax({
      func: "v2/order/detail",
      data: {
        "orderid": orderid,
        "type": (type != undefined ? type : "")
      }
    }, function (res) {
      if (res.code == 0) {
        let datas = res.data,
          star = 5,
          uploadImg = [],
          content = '',
          wordsLength = 0,
          option = [],
          options = that.data.two,
          cid = '',
          recomLevel = 0;
        if (!util.isObjEmpty(datas.comment)) {
          cid = datas.comment.cid;
          that.updateCommentData(datas.comment)
        }
        if (that.data.act != 'edit') {
          that.getlocalData();
        }
        that.setData({
          aid: datas.aid,
          cid,
          datas,
          title: datas.title,
        })
      } 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
    })
  },
  submit() {
    let that = this,
      data = {},
      uploadImg = this.data.uploadImg,
      act = this.data.act,
      userInfo = app.globalData.userInfo,
      star = this.data.star,
      content = this.data.content.replace(/\n/g, '<br/>'),
      option = this.data.two;
    if (star == 1 || star == 2) {
      option = this.data.one;
    }
    for (let i in option) {
      if (option[i].cet) {
        content = content + '#' + option[i].txt + '#'
      }
    }
    let pages = getCurrentPages(),
      prevPage = pages[pages.length - 2];
    if (prevPage.route == "pages/order/detail/index") {
      prevPage.data.query.otype = 0;
    }
    data['aid'] = this.data.aid;
    data['orderid'] = this.data.orderid;
    data['arating'] = star; //整体活动评分
    data['crating'] = this.data.cusStar; //客服评分
    data['ratingLeader'] = this.data.leaderStar; //领队评分
    data['ratingPhoto'] = this.data.pgStar; //摄影师评分
    data['outTradeNo'] = this.data.datas.outTradeNo; // 商户订单号
    data['content'] = content;
    data['cid'] = this.data.cid;
    data['image'] = uploadImg ? uploadImg.join(',') : '';
    data['rid'] = userInfo ? userInfo['rid'] : "";
    data['recomLevel'] = this.data.recomLevel;
    if (data.arating == 0 && this.data.datas.atype == 0) {
      util.showTips("尚未填写活动评分，请填写后再进行提交～");
      return false;
    }
    if (data.crating == 0 && this.data.datas.atype == 0) {
      util.showTips("请对于客服这次的服务打分。");
      return false;
    }
    if (data.ratingLeader == 0 && this.data.datas.atype == 0) {
      util.showTips("请对于领队这次的服务打分。");
      return false;
    }
    if (data.ratingPhoto == 0 && this.data.datas.atype == 0) {
      util.showTips("请对于摄影师这次的服务打分。");
      return false;
    }
    if (data.recomLevel <= 0 && this.data.datas.atype == 0) {
      util.showTips("请点击数字对本次活动体验进行打分。");
      return false;
    }
    if (util.isEmpty(data.content)) {
      util.showTips(this.data.datas.atype == 0 ? "请填写本次活动体验后的评价。" : '请填写对此商品的评价。');
      return false;
    }
    util.ajax({
      func: (act == 'edit' ? "v2/comments/order/update" : "v2/comments/order/addcomment"),
      data: data,
      method: "POST",
      load: true,
      title: '提交中'
    }, function (res) {
      if (res.code == 0) {
        if (act == 'edit' || that.data.datas.atype == 1) {
          wx.navigateBack();
        } else {
          wx.redirectTo({
            url: '/pages/order/scratch/scratch?orderid=' + that.data.orderid + '&coin=' + res.data || 0
          })
        }
      } else {
        util.showTips(res.reason);
      }
    });
  },
  // 获取的评论数据整理公共方法
  updateCommentData(commentData) {
    let star = 5,
      cusStar = 5,
      leaderStar = 5,
      pgStar = 5,
      uploadImg = [],
      content = '',
      wordsLength = 0,
      option = [],
      options = this.data.two,
      recomLevel = 0;
    star = commentData.arating;
    cusStar = commentData.crating;
    leaderStar = commentData.leaderStar;
    pgStar = commentData.pgStar;
    recomLevel = commentData.recomLevel;
    uploadImg = commentData.images || [];
    content = commentData.content.replace(/#([^#]+)#/g, "").replace(/<br\/>/g, '\n');
    option = commentData.content.match(/#([^#]+)#/g);
    star >= 3 || star == 0 ? options = this.data.two : options = this.data.one;
    for (let i in option) {
      option[i] = option[i].replace(/#/g, "");
      for (let k in options) {
        if (option[i] == options[k].txt) {
          options[k].cet = true
        }
      }
    }
    if (star >= 3 || star == 0) {
      this.setData({
        two: options
      })
    } else {
      this.setData({
        one: options
      })
    }
    this.setData({
      star,
      cusStar,
      leaderStar,
      pgStar,
      uploadImg,
      content,
      wordsLength: content.length,
      recomLevel
    })
  },
  // 获取本地草稿数据
  getlocalData() {
    if (!util.isObjEmpty(wx.getStorageSync("orderComment"))) {
      let localData = wx.getStorageSync("orderComment");
      if (localData.orderid == this.data.orderid) {
        this.updateCommentData(localData)
      }
    }
  },
  // 保存草稿至本地
  saveDataToLocal() {
    let that = this,
      commentData = {},
      option = this.data.two;
    if (!util.isEmpty(this.data.content) || this.data.uploadImg.length > 0) {
      commentData.orderid = this.data.orderid;
      commentData.saveDate = util.getNowFormatDate('yyyy-MM-dd hh:mm:ss');
      commentData.arating = this.data.star;
      commentData.crating = this.data.cusStar;
      commentData.leaderStar = this.data.leaderStar;
      commentData.pgStar = this.data.pgStar;
      commentData.recomLevel = this.data.recomLevel;
      commentData.images = this.data.uploadImg || '';
      let content = this.data.content.replace(/\n/g, '<br/>');
      if (this.data.star == 1 || this.data.star == 2) {
        option = this.data.one;
      }
      for (let i in option) {
        if (option[i].cet) {
          content = content + '#' + option[i].txt + '#'
        }
      }
      commentData.content = content;
      wx.setStorageSync("orderComment", commentData);
    }
  },
  chooseOpt: function (e) {
    var star = this.data.star,
      chooseIndex = this.data.chooseIndex,
      index = e.currentTarget.dataset.index,
      option = this.data.two;
    if (star == 1 || star == 2) {
      option = this.data.one;
    }
    if (option[index].cet) {
      option[index].cet = false;
    } else {
      option[index].cet = true;
    }
    if (star == 1 || star == 2) {
      this.setData({
        one: option
      });
    } else {
      this.setData({
        two: option
      });
    }
  },
  bindblur: function (e) {
    this.setData({
      content: e.detail.value,
      wordsLength: e.detail.value.length
    });
  },
  bindstar: function (e) {
    if (e.currentTarget.dataset.type == 'star') {
      if (this.data.star == 0 || !this.data.star) {
        this.setData({
          cusStar: e.target.dataset.index,
          leaderStar: e.target.dataset.index,
          pgStar: e.target.dataset.index,
        });
      }
      this.setData({
        star: e.target.dataset.index
      });
    } else if (e.currentTarget.dataset.type == 'leaderStar') {
      this.setData({
        leaderStar: e.target.dataset.index
      });
    } else if (e.currentTarget.dataset.type == 'pgStar') {
      this.setData({
        pgStar: e.target.dataset.index
      });
    } else {
      this.setData({
        cusStar: e.target.dataset.index
      });
    }
  },
  recommend(e) {
    this.setData({
      recomLevel: e.target.dataset.number
    })
  },
  onUnload: function () {
    if (this.data.datas.atype == '0') {
      this.saveDataToLocal()
    }
  },
  onHide() {
    if (this.data.datas.atype == '0') {
      this.saveDataToLocal()
    }
  }
})