<template>
	<view class="userInfo-container">
		<u-cell-group>
			<u-cell-item title="头像" :arrow="false" center>
				<view class="user-photo"><open-data type="userAvatarUrl"></open-data></view>
			</u-cell-item>
			<u-cell-item title="手机号码" :value="userDate.mobile" :arrow="false"></u-cell-item>
			<u-cell-item v-if="address!=null" title="收货地址" :value="address?'修改收货地址':'设置收货地址'" index="0" @click="editAddress"></u-cell-item>
			<u-cell-item v-if="existPayPwd!=null" title="支付密码" :value="existPayPwd?'修改密码':'设置密码'" @click="setPayPwd"></u-cell-item>
		</u-cell-group>
		<!-- 退出 -->
		<view class="user-btn">
			<u-button type="success" :custom-style="quitButton" @click="quitSys" shape="circle">退出登录</u-button>
		</view>
	</view>
</template>

<script>
	import {existPayPwd} from '@/api/order.js'
	import {findAddressBycustomerNo} from '@/api/receiveAddress.js'
	import {logout} from '@/api/login.js'
	export default {
		data() {
			return {
				quitButton: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					background: '#01c9b2'
				},
				userDate: {},
				existPayPwd: null,
				address: null,
				userInfo: {}
			}
		},
		onShow() {
			this.userDate = this.$store.state.vuex_userData
			this.getPayPwd()
			this.getAddress()
		},
		methods:{
			// 获取用户位置
			getAddress() {
				 findAddressBycustomerNo({}).then(res => {
					 console.log(res)
					 if (res.status == 200&&res.data.length) {
						 this.address = res.data[0].receiveAddress
						 this.userInfo = res.data[0]
					 } else {
						 this.address = ''
						 this.userInfo = {}
					 }
				 })
			},
			// 判断是否设置过支付密码
			getPayPwd(){
				existPayPwd().then(res=>{
					if(res.status == 200) {
						if(res.data) {
							this.existPayPwd = true
						} else {
							this.existPayPwd = false
						}
					}
				})
			},
			// 修改收货地址
			editAddress(index){
				this.$u.vuex("vuex_OrderAddress",this.userInfo)
				uni.navigateTo({
					url:"/pages/toOrder/editAddress"
				})
			},
			// 设置密码
			setPayPwd(){
				// 设置过支付密码,则修改密码
				if(this.existPayPwd){
					uni.navigateTo({ url: '/pages/userCenter/userInfo/paymentPwd' })
				}else{
					// 没设置过支付密码,提示设置密码
					uni.navigateTo({ url: '/pages/userCenter/userInfo/changePayPwd' })
				}
			},
			// 退出登录
			quitSys(){
				let _this = this
				uni.showModal({
				  title: '提示',
				  content: '您确定要退出登录吗?',
				  success(res) {
				    if (res.confirm) {
				      logout({}).then(res => {
				       getApp().globalData.token = ''
				       _this.$u.vuex('vuex_token','')
					   // 关闭socket
					   _this.$store.commit("$closeWebsocket")
				        uni.reLaunch({
				        	url: '/pages/index/index'
				        })
				      })
				    }
				  }
				})
			}
		}
	}
</script>

<style lang="less">
	.userInfo-container{
		width: 100%;
		.logout-btn{
			width: 60%;
			margin-top: 200rpx;
		}
		.user-btn{
			padding: 85rpx 200rpx;
		}
		.user-photo{
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-right: 5rpx;
			float: right;
		}
	}
</style>
