let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: ['男', '女'],
    sexindex: 0,
    showDialog: false,
    ifName: true,
    DOMAIN_NAME: '',
    park: [],
    parkindex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow(e) {
    var that = this
    that.setData({
      DOMAIN_NAME: app.img_Name
    })
    app.AjaxRequest('get', {
      'content-type': 'application/json'
    }, '/park/list', {}, function (res) {
      if (res.code == 0) {
        res.data.forEach(item => {
          let park = that.data.park
          park.push(item.parkName)
          that.setData({
            park
          })
        })
        that.setData({
          parkList: res.data
        })
        that.parkIdtype(res.data, wx.getStorageSync('parkId'))
      }
    })
    that.getList()
  },

  parkIdtype(list, id) {
    list.forEach((item, index) => {
      if (item.id == id) {
        this.setData({
          parkindex: index
        })
      }
    })
  },

  parkChange(e) {
    var that = this
    that.data.parkList.forEach(item => {
      if (that.data.park[e.detail.value] == item.parkName) {
        wx.request({
          url: app.DOMAIN_NAME + '/user/profile/update',
          data: {
            parkId: item.id
          },
          method: 'POST',
          header: {
            'content-type': 'application/json',
            'Authorization': 'Bearer ' + app.Getopenid()
          },
          success(ret) {
            if (ret.data.code == 402 || ret.data.code == 401) {
              wx.hideLoading()
              wx.showModal({
                confirmText: '确认',
                content: '身份已过期,需重登录',
                success(res) {
                  wx.removeStorageSync('MemberInfo')
                  wx.removeStorageSync('token')
                  if (res.confirm) {
                    wx.reLaunch({
                      url: '/pages/login/login'
                    })
                  }
                }
              })
            } else if (ret.data.code != 0) {
              wx.hideLoading()
              wx.showModal({
                confirmText: '好的',
                content: ret.data.msg || '服务器开小差去了，请重试',
                showCancel: false
              })
            } else if (ret.data.code == 0) {
              that.setData({
                parkindex: e.detail.value
              })
              wx.setStorageSync('parkId', item.id)
              wx.setStorageSync('parkName', item.parkName)
              wx.navigateBack()
              wx.hideLoading()
            }
          }
        })
      }
    })
  },

  cancel() {
    this.setData({
      ifName: true
    })
  },

  change(e) {
    this.setData({
      name: e.currentTarget.dataset.name,
      ifName: false
    })
  },

  setValue(e) {
    if (this.data.name == '姓名') {
      this.setData({
        nickname: e.detail.value
      })
    } else {
      this.setData({
        post: e.detail.value
      })
    }
  },

  confirm() {
    let that = this
    if (this.data.name == '姓名') {
      app.AjaxRequest('POST', {
        'content-type': 'application/json',
        'Authorization': 'Bearer ' + app.Getopenid()
      }, '/user/profile/update', {
        nickname: that.data.nickname
      }, function (ret) {
        that.setData({
          ifName: true
        })
        that.getList()
      })
    } else {
      app.AjaxRequest('POST', {
        'content-type': 'application/json',
        'Authorization': 'Bearer ' + app.Getopenid()
      }, '/user/profile/update', {
        post: that.data.post
      }, function (ret) {
        that.setData({
          ifName: true
        })
        that.getList()
      })
    }
  },

  // 性别选择
  sexChange: function (e) {
    let that = this
    that.setData({
      sexindex: e.detail.value
    })
    app.AjaxRequest('POST', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/user/profile/update', {
      gender: JSON.parse(e.detail.value)
    }, function (ret) {
      that.getList()
    })
  },
  getList() {
    let that = this
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/user/profile', {}, function (res) {
      that.setData({
        memberInfo: res.data,
        sexindex: res.data.sex
      })
    })
  },
  chooseimage: function () {
    var that = this
    let imgUrl = that.data.imgUrl
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        console.log(res.tempFilePaths[0])
        wx.navigateTo({
          url: '../cropper/cropper?img=' + res.tempFilePaths[0],
        })
      }
    })
  },
  navCropper() {
    this.chooseimage()
  },
  exitaccount() {
    wx.showModal({
      title: '提示',
      content: '确定退出账号吗',
      success(res) {
        if (res.confirm) {
          wx.clearStorageSync()
          wx.reLaunch({
            url: '/pages/login/login',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})