<template>
	<view>
		<u-mask :show="visible" :z-index="101" duration="0" />
		<view v-if="visible" class="bt-process-panel" :class="{ 'bt-process-panel--error': !!errorMessage }">
			<view class="bt-process-panel__accent"></view>
			<view class="bt-process-panel__glow"></view>
			<view class="bt-process-panel__inner">
				<view class="bt-process-panel__visual" v-if="!errorMessage">
					<view class="bt-process-panel__orbit">
						<view class="bt-process-panel__ring bt-process-panel__ring--a"></view>
						<view class="bt-process-panel__ring bt-process-panel__ring--b"></view>
						<view class="bt-process-panel__sweep"></view>
						<view class="bt-process-panel__core">
							<image class="bt-process-panel__icon" src="https://api.ccttiot.com/smartmeter/img/static/uCdHjNaNfD9aikVg9Cuk" mode="aspectFit" />
						</view>
					</view>
				</view>
				<view class="bt-process-panel__visual bt-process-panel__visual--err" v-else>
					<view class="bt-process-panel__core bt-process-panel__core--err">
						<image class="bt-process-panel__icon" src="https://api.ccttiot.com/smartmeter/img/static/uCdHjNaNfD9aikVg9Cuk" mode="aspectFit" />
					</view>
				</view>
				<view class="bt-process-panel__head">
					<text class="bt-process-panel__title">{{ errorMessage ? '连接异常' : '蓝牙连接' }}</text>
					<view class="bt-process-panel__status-pill">
						<view class="bt-process-panel__status-dot"></view>
						<text class="bt-process-panel__message">{{ stepMessage }}</text>
					</view>
				</view>
				<view class="bt-process-panel__progress-card" v-if="!errorMessage">
					<view class="bt-process-panel__progress-top">
						<text class="bt-process-panel__progress-label">连接进度</text>
						<view class="bt-process-panel__progress-num-wrap">
							<text class="bt-process-panel__progress-num">{{ progress }}</text>
							<text class="bt-process-panel__progress-unit">%</text>
						</view>
					</view>
					<view class="bt-process-panel__progress-track">
						<view class="bt-process-panel__progress-bar" :style="'width:' + progress + '%;'">
							<view class="bt-process-panel__progress-shine"></view>
						</view>
					</view>
				</view>
				<view v-if="errorMessage" class="bt-process-panel__alert">
					<view class="bt-process-panel__alert-icon">!</view>
					<text class="bt-process-panel__alert-text">{{ errorMessage }}</text>
				</view>
				<view v-if="errorMessage" class="bt-process-panel__actions">
					<view v-if="errorType === 'phone_bluetooth_off'" class="bt-process-panel__btn bt-process-panel__btn--accent" @click="$emit('go-phone-setting')">去设置</view>
					<view v-if="errorType === 'wechat_auth_denied'" class="bt-process-panel__btn bt-process-panel__btn--accent" @click="$emit('go-wechat-auth')">去授权</view>
					<view class="bt-process-panel__btn bt-process-panel__btn--main" @click="$emit('close')">我知道了</view>
				</view>
				<view v-if="errorMessage" class="bt-process-panel__hint">
					<text class="bt-process-panel__hint-icon">ⓘ</text>
					<text>蓝牙操作中请靠近车辆</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'BluetoothProcessPanel',
	props: {
		visible: {
			type: Boolean,
			default: false
		},
		progress: {
			type: Number,
			default: 0
		},
		stepMessage: {
			type: String,
			default: ''
		},
		errorMessage: {
			type: String,
			default: ''
		},
		errorType: {
			type: String,
			default: ''
		}
	}
}
</script>

<style lang="scss" scoped>
@keyframes bt-orbit-spin {
	from { transform: translate(-50%, -50%) rotate(0deg); }
	to { transform: translate(-50%, -50%) rotate(360deg); }
}

@keyframes bt-orbit-spin-rev {
	from { transform: translate(-50%, -50%) rotate(360deg); }
	to { transform: translate(-50%, -50%) rotate(0deg); }
}

@keyframes bt-sweep-spin {
	from { transform: rotate(0deg); }
	to { transform: rotate(360deg); }
}

@keyframes bt-core-breathe {
	0%, 100% {
		box-shadow:
			0 0 0 1rpx rgba(255, 255, 255, 0.8),
			0 10rpx 32rpx rgba(46, 73, 117, 0.14);
	}
	50% {
		box-shadow:
			0 0 0 1rpx rgba(255, 255, 255, 1),
			0 14rpx 40rpx rgba(76, 151, 231, 0.22);
	}
}

@keyframes bt-progress-shine-move {
	0% { left: -55%; }
	100% { left: 130%; }
}

@keyframes bt-status-dot-blink {
	0%, 100% { opacity: 1; transform: scale(1); }
	50% { opacity: 0.5; transform: scale(0.9); }
}

@keyframes bt-panel-enter {
	0% {
		opacity: 0;
		transform: translate(-50%, -48%) scale(0.97);
	}
	100% {
		opacity: 1;
		transform: translate(-50%, -50%) scale(1);
	}
}

.bt-process-panel {
	position: fixed;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
	z-index: 102;
	width: 608rpx;
	box-sizing: border-box;
	overflow: hidden;
	border-radius: 30rpx;
	background: #ffffff;
	border: 1rpx solid #e8ecf1;
	box-shadow:
		0 0 0 1rpx rgba(255, 255, 255, 0.6) inset,
		0 20rpx 50rpx rgba(46, 73, 117, 0.1),
		0 8rpx 24rpx rgba(46, 73, 117, 0.06);
	animation: bt-panel-enter 0.38s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.bt-process-panel__accent {
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 6rpx;
	background: linear-gradient(90deg, #2679d1 0%, #4c97e7 50%, #7eb8ff 100%);
	pointer-events: none;
	z-index: 4;
}

.bt-process-panel__glow {
	position: absolute;
	left: 50%;
	top: 80rpx;
	width: 320rpx;
	height: 200rpx;
	margin-left: -160rpx;
	background: radial-gradient(ellipse at 50% 50%, rgba(76, 151, 231, 0.09) 0%, transparent 72%);
	pointer-events: none;
}

.bt-process-panel__inner {
	position: relative;
	z-index: 2;
	padding: 56rpx 48rpx 48rpx;
}

.bt-process-panel__visual {
	display: flex;
	justify-content: center;
	margin-bottom: 44rpx;
}

.bt-process-panel__visual--err {
	margin-bottom: 36rpx;
}

.bt-process-panel__orbit {
	position: relative;
	width: 216rpx;
	height: 216rpx;

	&::after {
		content: '';
		position: absolute;
		left: 50%;
		top: 50%;
		width: 216rpx;
		height: 216rpx;
		margin: -108rpx 0 0 -108rpx;
		border-radius: 50%;
		border: 1rpx solid rgba(46, 73, 117, 0.06);
		box-sizing: border-box;
	}
}

.bt-process-panel__ring {
	position: absolute;
	left: 50%;
	top: 50%;
	border-radius: 50%;
	border: 2rpx solid transparent;
	transform: translate(-50%, -50%);
}

.bt-process-panel__ring--a {
	width: 208rpx;
	height: 208rpx;
	border-top-color: rgba(76, 151, 231, 0.45);
	border-right-color: rgba(76, 151, 231, 0.08);
	animation: bt-orbit-spin 4.8s linear infinite;
}

.bt-process-panel__ring--b {
	width: 172rpx;
	height: 172rpx;
	border-top-color: rgba(38, 121, 209, 0.28);
	border-left-color: rgba(38, 121, 209, 0.06);
	animation: bt-orbit-spin-rev 3.8s linear infinite;
}

.bt-process-panel__sweep {
	position: absolute;
	left: 50%;
	top: 50%;
	width: 208rpx;
	height: 208rpx;
	margin: -104rpx 0 0 -104rpx;
	border-radius: 50%;
	background: conic-gradient(from 0deg, transparent 0deg, transparent 284deg, rgba(76, 151, 231, 0.05) 300deg, rgba(76, 151, 231, 0.18) 360deg);
	animation: bt-sweep-spin 4.2s linear infinite;
}

.bt-process-panel__core {
	position: absolute;
	left: 50%;
	top: 50%;
	z-index: 3;
	width: 108rpx;
	height: 108rpx;
	margin: -54rpx 0 0 -54rpx;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	background: linear-gradient(160deg, #ffffff 15%, #e8f2fc 50%, #4c97e7 100%);
	animation: bt-core-breathe 3.2s ease-in-out infinite;
}

.bt-process-panel__core--err {
	position: relative;
	left: auto;
	top: auto;
	margin: 0 auto;
	background: linear-gradient(160deg, #ffffff 15%, #fceeee 55%, #e57373 100%);
	animation: none;
	box-shadow: 0 10rpx 32rpx rgba(180, 90, 90, 0.14);
}

.bt-process-panel__icon {
	width: 48rpx;
	height: 48rpx;
}

.bt-process-panel--error {
	.bt-process-panel__accent {
		background: linear-gradient(90deg, #c75c5c 0%, #e57373 50%, #f0a8a8 100%);
	}

	.bt-process-panel__glow {
		background: radial-gradient(ellipse at 50% 50%, rgba(200, 100, 100, 0.08) 0%, transparent 72%);
	}

	.bt-process-panel__title {
		color: #c75c5c;
	}

	.bt-process-panel__status-pill {
		background: #fdf9f9;
		border-color: #f5e0e0;
	}

	.bt-process-panel__status-dot {
		background: #e57373;
	}

	.bt-process-panel__message {
		color: #9a7070;
	}
}

.bt-process-panel__head {
	text-align: center;
	margin-bottom: 0;
}

.bt-process-panel__title {
	display: block;
	font-size: 34rpx;
	font-weight: 600;
	color: #2e4975;
	line-height: 1.35;
	letter-spacing: 1rpx;
}

.bt-process-panel__status-pill {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	max-width: 100%;
	margin-top: 20rpx;
	padding: 14rpx 28rpx;
	background: #f7fafe;
	border: 1rpx solid #e3ebf5;
	border-radius: 999rpx;
	box-sizing: border-box;
}

.bt-process-panel__status-dot {
	width: 10rpx;
	height: 10rpx;
	border-radius: 50%;
	margin-right: 12rpx;
	flex-shrink: 0;
	background: #4c97e7;
	animation: bt-status-dot-blink 2s ease-in-out infinite;
}

.bt-process-panel__message {
	font-size: 26rpx;
	font-weight: 400;
	color: #7c7c7c;
	line-height: 1.55;
}

.bt-process-panel__progress-card {
	margin-top: 36rpx;
	padding: 32rpx 30rpx 30rpx;
	background: linear-gradient(180deg, #f7fafe 0%, #f3f6fa 100%);
	border: 1rpx solid #e3ebf5;
	border-radius: 20rpx;
}

.bt-process-panel__progress-top {
	display: flex;
	align-items: flex-end;
	justify-content: space-between;
	margin-bottom: 24rpx;
}

.bt-process-panel__progress-label {
	font-size: 24rpx;
	font-weight: 500;
	color: #808080;
	padding-bottom: 6rpx;
}

.bt-process-panel__progress-num-wrap {
	display: flex;
	align-items: baseline;
}

.bt-process-panel__progress-num {
	font-size: 44rpx;
	font-weight: 600;
	color: #2e4975;
	font-variant-numeric: tabular-nums;
	line-height: 1;
}

.bt-process-panel__progress-unit {
	margin-left: 6rpx;
	font-size: 26rpx;
	font-weight: 500;
	color: #4c97e7;
}

.bt-process-panel__progress-track {
	position: relative;
	height: 10rpx;
	background: #dce4ee;
	border-radius: 999rpx;
	overflow: hidden;
	box-shadow: inset 0 1rpx 3rpx rgba(46, 73, 117, 0.08);
}

.bt-process-panel__progress-bar {
	position: relative;
	height: 100%;
	min-width: 0;
	border-radius: 999rpx;
	background: linear-gradient(90deg, #7eb8ff 0%, #4c97e7 100%);
	transition: width 0.42s cubic-bezier(0.22, 1, 0.36, 1);
	overflow: hidden;
}

.bt-process-panel__progress-shine {
	position: absolute;
	top: 0;
	bottom: 0;
	width: 36%;
	background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5), transparent);
	animation: bt-progress-shine-move 2.6s ease-in-out infinite;
	pointer-events: none;
}

.bt-process-panel__alert {
	display: flex;
	align-items: flex-start;
	margin-top: 32rpx;
	padding: 24rpx 22rpx;
	background: #fdf9f9;
	border: 1rpx solid #f5e0e0;
	border-radius: 16rpx;
}

.bt-process-panel__alert-icon {
	flex-shrink: 0;
	width: 38rpx;
	height: 38rpx;
	margin-right: 14rpx;
	border-radius: 50%;
	background: #e57373;
	color: #fff;
	font-size: 24rpx;
	font-weight: 600;
	line-height: 38rpx;
	text-align: center;
}

.bt-process-panel__alert-text {
	flex: 1;
	font-size: 26rpx;
	color: #9a5a5a;
	line-height: 1.6;
}

.bt-process-panel__actions {
	display: flex;
	justify-content: center;
	flex-wrap: wrap;
	gap: 20rpx;
	margin-top: 32rpx;
}

.bt-process-panel__btn {
	min-width: 200rpx;
	height: 76rpx;
	line-height: 76rpx;
	padding: 0 40rpx;
	text-align: center;
	border-radius: 8rpx;
	font-size: 28rpx;
	font-weight: 500;
	color: #fff;
	box-sizing: border-box;
}

.bt-process-panel__btn--accent {
	background: #5a9a6b;
	box-shadow: 0 6rpx 18rpx rgba(90, 154, 107, 0.22);
}

.bt-process-panel__btn--main {
	background: #4c97e7;
	box-shadow: 0 8rpx 22rpx rgba(76, 151, 231, 0.28);
}

.bt-process-panel__hint {
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 24rpx;
	padding: 0;
	font-size: 22rpx;
	color: #ababab;
	line-height: 1.5;
}

.bt-process-panel__hint-icon {
	margin-right: 8rpx;
	font-size: 24rpx;
	color: #b0b0b0;
}
</style>
