const _Http = getApp().globalData.http;
let downCount = null;

Component({
	properties: {

	},
	options: {
		addGlobalClass: true,
	},
	data: {},
	methods: {
		init() {
			this.queryUserMsg();
			// let authlist = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['通用'], ['个人中心']);
			let pathList = [{
				name: "修改登录密码",
				icon: "icon-a-wodeguanyuyingyong",
				color: "var(--warning)",
				path: `/pages/tabbar/mine/changePassword/index`
			}]
		/* 	if (authlist.length != 0) authlist[0].apps.forEach(v => {
				switch (v.name) {
					case "teamManagement":
						console.log(v)
						pathList.unshift({
							name: "团队管理",
							icon: "icon-a-wodetuanduiguanli",
							color: "var(--assist)",
							path: `/${v.path}`
						})
						this.setData({
							teamAuth: JSON.stringify(v.meta.auth)
						})
						break;
				}
			}); */
			this.setData({
				pathList
			});
			return true;
		},
		/* 查询用户信息 */
		queryUserMsg() {
			_Http.basic({
				"classname": "common.usercenter.usercenter",
				"method": "queryUserMsg",
				"content": {
					"nocache": true
				}
			}).then(res => {
				console.log("查询用户信息", res)
				if (res.msg != '成功') {
					wx.showToast({
						title: res.msg,
						icon: "none"
					})
				} else {
					this.setData({
						userMsg: res.data
					})
				}
			})
		},
		/* 退出登录 */
		outLogin() {
			clearTimeout(downCount);
			wx.showLoading({
				title: '正在退出...',
			})
			downCount = setTimeout(() => {
				_Http.logout().then(res => {
					getApp().globalData.SocketTask.close()
					wx.showToast({
						title: '退出成功',
						mask:true
					});
					let loginMsg = wx.getStorageSync("loginMsg");
					wx.clearStorageSync();
					wx.setStorageSync('loginMsg', loginMsg)
					setTimeout(() => {
						wx.reLaunch({
							url: '/pages/login/phone',
						})
					}, 300)
				})
			}, 300);
		},
		/* 去修改用户信息 */
		changeUserMsg() {
			let {
				name,
				phonenumber,
				attinfos,
				hr
			} = this.data.userMsg;
			wx.navigateTo({
				url: `./userMsg/index?attinfos=${JSON.stringify(attinfos)}&name=${name}&phonenumber=${phonenumber}&email=${hr.email}`
			})
		},
		/* 绑定或解绑微信 */
		bindingWechat(e) {
			if (this.data.userMsg.iswechatbinding) {
				let that = this;
				wx.showModal({
					title: "提示",
					content: "是否解除绑定",
					success: (res) => {
						if (res.confirm) that.handleBDWechat(0);
					}
				})
			} else {
				this.handleBDWechat(1);
			}
		},
		handleBDWechat(isbinging) {
			let that = this;
			wx.getUserProfile({
				desc: '用于完善用户资料',
				success: ({
					userInfo
				}) => {
					wx.login({
						success(res) {
							if (res.code) _Http.basic({
								"classname": "common.usercenter.usercenter",
								"method": "WechatBinding",
								content: {
									"wechat_code": res.code,
									isbinging, // 0解绑 1绑定
									wechatuserinfo: userInfo
								}
							}).then(s => {
								if (s.msg != '成功') return wx.showToast({
									title: s.data,
									icon: "none"
								});
								setTimeout(() => {
									wx.showToast({
										title: isbinging == 0 ? '解除成功' : '绑定成功',
										icon: "none"
									})
								}, 100);
								that.queryUserMsg();
							})
						}
					})
				},
				fail: () => {
					wx.showToast({
						title: '操作失败，未获得授权',
						icon: "none"
					})
				}
			})
		},
	}
})