<template>
	<view class="container">
		<!-- 设备信息 -->
		<view class="device-info">
			<view class="device-header">
				<view class="deviceIMG">
					<image :src="itemInfo.devicePic" mode="aspectFit" class="IMG"></image>
				</view>
				<view class="device-name">
					<view>设备:{{itemInfo.deviceName}}</view>
					<view class="ID">
						<view class="ID">ID: {{itemInfo.deviceImei}}
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 功能权限 -->
		<view class="permissions">
			<view class="permissions-title">功能权限</view>
			<view :checked="allSelected" class="allSelect" @click="toggleAllSelection()">全选</view>
			<text class="permissions-title"></text>
			<view class="permission-item" v-for="(item, index) in permissions" :key="index"
				@click="toggleSelection(index)">
				<view class="checkbox" :class="{ checked: item.checked }">
					<uni-icons v-if="item.checked" type="checkmarkempty" size="18" color="rgb(0, 0, 0)"></uni-icons>
				</view>
				<view> {{ item.label }}</view>

			</view>
		</view>

		<!-- 被分享人信息 -->
		<view class="recipient-info">
			<view class="recipient-title">被分享人信息</view>
			<input type="number" v-model="phone" placeholder="输入手机号" class="phone-input" :maxlength="11" />
			<view class="verification-code">
				<input type="number" v-model="code" placeholder="短信验证码" class="code-input" :maxlength="6" />
				<view class="get-code-btn" :class="showView?' get_phone_number blue':'get_phone_number grad'"
					@click="getPhoneCode">{{ isCounting ? `${countdown}s后重新获取` : '获取验证码' }}</view>
			</view>
		</view>

		<!-- 分享按钮 -->
		<button class="share-btn" @click="shareUp">分享</button>
		<!-- 分享弹框 -->
		<view class="agreement-mask" v-if="shareShow" @click="closePopup('share')">
			<view class="agreement-popup" @click.stop>
				<view class="popup-content">
					<image src="/static/images/success.png" mode="aspectFit" class="svg"></image>
					<view>
						<view class="popup-Title">设备分享成功</view>
					</view>
				</view>
				<!-- 按钮组 -->
				<view class="popup-buttons">
					<button class="btn agreeBtn" @click="handleBtn">确定</button>
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		deviceShareId,
		deviceShareCode,
		deviceShareAdd
	} from '@/api/6170/share.js'
	export default {
		data() {
			return {
				allSelected: false,
				shareShow: false,
				showView: false,
				isCounting: false,
				countdown: 0,
				permissions: [{
						value: "1",
						label: "灯光模式",
						checked: false
					},
					{
						value: "2",
						label: "激光模式",
						checked: false
					},
					{
						value: "3",
						label: "开机画面",
						checked: false
					},
					{
						value: "4",
						label: "人员信息登记",
						checked: false
					},
					{
						value: "5",
						label: "发送信息",
						checked: false
					},
					{
						value: "6",
						label: "产品信息",
						checked: false
					}
				],
				deviceInfo: {},
				phone: '',
				code: '',
				itemInfo: ''
			};
		},
		methods: {
			closePopup() {
				this.shareShow = false
			},
			toggleAllSelection() {
				this.allSelected = !this.allSelected;
				this.permissions.forEach(permission => permission.checked = this.allSelected);
			},
			toggleSelection(index) {
				this.$set(this.permissions[index], 'checked', !this.permissions[index].checked);
				this.allSelected = this.permissions.every(permission => permission.checked);
			},

			// 获取验证码
			async getPhoneCode() {
				const phoneNumber = this.phone
				const myreg = /^1[3456789]\d{9}$/;
				if (!phoneNumber) {
					uni.showToast({
						title: '手机号不能为空',
						icon: 'none',
						duration: 1000
					});
					return false;
				} else if (!myreg.test(phoneNumber)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 1000
					});
					return false;
				}
				try {
					await deviceShareCode({
						phonenumber: this.phone
					})
					// 更新倒计时状态
					this.isCounting = true;
					this.showView = true;
					this.countdown = 60
					const timer = setInterval(() => {
						this.countdown--
						if (this.countdown <= 0) {
							clearInterval(timer)
							this.isCounting = false;
							this.showView = false;
						}
					}, 1000)
					uni.showToast({
						title: '验证码已发送',
						icon: 'none'
					})
				} catch (error) {}
			},
			// 分享按钮提交
			async shareUp() {
				if (this.phone == '') {
					uni.showToast({
						title: '手机号不能为空',
						icon: 'none',
						duration: 1000
					})
					return false
				} else if (this.code == '') {
					uni.showToast({
						title: '验证码不能为空',
						icon: 'none',
						duration: 1000
					})
					return false
				}else if (!this.permissions.some(permission => permission.checked)) {
				    uni.showToast({
				      title: '请选择至少一个功能权限',
				      icon: 'none',
				      duration: 1000
				    });
				    return false;
				  }
				try {
					uni.showLoading({
						title: '分享中'
					})
					const selectedPermissions = this.permissions.filter(permission => permission.checked).map(
						permission =>
						permission.value);
					const res = await deviceShareAdd({
						phonenumber: this.phone,
						smsCode: this.code,
						deviceId: this.itemInfo.id,
						permission: selectedPermissions.join(',')
					})
					if (res.code == 200) {
						this.shareShow = true
						uni.hideLoading()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}

				} catch (error) {
					uni.showToast({
						title: error.msg,
						icon: 'none'
					});
				}


			},
			handleBtn() {
				this.shareShow = false
				uni.navigateTo({
					url: '/pages/6170/allShare/index'
				})
			},
		},
		onLoad() {
			const eventChannel = this.getOpenerEventChannel();
			// 监听 'shareDevice' 事件，获取传过来的数据
			eventChannel.on('shareDevice', (data) => {
				console.log(data, 'data1t111');
				this.itemInfo = data.data;
			})
		}
	};
</script>

<style scoped>
	.container {
		width: 100%;
		background: #121212;
		min-height: 100vh;
		padding: 30rpx;
	}

	.device-header {
		display: flex;
		align-items: center;
		margin-bottom: 15rpx;
	}

	.device-name {
		font-size: 32rpx;
		color: rgba(255, 255, 255, 0.87);
		margin-left: 25rpx;
		line-height: 50rpx;
		white-space: nowrap;
	}

	.ID {
		color: rgba(255, 255, 255, 0.6);
		font-size: 26rpx;
		display: flex;
		justify-content: space-between;
		position: relative;
	}

	.device-info {
		font-size: 28rpx;
		color: rgba(255, 255, 255, 0.87);
		padding-top: 10rpx;
		position: relative;
	}
	.deviceIMG {
		width: 100rpx;
		height: 100rpx;
		border-radius: 16rpx;
		position: relative;
		background-color: rgba(42, 42, 42, 0.6);
		display: flex;
		align-items: center;
	}
	.IMG {
		width: 68rpx;
		height: 50rpx;
		margin-left: 17%;
	}
	.title {
		font-size: 18px;
		color: white;
	}
	.device-info {
		width: 100%;
		padding: 10px;
		border-bottom: 1rpx solid rgba(255, 255, 255, 0.06);
	}
	.allSelect {
		color: rgba(255, 255, 255, 0.87);
		float: right;
	}
	.device-icon {
		width: 30px;
		height: 30px;
	}
	.device-id,
	.device-serial {
		color: white;
	}
	.permissions {
		width: 100%;
		margin-top: 20px;
		padding: 10px;
		border-bottom: 1rpx solid rgba(255, 255, 255, 0.06);
	}
	.permissions-title {
		color: rgba(255, 255, 255, 0.87);
		font-size: 30rpx;
		/* padding: 30rpx 0rpx; */
	}
	.checkbox {
		width: 40rpx;
		height: 40rpx;
		border: 2rpx solid rgba(255, 255, 255, 0.5);
		margin-right:30rpx;
		border-radius: 4rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.checkbox.checked {
		background-color: rgb(187, 230, 0);
		border-color: rgb(187, 230, 0);
	}
	.permission-item {
		margin-top:35rpx;
		display: flex;
		color: rgba(255, 255, 255, 0.87);
		line-height: 60rpx;
		align-items: center;
		cursor: pointer;
	}
	.recipient-info {
		width: 100%;
		margin-top: 20px;
		padding: 10px;
		border-radius: 5px;
	}
	.recipient-title {
		color: rgba(255, 255, 255, 0.87);
		font-size: 30rpx;
	}
	.phone-input {
		width: 100%;
		margin-top: 20rpx;
		padding: 40rpx 0;
		border-bottom: 1rpx solid rgba(255, 255, 255, 0.06);
		color: rgba(255, 255, 255, 0.87);
	}
	.verification-code {
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1rpx solid rgba(255, 255, 255, 0.06);
		padding: 40rpx 0;
		color: rgba(255, 255, 255, 0.87);
	}
	.get-code-btn {
		margin-top: 10px;
		background-color: transparent;
		color: rgba(255, 255, 255, 0.87);
		font-size: 30rpx;
		white-space: nowrap;
	}

	.share-btn {
		margin-top: 20px;
		width: 100%;
		padding: 5rpx;
		border-radius: 91rpx;
		background: rgba(187, 230, 0, 1);
	}

	/* 遮罩层 */
	.agreement-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.8);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 9999;
	}

	.popup-Title {
		color: rgba(255, 255, 255, 0.86);
		text-align: center;
		padding: 30rpx 0rpx;
	}

	.popup-buttons {
		display: flex;
		text-align: center;
	}

	/* 弹窗主体 */
	.agreement-popup {
		width: 60%;
		background: rgba(56, 57, 52, 0.4);
		border-radius: 40rpx;
		padding: 30rpx;
		text-align: center;
		border: 1px solid rgba(187, 230, 0, 0.3);
	}
	.svg {
		width: 58rpx;
		height:58rpx;
	}
	/* 通用按钮样式 */
	.btn {
		height: 60rpx;
		line-height: 60rpx;
		border-radius: 40rpx;
		font-size: 24rpx;
		margin: 10rpx auto;
		text-align: center;

	}

	/* 同意按钮 */
	.agreeBtn {
		background: rgba(187, 230, 0, 1);
		color: #232323;
		border: none;
		width: 170rpx !important;
	}
</style>