<template>
	<div class="phone-popup">
		<u-popup
			:show="popShow"
			:round="10"
			mode="bottom"
			:closeOnClickOverlay="false"
			@close="refuse"
		>
			<view class="title">提示</view>
			<view class="content">
				进行此操作之前需要先获取您的手机号
			</view>
			<view class="footer">
				<button class="allow" open-type="getPhoneNumber" @click="allow" @getphonenumber="getPhoneNumber" plain>允许</button>
				<button class="allow refuse" @click="refuse">拒绝</button>
			</view>
		</u-popup>
	</div>
</template>

<script>
	import { getUserPhoneNumber, verifyPhone } from '@/api/index.js';
	import { getUserProfile } from "@/utils/auth.js";
	export default {
		props: {
			popShow: {
				type: Boolean,
				default: false
			}
		},
		methods: {
			// 拒绝
			refuse() {
				this.$emit('closePop')
				uni.switchTab({
					url: '/pages/index/index'
				});
			},
			allow() {
				console.log('点击允许')
			},
			//获取手机号
			async getPhoneNumber (e) {
				if(e.detail.errMsg === 'getPhoneNumber:ok'){ //允许
					const phone = await getUserPhoneNumber(e.detail.code);
					//调获取手机号接口，成功后隐藏对话框
					uni.setStorageSync("registerPhone", phone);
					const res = await verifyPhone(phone);
					if (res === "用户未注册") {
					  wx.reLaunch({
					    url: "/pages/register/register",
					  });
					} else {
						this.$emit('refresh');
					  await getUserProfile();
					}
				}  
				else if(e.detail.errMsg === 'getPhoneNumber:fail user deny'){ //取消
					uni.switchTab({
						url: '/pages/index/index'
					});
				}
				this.$emit('closePop');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.phone-popup {
		display: flex;
		justify-content: center;
		align-items: center;
		// padding-bottom: 0;
		.title {
			font-size: 36rpx;
			color: #000;
			font-weight: 600;
			margin-bottom: 32rpx;
			text-align: center;
		}
		.content {
			font-size: 32rpx;
			color: #000;
			margin-bottom: 42rpx;
			text-align: center;
		}
		.footer {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: flex-end;
			.allow {
				color: rgba(197, 0, 10, 1);
				height: 68rpx;
				padding: 16px 19px;
				line-height: 0;
				border-color: rgba(197, 0, 10, 1);
			}
			.refuse {
				color: rgba(142, 142, 142, 1.0);
				border-color: rgba(142, 142, 142, 1.0);
				background-color: transparent;
			}
		}
	}
</style>