import {
  get, post,
} from '../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    customerInfo: {},
    name: '',
    type: '',
    userId: '',
    phone:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.userId) {
      this.setData({
        phone: options.phone,
        name: options.name,
        type: options.type,
        userId: options.userId
      })
    } else {
      this.getUserFiles()
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 监听输入姓名
   */
  onChangePhone(e) {
    this.setData({
      phone: e.detail.value,
    })
  },

  /**
   * 更新客户档案
   * Customer
   * 
  */
  setCustomerInfo() {
    let { userInfo,type,userId,phone } = this.data;
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if(!myreg.test(phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return;
    }
    post('api/user/update',{
      user_id: userId,
      mobile: phone
    },(res) => {
      wx.showToast({
        title: '修改成功',
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000);
    })
  },

  /**
   * 获取美导师用户档案
   * api/user/files
   */
  getUserFiles() {
    get('api/mruser/info', {}, (res) => {
      this.setData({
        userInfo: res.data
      })
    })
  },

  /**
   * 更新美导师档案
   * api/user/save_files
   */
  setUserFiles() {
    let {
      avatar_url,
      realname,
      birthday,
      sex,
      city
    } = this.data.userInfo;
    post('api/mruser/update', {
      avatar_url,
      realname,
      birthday,
      sex,
      city
    }, () => {
      wx.showToast({
        title: '修改成功',
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000);
    })
  }
})