// pages/home/me/grzl/grzl.js
const app = getApp();//新建页面时 默认引入
const req = app.xzjlReq();//初始化一个的request() 实例
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatar1:[
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-1.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-2.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-3.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-4.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-5.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-6.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-7.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-8.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-9.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-10.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-11.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-12.png',
        ],
        avatar2:[
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-13.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-14.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-15.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-16.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-17.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-18.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-19.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-20.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-21.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-22.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-23.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-24.png',
        ],
        avatar3:[
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-25.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-26.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-27.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-28.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-29.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-30.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-31.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-32.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-33.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-34.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-35.png',
            'https://wish-assets.oss-cn-hangzhou.aliyuncs.com/xy/headimg-36.png',
        ],
        sexs:[{label:'男生',value:'male'},{label:'女生',value:'female'}],
        sexVisible:false,
        note:'',
        cVisible:false,
        userInfo:{},
        endDate:new Date().getTime()
    },
    changeAvatar(e){
        req.patchRequest('/api/user/curt',{avatar:e.target.dataset.img}).then((res)=>{
            if(res.data.code==200){
                this.setData({userInfo:res.data.data})
                wx.showToast({
                    title:'修改成功',
                    icon: 'none',
                    duration: 1000
                })
            }
           }).catch((err)=>{
            console.log(err);
        })
    },
    onColumnChange(e) {/**修改性别 */
        req.patchRequest('/api/user/curt',{spriteType:e.detail.value[0]}).then((res)=>{
            if(res.data.code==200){
                this.setData({userInfo:res.data.data})
                wx.showToast({
                    title:'修改成功',
                    icon: 'none',
                    duration: 1000
                })
            }
           }).catch((err)=>{
            console.log(err);
        })
      },
    handleCalendar(){
        this.setData({cVisible:true})
        console.log('11111')
    },
    changeDate(e) {/**修改日期 */
    console.log(e);
        const { value } = e.detail;
        console.log(value);
        const format = (val) => {
          const date = new Date(val);
          return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
        };
  
        this.setData({
          note: format(value),
        });
        req.patchRequest('/api/user/curt',{birthday:format(value)}).then((res)=>{
            console.log(res);
            if(res.data.code==200){
                this.setData({userInfo:res.data.data})
                wx.showToast({
                    title:'修改成功',
                    icon: 'none',
                    duration: 1000
                })
                this.getUserInfo()
            }
           }).catch((err)=>{
            console.log(err);
        })
      },
      changeSex(){
          this.setData({sexVisible:true})
        console.log('修改性别')
      },
    doChangeName1(e){
        if (e.detail.value !== this.data.userInfo.nick) {
            req.patchRequest('/api/user/curt',{nick:e.detail.value}).then((res)=>{
                if(res.data.code==200){
                    this.setData({userInfo:res.data.data})
                    wx.showToast({
                        title:'修改成功',
                        icon: 'none',
                        duration: 1000
                    })
                }
               }).catch((err)=>{
                console.log(err);
            })
        }
    },
    getUserInfo(){
        req.getRequest('/api/user/curt',{}).then((res)=>{
            if(res.data.code==200){
                this.setData({userInfo:res.data.data})
            }
            console.log(this.userInfo);
           }).catch((err)=>{
            console.log(err);
        })
    },
    go2Reback(){
        wx.switchTab({
          url: '/pages/home/me/indexx',
        })
    },

    // 自定义头像
    editAvatar(){
        wx.chooseMedia({
            count: 1,
            mediaType: 'image',
            sourceType: ['album', 'camera'],
            camera: 'front',
            success: (res) => {
              wx.uploadFile({
                url: 'https://xzjl-api.windymuse.cn/api/resource',
                filePath: res.tempFiles[0].tempFilePath,
                name: 'file',
                header: {
                    'Authorization': wx.getStorageSync('token'),        
                    'Content-Type': 'multipart/form-data'
                },
                formData: {
                    type: 'poster'
                },
                success: (res) => {
                    const result = JSON.parse(res.data)
                    req.patchRequest('/api/user/curt',{avatar:result.data.abUrl}).then((res)=>{
                        if(res.data.code==200){
                            this.setData({userInfo:res.data.data})
                            wx.showToast({
                                title:'修改成功',
                                icon: 'none',
                                duration: 1000
                            })
                        }
                       }).catch((err)=>{
                        console.log(err);
                    })
                }
            })
            }
        })
    },

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

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})