// pages/feedBack/feedBack.js
const {
  api,
  rapi,
  formatTime,
  imgName,
  getUserInfo,
  wxLogin,
  toastTip,
  alertTip,
  confirmTip,
  authAgainByUser,
  userLocation,
  uploadFile
} = require('../../lib/util.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    genders: [{
      name: '先生',
      value: 1
    }, {
      name: '女士',
      value: 0
    }],
    userPhone: '',
    avator: '',
    nickname: "",
    gender: 0,
    userInfo: {},
    member: {},
    picker_switch: false,
    hasUserInfo: false,
    date: '未填写',
    endDate: ''
  },

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

  genderClick: function(e) {
    const that = this
    var value = e.currentTarget.dataset.value;
    if (that.data.gender !== value) {
      that.setData({
        gender: value
      })
    }
  },
  getUserInfo() {
    let that = this
    api.get('user/myInfo', {}, false)
      .then(res => {
        console.log(res)
        let user = res.data
        if (user.gender !== true && user.gender !== false) {
          user.gender = 1
        } else {
          user.gender = user.gender ? 1 : 0
        }
        this.setData({
          userPhone: user.mobile.replace(/(\d{3})(\d{4})(\d{3})/, '$1****$3'),
          nickname: user.nickname || '',
          // vehicleName: device.vehicleName
          avator: user.avatarUrl || '',
          gender: user.gender
        })
      })
      .catch(err => {
        alertTip(err)
      })
  },
  // 手机号隐藏中间四位
  hideTel: function(tel) {
    var reg = /^(\d{3})\d{4}(\d{4})$/;
    tel = tel.replace(reg, "$1****$2");
    return tel
  },
  // 昵称输入
  inputNiceName: function(e) {
    this.setData({
      nickname: e.detail.value
    })
    if (e.detail.cursor > 10) {
      wx.showToast({
        title: '昵称最大不能超过10个字符哦~',
        icon: 'none',
        duration: 2000
      })
    }
  },

  //生日选择
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  handleSave: function(e) {
    console.log(e.detail.value)
    var self = this;
    let {
      nickname,
      gender
    } = this.data
    if (!nickname) {
      toastTip({
        title: "请填写昵称",
        icon: "none"
      })
    } else {
      //调起接口
      let postData = {
        nickname,
        gender,
      }
      api.post('user/update', postData)
        .then(res => {
          toastTip({
            title: "修改成功",
            icon: "none"
          })
          this.getUserInfo()
        })
        .catch(err => {
          alertTip(err)
        })
    }
  },
  updateAvator(e) {
    let that = this
    confirmTip('是否更换头像？', res => {
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths)
          var filePaths = that.data.feedbackImages
          uploadFile(tempFilePaths[0])
            .then(res => {
              toastTip({
                title: "修改成功",
                icon: "none"
              })
              that.getUserInfo()
            })
            .catch(err => {
              alertTip(err)
            })
        }
      })
    })

  },
})