let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cursor: 0,
    hideAdd: 0,
    ismodify: false,
    imgUrl: [],
    content: '',
    num: 0,
    imgUrls: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    let that = this
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/repair/add', {}, function (res) {
      if (res.code == 0) {
        that.setData({
          parkName: wx.getStorageSync('parkName'),
          member: res.data
        })
      }
    })
  },
  bindTextAreaBlur(e) {
    console.log(e.detail.value)
    this.setData({
      cursor: e.detail.cursor,
      content: e.detail.value
    })
  },
  headerInput(e) {
    var value = e.detail.value
    var name = e.currentTarget.dataset.name
    this.setData({
      ["member." + e.currentTarget.dataset.name]: value
    })
  },

  previewImg(e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index
    //所有图片
    var imgs = this.data.imgUrl
    console.log(imgs[index])
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  chooseimage: function () {
    var that = this
    let imgUrl = that.data.imgUrl
    var num = 9 - imgUrl.length
    wx.chooseImage({
      count: num, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        //把每次选择的图push进数组
        for (let i = 0; i < res.tempFilePaths.length; i++) {
          imgUrl.push(res.tempFilePaths[i])
        }
        if (imgUrl.length == 9) {
          that.setData({
            hideAdd: 1
          })
        } else {
          that.setData({
            hideAdd: 0
          })
        }
        that.setData({
          imgUrl
        })
      }
    })
  },
  delImg(e) {
    let index = e.currentTarget.dataset.index
    let imgUrl = this.data.imgUrl
    imgUrl.splice(index, 1)
    this.setData({
      imgUrl
    })
  },
  modify() {
    var ismodify = this.data.ismodify
    this.setData({
      ismodify: ismodify == true ? false : true
    })
  },
  apply() {
    let that = this
    let member = that.data.member
    var img = []
    console.log(member)
    // if (!member.customer) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '请输入需要维修得公司名字',
    //   })
    //   return false
    // }
    if (!member.userName) {
      wx.showToast({
        icon: 'none',
        title: '请输入报修人',
      })
      return false
    }
    if (!member.mobile) {
      wx.showToast({
        icon: 'none',
        title: '请输入联系电话',
      })
      return false
    }
    if (!member.area) {
      wx.showToast({
        icon: 'none',
        title: '请输入报修区域',
      })
      return false
    }
    if (that.data.imgUrl.length > 0) {
      var num = 0
      that.data.imgUrl.forEach(item => {
        app.Upload(item, function (res) {
          if (res.code == 0) {
            img.push({
              name: res.fileName,
              url: res.url,
              uid: num
            })
            that.setData({
              imgUrls: img
            })
            num++
          }
          if (num == that.data.imgUrl.length) {
            that.savefun()
          }
        })
      })
    } else {
      that.savefun()
    }
  },

  savefun() {
    let that = this
    let member = that.data.member
    app.AjaxRequest('POST', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/repair/save?parkId=' + wx.getStorageSync('parkId'), {
      name: member.userName,
      phone: member.mobile,
      area: member.area,
      content: that.data.content,
      userId: member.userId,
      // customerId: member.customer.id,
      repairImages: that.data.imgUrls
    }, function (res) {
      if (res.code == 0) {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          success: (res) => {
            wx.navigateBack()
          }
        })
      }
    })
  }
})