// index/me.js
const App = getApp();
import basicApi from '../api/basic.js'
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        navHeight: App.globalData.navHeight,
        windowHeight: App.globalData.windowHeight,
        accountName: '',
        name: '',
        menuItems: [{
            name: '切换服务器地址',
            icon: 'position',
        }],
        userForm: {},
        rolesList: []
    },
    pageLifetimes: {
        show() {
            const name = wx.getStorageSync('name');
            const accountName = wx.getStorageSync('accountName');
            this.setData({
                name: name,
                accountName: accountName
            })
            this.getUserInfo()
            if (typeof this.getTabBar === 'function' && this.getTabBar()) {
                this.getTabBar().setData({
                    // 当前页面的 tabBar 索引
                    curClick: 3
                })
            }
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        All(e) {
            console.log(e)
        },
        inputBlur(e) {
            this.data.userForm.realName = e.detail
        },
        inputBlur2(e) {
            this.data.userForm.tel = e.detail
        },
        inputBlur3(e) {
            this.data.userForm.email = e.detail
        },
        getUserInfo() {
            basicApi.singleUser(wx.getStorageSync('uid')).then(res => {
                this.getRolePermission(res.data)
            })
        },
        getRolePermission(resData) {
            basicApi.getPermission(resData.unid).then(res => {
                let roles = []
                res.roles.forEach(item => {
                    roles.push(item.name)
                })
                resData.roles = roles.toString()
                this.setData({
                    userForm: resData
                })
            })
        },
        onSubmit(e) {
            basicApi.editUserInfo({
                userId: this.data.userForm.id,
                email: this.data.userForm.email,
                loginName: this.data.userForm.loginName,
                realName: this.data.userForm.realName,
                tel: this.data.userForm.tel,
            }).then(res => {
                if (res.code == 200) {
                    wx.showToast({
                        title: '修改成功',
                        icon: 'success'
                    })
                    this.getUserInfo()
                }
            })
        },
        undatePwd() {
            wx.navigateTo({
                url: '/pages/editPwd/editPwd'
            })
        },
        logout: () => {
            wx.getStorageInfo({
                success: (res) => {
                    res.keys.forEach(item => {
                        if (item !== 'name' && item !== 'password' && item !== 'requestUrl' && item !== 'baseUrl' && item !== 'deploymentType') {
                            wx.removeStorage({
                                key: item,
                                success: function (res) {}
                            })
                        }
                    })
                }
            })

            wx.reLaunch({
                url: '/pages/login/index'
            })
        },

        goOtherPage: async function (e) {
            const {
                value
            } = e.target.dataset
            if (value.name === '切换服务器地址') {
                await wx.setStorage({
                    data: 'success',
                    key: 'modalShow',
                })
                wx.getStorageInfo({
                    success: (res) => {
                        res.keys.forEach(item => {
                            if (item !== 'name' && item !== 'password' && item !== 'requestUrl' && item !== 'baseUrl' && item !== 'deploymentType' && item !== 'modalShow') {
                                wx.removeStorage({
                                    key: item,
                                    success: function (res) {}
                                })
                            }
                        })
                    }
                })
                wx.reLaunch({
                    url: '/pages/login/index'
                })
            }
        }
    }
})