// pages/userEdit/userEdit.js
var urlPath = require('../../config.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: urlPath.host,
    usesDetails:{},
    region: ['山东省', '青岛市'],
    province:'山东省',
    city:'青岛市',
    // county:'',
    customItem:'全部',
    custom_avatar:'',
    nick_name:''
  },
	onAuth() {
		let that = this
		wx.getUserProfile({
			desc: '用于完善会员资料',
			lang: "zh_CN",
			success: function (res) {
        console.log(res)
				that.setData({
					userInfo:res.userInfo,
					'userInfo.address':res.userInfo.province+" • "+res.userInfo.city
				})
				
			}
		})
  },
  onUpimg: function(){
    var that  =this
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          // wx.request({
          url: urlPath.upImg, //仅为示例,非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            openid: wx.getStorageSync('openid')
            // 'user': 'test'
          },
          header: {
            // 'content-type': 'application/x-www-form-urlencoded'
            'content-type': 'multipart/form-data'
          },
          dataType: 'json', // 添加这个配置
          method: 'post',
          // data: {
          //   file:tempFilePaths[0],
          // },
          success (res){
            const data = JSON.parse(res.data)
            const custom_avatar = urlPath.host+JSON.parse(data.data).custom_avatar
            that.setData({
              custom_avatar:custom_avatar
            })
            //do something
          }
        })
      }
    })
  },
  bindNameChange: function (e) {
    this.setData({
      nick_name: e.detail.value
    })
  },
  bindRegionChange: function (e) {
    this.setData({
      province: e.detail.value[0],
      city: e.detail.value[1],
      // county: e.detail.value[2]
      county: ''
    })
  },
  save: function(){
    // var address = this.data.region.join(',')
    wx.request({
			url: urlPath.apiCustomEditUserInfo,
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json', // 添加这个配置
			method: 'post',
			data: {
        openid: wx.getStorageSync('openid'),
        nick_name:this.data.nick_name,
        // address:address,
        province:this.data.province != '全部' ? this.data.province : '',
        city:this.data.city != '全部' ? this.data.city : '',
        county:this.data.county != '全部' ? this.data.county : '',
			},
      success(res) {
        wx.showToast({
          title: "保存成功",
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
		wx.request({
			url: urlPath.getUserInfo,
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json', // 添加这个配置
			method: 'post',
			data: {
				openid: wx.getStorageSync('openid')
			},
			success(res) {
        if (res.data.result == 'success') {
          var data = JSON.parse(res.data.data)
          let {nickname,avatar,address,province,city,county,area_str} =  data.user
					var url = data.user.avatar != '' ? urlPath.host+(data.user.avatar) : ''
					that.setData({
						custom_avatar: url,
						nick_name: nickname,
						region: area_str,
						province: province?province:'请选择省份',
						city:city?city:'请选择城市',
          })
				} else {
					console.log(res.data.msg); //失败
				}
			}
		});
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})