<template>
	<view v-if="showPopup" class="uni-popup" @touchmove.stop.prevent="">
		<view :class="[type, ani, animation ? 'ani' : '']" class="uni-custom uni-popup__wrapper"
			@click="close(true)">
			<view class="uni-popup__wrapper-box">
				<view :style="[{paddingTop: StatusBar+'px'}]">
					<view class="title">{{authList[permissionID].title}}</view>
					<view class="content">{{authList[permissionID].content}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'YkAuthpup',
		props: {
			// 开启动画
			animation: {
				type: Boolean,
				default: true
			},
			//弹出层类型，可选值，top: 顶部弹出层；bottom：底部弹出层；center：全屏弹出层,
			type: {
				type: String,
				default: 'center'
			},
			show: {
				type: Boolean,
				default: true
			},
			permissionID: {
				type: [String, Number],
				default: ''
			}
		},
		data() {
			const appName = '馨美研医'
			return {
				ani: '',
				showPopup: false,
				StatusBar:'',
				authList: {
					'WRITE_EXTERNAL_STORAGE': {
						title: appName + "对存储空间/照片权限申请说明",
						content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像、意见反馈、保存相册、分享、客服沟通等场景中读取和写入相册和文件内容。"
					},
					'ACCESS_FINE_LOCATION': {
						title: appName + "对地理位置权限申请说明",
						content: "便于应用程序可以提供基于位置的服务、定位导航、附近搜索等功能。"
					},
					'CALL_PHONE': {
						title: appName + "对拨打/管理电话权限申请说明",
						content: "便于您使用该功能联系客服、业务经理与联系等场景下使用"
					},
					'CAMERA': {
					    title: appName + "对拍摄照片/录制视频权限申请说明",
					    content: "便于您使用该功能上传您的照片/图片/视频/识别二维码及用于更换头像等场景下使用。"
					},
					'RECORD_AUDIO': {
					    title: appName + "申请获取麦克风权限",
					    content: "在下方弹窗中选择允许后，你可以在" + appName + "中发送语音消息的场景使用麦克风进行录音。"
					}
				}
			}
		},
		created() {
			this.getSystemInfo();
		},
		methods: {
			//获取状态栏高度
			getSystemInfo(){
				uni.getSystemInfo({
				  success: function(e) {
				    this.StatusBar = e.statusBarHeight;
				  }
				})
			},
			open() {
				this.requestPermissions(this.permissionID);
			},
			close(type) {
				this.ani = ''
				this.$nextTick(() => {
					setTimeout(() => {
						this.showPopup = false
					}, 300)
				})
			},
			//权限检测
			requestPermissions(permissionID) {
				let _this = this;
				let _permissionID;
				_permissionID = 'android.permission.' + permissionID;
				// #ifdef APP-PLUS
				//判断安卓与ios设备
				if ( plus.os.name =='Android') {
					plus.android.checkPermission(_permissionID,
						granted => {
							if (granted.checkResult == -1) {
								//还未授权当前查询的权限，打开权限申请目的自定义弹框
								_this.showPopup = true;
								_this.$nextTick(() => {
									setTimeout(() => {
										_this.ani = 'uni-' + _this.type
									}, 30)
								})
							}
						},
						error => {
							console.log(error.message);
						}
					);
					plus.android.requestPermissions([_permissionID],
						(e) => {
							//关闭权限申请目的自定义弹框
							_this.ani = '';
							_this.$nextTick(() => {
								setTimeout(() => {
									_this.showPopup = false
								}, 300)
							})
							if (e.granted.length > 0) {
								//当前查询权限已授权，此时可以通知页面执行接下来的操作
								_this.$emit('changeAuth');
							}
						})
				}else{
					//IOS不需要，在配置文件的隐私信息访问的许可描述里可添加
				}
				// #endif
			}
		}
	}
</script>
<style lang="scss">
	.uni-popup {
		position: fixed;
		/*  #ifdef  H5  */
		top: 0px;
		// top: 50px;
		/*  #endif  */
		/*  #ifndef  H5  */
		top: 0px;
		/*  #endif  */
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 99999;
		overflow: hidden;
		background-color: #fff;
		&__wrapper {
			position: absolute;
			z-index: 999;
			box-sizing: border-box;

			&.ani {
				transition: all 0.3s;
			}

			&.top {
				top: 0;
				left: 0;
				width: 100%;
				transform: translateY(-100%);
			}

			&.bottom {
				bottom: 0;
				left: 0;
				width: 100%;
				transform: translateY(100%);
			}

			&.right {
				bottom: 0;
				left: 0;
				width: 70%;
				transform: translateY(100%);
			}

			&.center {
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				transform: scale(1.2);
				opacity: 0;
			}

			&-box {
				position: relative;
				box-sizing: border-box;
			}

			&.uni-custom {
				& .uni-popup__wrapper-box {
					// border-radius: 20upx 20upx 0 0;
					padding: 30upx;
					background: #fff;
					.title{
						font-size: 32rpx;
						font-weight: bold;
					}
					.content{
						margin-top: 16rpx;
						line-height: 1.6;
					}
				}

				&.center {
					& .uni-popup__wrapper-box {
						position: relative;
						max-width: 80%;
						max-height: 80%;
						overflow-y: scroll;
					}
				}

				&.top,
				&.bottom {
					& .uni-popup__wrapper-box {
						width: 100%;
						max-height: 1000upx;
						// overflow-y: scroll;
					}
				}
			}

			&.uni-top,
			&.uni-bottom {
				transform: translateY(0);
			}

			&.uni-center {
				transform: scale(1);
				opacity: 1;
			}
		}
	}
</style>