<template>
	<view
		v-if="visible"
		class="common-loading"
		:class="{'common-loading--mask': mask}"
		@touchmove.stop.prevent="noop"
		@tap.stop="noop"
	>
		<view class="common-loading__box">
			<view class="common-loading__halo"></view>
			<image class="common-loading__gif" src="/static/images/img/loading.gif" mode="aspectFit"></image>
			<view v-if="title" class="common-loading__text">{{ title }}</view>
			<view class="common-loading__hint">请稍候，正在为你处理</view>
		</view>
	</view>
</template>

<script>
	import { LOADING_HIDE_EVENT, LOADING_SHOW_EVENT, getLoadingState } from '@/utils/loading.js'

	export default {
		name: 'CommonLoading',
		data() {
			return {
				visible: false,
				title: '加载中...',
				mask: true
			}
		},
		created() {
			const state = getLoadingState()
			this.visible = state.visible
			this.title = state.title
			this.mask = state.mask
			uni.$on(LOADING_SHOW_EVENT, this.show)
			uni.$on(LOADING_HIDE_EVENT, this.hide)
		},
		beforeDestroy() {
			uni.$off(LOADING_SHOW_EVENT, this.show)
			uni.$off(LOADING_HIDE_EVENT, this.hide)
		},
		methods: {
			show(options = {}) {
				this.visible = true
				this.title = options.title || '加载中...'
				this.mask = options.mask !== false
			},
			hide() {
				this.visible = false
			},
			noop() {}
		}
	}
</script>

<style lang="scss" scoped>
	.common-loading {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 99999;
		display: flex;
		align-items: center;
		justify-content: center;
		pointer-events: none;
		background: rgba(13, 18, 34, 0.46);
		backdrop-filter: blur(16rpx);
	}

	.common-loading--mask {
		pointer-events: auto;
	}

	.common-loading__box {
		position: relative;
		min-width: 280rpx;
		min-height: 242rpx;
		overflow: hidden;
		padding: 44rpx 44rpx 36rpx;
		border-radius: 40rpx;
		background: linear-gradient(180deg, rgba(255,255,255,.98), rgba(245,249,255,.96));
		border: 1rpx solid rgba(255,255,255,.86);
		box-shadow: 0 28rpx 82rpx rgba(28, 35, 74, 0.28);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.common-loading__halo {
		position: absolute;
		top: -82rpx;
		right: -74rpx;
		width: 220rpx;
		height: 220rpx;
		border-radius: 50%;
		background: rgba(169, 255, 231, .72);
	}

	.common-loading__gif {
		position: relative;
		width: 92rpx;
		height: 92rpx;
	}

	.common-loading__text {
		position: relative;
		margin-top: 18rpx;
		max-width: 380rpx;
		font-size: 28rpx;
		line-height: 40rpx;
		color: #11162a;
		font-weight: 900;
		white-space: pre-line;
		text-align: center;
	}

	.common-loading__hint {
		position: relative;
		margin-top: 10rpx;
		color: rgba(17, 22, 42, .48);
		font-size: 22rpx;
		line-height: 32rpx;
		text-align: center;
	}
</style>
