<template>
	<!-- 来电接听界面 -->
	<view class="incoming-call">
		<!-- 背景模糊层 -->
		<view class="blur-background" v-if="callBackground">
			<image :src="callBackground" mode="aspectFill" class="bg-image"></image>
			<view class="bg-overlay"></view>
		</view>

		<!-- 主内容区 -->
		<view class="caller-info">
			<view class="caller-avatar-container">
				<view class="caller-avatar" :class="{'avatar-ring': isRinging}">
					<image v-if="callerAvatar" :src="callerAvatar" class="avatar-image"></image>
					<uni-icons v-else type="contact-filled" size="50" color="#fff"></uni-icons>
				</view>
			</view>
			<text class="caller-name">{{callerName}}</text>
			<text class="call-type">{{ localSession.mediaType === 0 ? '语音通话' : "视频通话" }}</text>
			<text class="call-status">{{callStatus}}</text>
		</view>

		<!-- 控制按钮 -->
		<view class="incoming-controls">
			<view class="incoming-control" @click="cutFn(false)">
				<view class="call-btn decline-btn" :class="{'btn-active': activeBtn === 'decline'}">
					<uni-icons type="closeempty" size="32" color="#fff"></uni-icons>
				</view>
				<text class="btn-text">拒绝</text>
			</view>

			<view class="incoming-control" @click="cutFn(true)">
				<view class="call-btn accept-btn" :class="{'btn-active': activeBtn === 'accept'}">
					<uni-icons type="checkmarkempty" size="28" color="#fff"></uni-icons>
				</view>
				<text class="btn-text">接听</text>
			</view>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	import * as call from "@/uni_modules/RongCloud-CallWrapper/lib/index"
	// #endif
	export default {
		name: "incoming-call",
		data() {
			return {
				// 通话状态
				isRinging: true,
				isCalling: false,
				isEnded: false,

				// 通话时间
				callDuration: 0,
				callTimer: null,

				// 控制按钮状态
				activeBtn: null,

				// 通话设置
				isMuted: false,
				isSpeakerOn: true,
				isVideoOn: true,

				// 控制界面显示
				showActionBar: false,
				callerName: "用户id",
				callerAvatar: "/static/images/public/random2.png",
				callBackground: "/static/images/public/random2.png",
				localSession: null,
			};
		},
		mounted() {
			// 30秒后自动挂断
			setTimeout(() => {
				if (!this.isCalling && !this.isEnded) {
					this.autoDecline();
				}
			}, 30000);
		},
		onLoad() {
			uni.getStorage({
				key: "room-parameters",
				success: (res) => {
					console.log('res: ',res);
					this.localSession = res.data
					this.callerName = res.data.mine
				}
			});
		},
		methods: {
			//是否接入
			cutFn(isFlag){
				//确认接入
				console.log('isFlag: ',isFlag);
				if(isFlag){
					this.isEnded = true;
					console.log('localSession: ',this.localSession);
					if (this.localSession.callId && this.localSession.callId.length > 0) {
						this.onCallReceived(this.localSession);
					}
				}else{
					//取消接入
					call.hangup();
					uni.navigateBack()
				}
			},
			//是否接收逻辑
			onCallReceived(session) {
				// //呼入
				uni.setStorageSync('room-parameters', {
					callType: 'in',
					mediaType: session.mediaType === 0 ? 'audio' : 'video'
				});
				//跳转.nvue
				uni.redirectTo({
					url:'/pages/room/room'
				});
			},
			// 自动拒绝（无人接听）
			autoDecline() {
				this.stopRinging();
				this.isEnded = true;
				this.callStatus = "未接听";

				call.hangup();

				// 2秒后关闭界面
				setTimeout(() => {
					uni.navigateBack()
				}, 2000);
			},
		}
	};
</script>

<style lang="scss" scoped>
	.incoming-call {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 9999;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		background-color: #000;

		.blur-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 0;

			.bg-image {
				width: 100%;
				height: 100%;
				filter: blur(20px);
				opacity: 0.6;
			}

			.bg-overlay {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				background: rgba(0, 0, 0, 0.5);
			}
		}

		.caller-info {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding-top: 100rpx;
			z-index: 1;

			.caller-avatar-container {
				position: relative;
				margin-bottom: 40rpx;

				.caller-avatar {
					width: 180rpx;
					height: 180rpx;
					border-radius: 50%;
					background: linear-gradient(135deg, #1aad19, #129611);
					display: flex;
					align-items: center;
					justify-content: center;
					overflow: hidden;

					.avatar-image {
						width: 100%;
						height: 100%;
					}

					&.avatar-ring {
						animation: ringPulse 1.5s infinite;
					}
				}
			}

			.caller-name {
				font-size: 48rpx;
				color: #fff;
				font-weight: 500;
				margin-bottom: 20rpx;
			}

			.call-type {
				font-size: 32rpx;
				color: rgba(255, 255, 255, 0.9);
				margin-bottom: 10rpx;
			}

			.call-status {
				font-size: 28rpx;
				color: rgba(255, 255, 255, 0.7);
			}

			.call-timer {
				margin-top: 30rpx;
				padding: 10rpx 30rpx;
				background: rgba(255, 255, 255, 0.1);
				border-radius: 30rpx;

				.timer-text {
					font-size: 32rpx;
					color: #fff;
					font-weight: 500;
				}
			}
		}

		.incoming-controls {
			display: flex;
			justify-content: space-around;
			padding: 80rpx 100rpx 120rpx;
			z-index: 1;

			.incoming-control {
				display: flex;
				flex-direction: column;
				align-items: center;

				.call-btn {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 20rpx;
					transition: all 0.2s;

					&.decline-btn {
						background-color: #f43f3b;

						&.btn-active {
							transform: scale(0.9);
							background-color: #d93632;
						}
					}

					&.accept-btn {
						background-color: #07c160;

						&.btn-active {
							transform: scale(0.9);
							background-color: #06ad56;
						}
					}
				}

				.btn-text {
					font-size: 28rpx;
					color: #fff;
				}
			}
		}

		.action-bar {
			position: absolute;
			bottom: 60rpx;
			left: 0;
			width: 100%;
			display: flex;
			justify-content: space-around;
			padding: 0 60rpx;
			z-index: 1;

			.action-item {
				display: flex;
				flex-direction: column;
				align-items: center;

				.action-icon {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					background-color: rgba(255, 255, 255, 0.2);
					display: flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 20rpx;
					transition: all 0.3s;

					&.active {
						background-color: rgba(255, 255, 255, 0.4);
					}
				}

				.action-text {
					font-size: 24rpx;
					color: #fff;
				}
			}
		}
	}

	@keyframes ringPulse {
		0% {
			box-shadow: 0 0 0 0 rgba(26, 173, 25, 0.4);
		}

		70% {
			box-shadow: 0 0 0 30rpx rgba(26, 173, 25, 0);
		}

		100% {
			box-shadow: 0 0 0 0 rgba(26, 173, 25, 0);
		}
	}

	/* 响应式调整 */
	@media (max-height: 600px) {
		.incoming-controls {
			padding: 40rpx 100rpx 80rpx !important;
		}

		.action-bar {
			bottom: 40rpx !important;
		}
	}
</style>