// mycomponent/yz_commentInput/yz_commentInput.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //弹窗是否显示，默认不显示
    showCommentInput: {
      type: Boolean,
      value: false
    },
    openImg: {
      type: Boolean,
      value: false
    },
    options: {
      type: Object,
      value: () => ({})
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    fileList1: [],
    imgList: [],
    maincomment: "",
    prop_placeholder: "请留下你的精彩评论吧"
  },
  observers: {
    showCommentInput: function (show) {
      this.setData({
        maincomment: '',
        fileList1: [],
        imgList: [],

      });
      console.log(show, ";sdasdasd");
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event) {
      this.setData({
        maincomment: event.detail
      });
    },
    subMainBtn() {
      if (this.data.maincomment.length == 0 && this.data.fileList1.length == 0) {
        wx.showToast({
          title: '不能发送空白信息',
          icon: 'error'
        });
        return;
      }
      let _json = {
        text: this.data.maincomment,
        imgs: this.data.fileList1
      };
      if (this.data.options) {
        _json.options = this.data.options;
      }
      this.triggerEvent('confirm', _json);
      this.setData({
        showCommentInput: false
      });
    },
    closedPopup() {
      this.setData({
        showCommentInput: false
      });
    },
    updateImages() {
      // console.log(that.data.fileList1);
      // this.triggerEvent('updateImages', this.data.fileList1);
    },
    //预览图片，放大预览
    preview(e) {
      let currentUrl = e.currentTarget.dataset.src;
      wx.previewImage({
        current: this.data.fileList1[currentUrl], // 当前显示图片的http链接
        urls: this.data.fileList1 // 需要预览的图片http链接列表
      });
    },
    delIntu_1(e) {
      let del = e.currentTarget.id;
      this.data.fileList1.splice(del, 1);
      this.setData({
        fileList1: this.data.fileList1
      });
      this.updateImages();
    },
    onRead_1() {
      let that = this;
      let ig = '1';
      wx.chooseImage({
        count: this.properties.max_count,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // console.log(res.tempFilePaths);
          that.unload({
            tempFilePaths: res.tempFilePaths
          }, ig);
        }
      });
    },
    //多张上传方法
    unload(data, ig) {
      let urlStr = app.getNetAddresss("upload.uploadPic");
      var that = this,
        i = data.i ? data.i : 0,
        success = data.success ? data.success : 0,
        fail = data.fail ? data.fail : 0;
      wx.showLoading({
        title: '正在上传',
      });
      wx.uploadFile({
        url: urlStr,
        filePath: data.tempFilePaths[i],
        name: 'file',
        formData: null,
        success(resdata) {
          var res = JSON.parse(resdata.data);
          // console.log(res.data.img_url);
          if (ig == '1') {
            that.data.fileList1.push(res.data.img_url);
            that.setData({
              fileList1: that.data.fileList1,
            });
            // console.log(that.data.fileList1);
            that.updateImages();
          }
        },
        fail(e) {
          fail++;
        },
        complete(e) {
          wx.hideLoading();
          i++;
          if (i == data.tempFilePaths.length) { //当图片传完时，停止调用
            console.log('执行完毕');
            console.log('成功：' + success + " 失败：" + fail);
          } else { //若图片还没有传完，则继续调用函数
            data.i = i;
            data.success = success;
            data.fail = fail;
            that.unload(data, ig); //递归，回调自己
          }
        }
      });

    },
  }
});