// pages/opinion/index.js
const app = getApp();
import { customRequest } from '../../utils/request.js';
import { uploadImg } from "../../utils/commonUtil.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    textVal: "",
    loading: false,
    subLoad: false,
    success: false,
    troubleList: ["投诉", "建议", "故障", "其他"],
    troubleIndex: 0,
    imgList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () { },
  /**
   * 意见提交事件
   *
   *
   */
  submit () {
    const that = this;
    if (that.data.subLoad) return
    that.data.subLoad = true
    if (that.data.textVal.trim() != '') {
      console.log(312313123);
      that.setData({
        loading: true,
      });
      customRequest("/daotian/feedback/add", {
        header: 'headers', method: 'post', data: {
          content: that.data.textVal, classify: that.data.troubleIndex + 1,
          imgs: that.data.imgList.join(","),
        }
      }).then((res) => {
        console.log(res);
        if (res.data.status == 200) {
          that.setData({
            success: true,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
        that.setData({
          loading: false,
        });
        that.data.subLoad = false

      }).catch(() => {
        that.setData({
          loading: false,
        });
      })
    } else {
      wx.showToast({
        title: '请输入内容再提交',
        icon: 'none'
      })
      that.data.subLoad = false
    }


  },
  getVal (e) {
    console.log(e);
    this.setData({
      textVal: e.detail.value
    })
    console.log(this.data.textVal);
  },
  uploadImg () {
    let that = this
    uploadImg(4, (res) => {
      console.log(res);
      if (res.status == 200 && that.data.imgList.length < 4) {
        if (that.data.imgList.length > 4) {
          wx.showToast({
            title: '最多上传4张图片',
          })
        } else {
          that.data.imgList.push(res.data)
        }
      }
      that.setData({
        imgList: that.data.imgList
      })
    })
  },
  delImg (e) {
    let ind = e.currentTarget.dataset.ind
    this.data.imgList.splice(
      this.data.imgList.findIndex((item, index) => index == ind),
      1
    );
    this.setData({
      imgList: this.data.imgList
    })
  },
  goback () {
    wx.navigateBack({
      delta: 1,
    });
  },
  goList () {
    wx.navigateTo({
      url: "/pages/opinionList/index",
    });
  },
  changeType (e) {
    this.setData({
      troubleIndex: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});
