// pages/user/userfw/userfw.js
var e = getApp(), os = e.globalData.setting;
var utils = require('../../../../utils/util.js');
var regeneratorRuntime = require('../../../../utils/runtime.js');


Page({
    /**
     * 页面的初始数据
     */
    data: {
        defaultAvatar: os.imghost + "/miniapp/images/no-head.jpg",
        ob: {},
        load: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
         if(getApp().globalData.up_nick_avatar){
              this.setData({
                  ob: JSON.parse(JSON.stringify(getApp().globalData.up_nick_avatar))
              })
              getApp().globalData.up_nick_avatar=null;
         }

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

    },


    onChooseAvatar: function (e) {
        console.log(e, 1111);
        var th = this;
        getApp().request.uploadFile(os.url + "/api/weshop/comment/uploadCommentImg", {
            filePath: e.detail.avatarUrl,
            name: "file",
            success: function (t) {
                var u = t.data.data;
                th.setData({'ob.head_pic': u})
            }
        });

    },

    get_val: function (e) {
        console.log(e, 2222);
        this.setData({'ob.nickname': e.detail.value})
    },

    save_data() {

        var that=this;
        setTimeout(()=>{
            if (!this.data.ob.head_pic) {
                wx.showToast({
                    title: '请选择头像',
                    icon: 'none',
                    duration: 2000
                });
                return false;
            }

            if (!this.data.ob.nickname) {
                wx.showToast({
                    title: '请输入昵称',
                    icon: 'none',
                    duration: 2000
                });
                return false;
            }

            if (this.data.load) return false;
            this.setData({load: 1})

            wx.showLoading();

            getApp().request.put("/api/weshop/users/update", {
                data: {
                    store_id: os.stoid,
                    user_id: getApp().globalData.user_id,
                    nickname: this.data.ob.nickname,
                    head_pic: this.data.ob.head_pic

                },
                success: function (su) {

                    setTimeout(()=>{
                        wx.hideLoading();
                        if (su.data.code == 0) {
                            wx.navigateBack({delta: 1})
                        }else{
                            that.setData({load: 0})
                            wx.showToast({
                                title: su.data.msg,
                                icon: 'none',
                                duration: 2000
                            });
                        }
                    },1000)


                }
            });
        },500)


    },
    save_cell(){
      if (!this.data.ob.head_pic) {
          wx.showToast({
              title: '请选择头像',
              icon: 'none',
              duration: 2000
          });
          return false;
      }
      if (!this.data.ob.nickname) {
          wx.showToast({
              title: '请输入昵称',
              icon: 'none',
              duration: 2000
          });
          return false;
      }
      if (this.data.load) return false;
      this.setData({load: 1})
      wx.setStorageSync('nick_obj', this.data.ob)
      wx.navigateBack()
    }

})