// packageD/directSeeding/liveReport/liveReport.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    fileList: [],
    uploadUrl: 'upload.uploadPic',
    reportContentShow: true,
    room_id: "",
    liveReportData: '',
    radio: "1",
    desc: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        room_id: options.id
      });
    }
    this.getLiveReport();
  },
  descInp(e) {
    let val = e.detail.value;
    this.setData({
      "desc": val
    });
  },
  submit() {
    if (!this.data.desc) {
      wx.showToast({
        icon: 'none',
        title: '请填写问题描述',
        duration: 1500
      });
      return false;
    }
    if (this.data.fileList.length <= 0) {
      wx.showToast({
        icon: 'none',
        title: '请上传相关截图',
        duration: 1500
      });
      return false;
    }
    let that = this;
    let urlStr = app.getNetAddresss("plugin.room.frontend.live.report-add");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        room_id: this.data.room_id,
        report_type: Number(this.data.radio),
        desc: this.data.desc,
        image: this.data.fileList
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: '消息提交成功',
            duration: 1500
          });
          that.setData({
            reportContentShow: false
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  onChange(e) {
    let index = e.detail;
    this.setData({
      radio: index
    });
  },
  getLiveReport() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.room.frontend.live.report");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        room_id: this.data.room_id
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            liveReportData: res.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  returnBtn() {
    wx.navigateBack({
      delta: 1
    });
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  removeImg_1: function(e) {
    let index = e.currentTarget.dataset.index;
    let fileList = this.data.fileList;
    let infoArr = this.data.infoArr;
    fileList.splice(index, 1);
    this.setData({
      fileList: fileList,
      infoArr
    });
  },
  // 选择图片
  onRead_2() {
    if (this.data.upNumMaxOther == 4 || this.data.upNumMaxOther >= 4) {
      wx.showToast({
        icon: 'none',
        title: '图片数量已达到上限',
        duration: 1500
      });
      return false;
    }
    let that = this;
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        wx.compressImage({
          src: tempFilePaths[0], // 图片路径
          quality: 60, // 压缩质量
          success(e) {
            let photourl = e.tempFilePath;
            wx.uploadFile({
              url: urlStr,
              filePath: photourl,
              name: 'file',
              formData: {
                attach: 'upload'
              },
              success(resdata) {
                var res = JSON.parse(resdata.data);
                if (res.result == 1) {
                  let fileList = that.data.fileList;
                  fileList.push({
                    url: res.data.img_url
                  });
                  that.setData({
                    upNumMaxOther: that.data.upNumMaxOther + 1,
                    fileList: fileList
                  });
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: res.msg,
                    duration: 1500
                  });
                }
              }
            });
          },
          fail(e) {},
          complete(e) {}
        });
      }
    });
  },
});
