// pages/index/suggestion/addsuggestion/addsuggestion.js
import HTTP from '../../../../../requestFn/Api'
const app = getApp();
const {
  baseURL
} = app.globalData;
Page({
  data: {
    garden_id: null,
    type_list: [{
      name: "投诉",
      value: 1
    }, {
      name: "建议",
      value: 2
    }],
    type: '',
    valueLength: 0, 
    files: [],
    apply_pic: [],
  },
  onLoad(options) {
    const that = this;
    const {
      garden_id
    } = app.globalData;
    that.setData({
      garden_id
    })
  },
  getType(e) {
    this.setData({
      type: e.detail.value
    })
  },
  submit(e) {
    const that = this;
    const {
      type,
      apply_people,
      apply_people_phone,
      apply_content
    } = e.detail.value;
    const {
      garden_id,
      apply_pic,
      type_list
    } = that.data;
    if (type == "") {
      wx.showToast({
        title: '请选择分类',
        icon: "error",
      })
      return false
    }
    if (apply_people == "") {
      wx.showToast({
        title: '请输入报修人',
        icon: "error",
      })
      return false
    }
    if (apply_people_phone == "") {
      wx.showToast({
        title: '请输入联系电话',
        icon: "error",
      })
      return false
    }
    if (!/^1[345789]\d{9}$/.test(apply_people_phone)) {
      wx.showToast({
        title: "联系电话有误",
        duration: 2000,
        icon: "error",
      });

      return false;
    }
    if (apply_content == "") {
      wx.showToast({
        title: '请输入问题',
        icon: "error",
      })
      return false
    }
    const suggestion_type = type_list[type].value;
    HTTP.Suggestion({
      method: 'post',
      garden_id,
      apply_people,
      apply_people_phone,
      apply_content,
      apply_pic,
      suggestion_type
    }).then(res => {
      if (res.code == 0) {
       setTimeout(()=>{
        wx.showToast({
          title: '提交成功',
          icon: "success",
          success() {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            },1000)
          }
        })
       },500)
      }
    })
  },
  getTextLength(e) {
    const valueLength = e.detail.value.length;
    this.setData({
      valueLength
    })
  },
  chooseImage() {
    const that = this;
    wx.chooseMedia({
      mediaType: ["image"],
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const userInfo = wx.getStorageSync('userInfo')
        const pic = res.tempFiles.map(item => {
          wx.uploadFile({
            filePath: item.tempFilePath,
            name: 'file',
            url: baseURL + '/upload',
            header: {
              "token": userInfo.token
            },
            success(res) {
              const result = that.data.apply_pic;
              result.push(JSON.parse(res.data).data)
              that.setData({
                apply_pic: result
              })
            },
            fail(res) {
              console.log(res);
            }
          })
          return item.tempFilePath
        })
        that.setData({
          files: that.data.files.concat(pic),
        });
      },
      fail(res) {
        console.log(res);
      }
    });
  },
  previewImage(e) {
    wx.previewImage({
      current: this.data.files[e.currentTarget.dataset.index],
      urls: this.data.files,
    });
  },
  deleteImg(e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否删除图片",
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.files.length; i++) {
            if (i == e.currentTarget.dataset.index) {
              that.data.files.splice(i, 1)
              that.data.apply_pic.splice(i, 1)
            }
          }
          that.setData({
            files: that.data.files
          })
        } else if (res.cancel) {
          console.log("用户点击取消")

        }
      }
    })
  },
})