<template>
	<view>
		<u-cell-item :title="userInfo.mobile?'修改手机号':'绑定手机号'" :value="tel" @click="gophone"></u-cell-item>
		<u-cell-item title="修改密码" @click="gopwd"></u-cell-item>
		<view class="fixed-bottom pb-80" v-if="token">
			<u-button type="primary" @click="exit">退出登录</u-button>
		</view>
		<u-modal v-model="show" content="确定解除微信绑定？" :show-cancel-button="true" @confirm="confirm"></u-modal>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				show: false,
			}
		},
		computed: {
			...mapState(['userInfo', 'token']),
			tel() {
				return !this.token? '请登录':this.userInfo.mobile ? this.userInfo.mobile : '未绑定'
			},
			vx() {
				return !this.token? '请登录':this.userInfo.openid ? '已绑定（' + this.userInfo.wx_name + '）' : '未绑定'
			}
		},
		methods: {
			gophone() { //修改手机号
				if (this.token) {
					if (this.userInfo.mobile) {
						uni.navigateTo({
							url: '/pages/my/set/phone-number'
						})
					} else {
						uni.navigateTo({
							url: '/pages/login/phone?type=1'
						})
					}
				}
			},
			gopwd() { //修改密码
				if (this.token) {
					uni.navigateTo({
						url: '/pages/login/forget-pwd?type=2'
					})
				}
			},
			exit() {
				this.$store.commit('exitLogin')
				uni.navigateBack({})
				setTimeout(() => {
					uni.showToast({
						title: '退出成功'
					})
				}, 100)
			},
			wxLogin() {
				if (this.token) {
					if (this.vx == '未绑定') {
						let _this = this;
						uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								console.log(loginRes.authResult);
								// 获取用户信息
								uni.getUserInfo({
									provider: 'weixin',
									success: function(infoRes) {
										console.log(infoRes.userInfo);
										_this.$http('/addons/ddrive/wx_login/wxlogin', {
											openid: infoRes.userInfo.openId,
											wx_name: infoRes.userInfo.nickName
										}, "POST").then(data => {
											console.log(data);
											_this.$store.dispatch('updateUserInfo')
											setTimeout(() => {
												uni.showToast({
													title: '绑定成功'
												})
											}, 100)
										})
									}
								});
							}
						});
					} else {
						if (this.userInfo.mobile) {
							this.show = true
						}
					}
				}
			},
			// 解除微信绑定
			confirm() {
				this.$http('/addons/ddrive/wx_login/untie', "POST").then(data => {
					this.$store.dispatch('updateUserInfo')
					uni.showToast({
						title: '解绑成功'
					})
					console.log(data);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/.u-btn {
		height: 96rpx !important;
	}

	/deep/.u-primary-hover {
		background-color: $blue !important;
	}

	/deep/.u-cell {
		padding: 30rpx 32rpx;
	}
	
</style>
