<template>
	<view class="u-page">
		<view class="" style="height: 44px;margin-bottom: 88rpx;">
			<u-navbar bgColor="#3c9cff" title="猫眼测试" @leftClick="leftClick">
			</u-navbar>
		</view>

		<view class="u-demo-block">
			<view class="u-demo-block__content steps">
				<u-steps :current="current1" direction="column">
					<u-steps-item class="steps-item" v-for="item in stepList" :key="item.id" :error="item.error"
						:title="item.title" :desc="item.desc">
					</u-steps-item>
					<!-- 	<u-steps-item  title="测试开门" desc="10:35">
					</u-steps-item>
					<u-steps-item  title="测试刷卡" desc="11:40">
					</u-steps-item>
					<u-steps-item  title="输入功耗" desc="11:40">
					</u-steps-item> -->
				</u-steps>
			</view>

		</view>
		<view class="upload" style="width: 100vw;">
			<u-button :disabled="uploadBtn" type="primary" size="large" @click="uploadTestMsg">完成测试并上传信息</u-button>
		</view>
		<u-modal :show="show1" :title="title" :content='content1' @confirm="confirm" :asyncClose="true">
			<u-button @click=pass slot="confirmButton" type="primary" text="通过" shape="circle"></u-button>
			<u-button @click="noPass" slot="confirmButton" type="error" text="不通过" shape="circle"></u-button>
		</u-modal>
		<u-popup customStyle="width: 70vw" :round="10" mode="center" :show="show7" @close="close">
			<view class="popupView" style="height: 150px;">
				<view class="power">
					功耗选择
				</view>
				<view class="inputNum" style="">
					<view class="">
						功耗：
					</view>
					<u--input placeholder="请输入功耗" border="surround" v-model="powerNum" @change="changeInput"></u--input>
				</view>

				<u-radio-group placement="row">
					<u-radio :customStyle="{margin: '15px 30px'}" v-for="(item, index) in radiolist1" :key="index"
						:label="item.value" :name="item.powerRefs" @change="radioChange" iconPlacement="right">
					</u-radio>

				</u-radio-group>

			</view>
		</u-popup>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				signalNum: '',
				uploadBtn: false,
				powerNum: 160,
				uploadMsg: {
					testProjectId: uni.getStorageSync('testProjectId'),
					deviceId: "",
					type: null
				},
				radiolist1: [{
						value: '通过',
						disabled: false,
						powerRefs: true

					},
					{
						value: '不通过',
						disabled: false,
						powerRefs: false
					}

				],
				show7: false,
				flag: 0,
				gatewayDeviceId: uni.getStorageSync('gatewayDeviceId'),
				show1: false,
				title: '温馨提示',
				content1: '系统已成功植入您的人脸，请问您的人脸验证是否通过',
				obj: {
					testProjectId: uni.getStorageSync('testProjectId'),
					deviceId: ""
				},
				show: false,
				current1: null,
				timer: null,
				stepList: [{
						id: 0,
						title: '测试信号',
						desc: '测试网关信号',
						error: false
					}, {
						id: 1,
						title: '测试人脸开门',
						desc: '测试人脸是否开门正常',
						error: false
					},
					{
						id: 2,
						title: '功耗输入',
						desc: '测试功耗',
						error: false
					}
				]
			}
		},
		methods: {
			changeInput(e) {
				console.log('change', e);
			},
			radioChange(n) {
				console.log(n);
				if (n) {
					this.current1 = 3
					this.show7 = false
					this.flag = 6
				} else {
					this.stepList[2].error = true
					this.show7 = false
				}
				this.show7 = false
			},
			pass() {
				this.current1 = 2
				this.show1 = false
				this.flag = 5
				this.show7 = true
			},
			noPass() {
				this.show1 = false
				this.stepList[1].error = true
			},
			async uploadTest() {
				uni.showLoading({
					mask: true,
					title: "正在上传本次测试结果",
					icon: 'none'
				})
				let {
					data: res
				} = await uni.$http.post('/api/v1/test/testResult', this.uploadMsg)
				uni.hideLoading()
				console.log(res);
				if (res.code == 200) {
					uni.$showMsg('上传成功')
					// this.uploadBtn = true
					setTimeout(() => {
						this.resetDevice()
					}, 1500)
				
				} else {
					uni.$showMsg('上传失败')
				}
			},
			// 重置设备
			async resetDevice() {
				uni.showLoading({
					mask: true,
					icon: 'none',
					title: '正在重置设备',
				})
				let {
					data: res
				} = await uni.$http.post('/api/v1/test/bluetooth/resetDevice', {
					deviceId: this.uploadMsg.deviceId
				})
				console.log(res);
				uni.hideLoading()
				if (res.code == 200) {
					this.$u.toast(`重置成功`)
					uni.switchTab({
						url:"/pages/bluetooth/bluetooth"
					})
				} else {
					this.$u.toast(`重置失败`)
				}
			},
			uploadTestMsg() {
				console.log(this.uploadMsg);
				if (this.flag == 0 || this.flag == 1 || this.flag == 2) {
						this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 0)
					// this.$set(this.uploadMsg, 'faceOpenRes', true)
					this.$set(this.uploadMsg, 'signalRes', false)
					this.$set(this.uploadMsg, 'faceOpenRes', false)
					this.$set(this.uploadMsg, 'powerRes', false)

					this.uploadTest()
				}
				if (this.flag == 3 || this.flag == 4) {
						this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 0)
					this.$set(this.uploadMsg, 'signalRes', true)
					this.$set(this.uploadMsg, 'faceOpenRes', false)
					this.$set(this.uploadMsg, 'powerRes', false)
					this.uploadTest()
				}
				if (this.flag == 5) {
						this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 0)
					this.$set(this.uploadMsg, 'signalRes', true)
					this.$set(this.uploadMsg, 'faceOpenRes', true)
						this.$set(this.uploadMsg, 'powerRes', false)
					this.$set(this.uploadMsg, 'powerNum', this.powerNum)
					console.log(this.uploadMsg);
					this.uploadTest()
				}
				if (this.flag == 6) {
						this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 1)
					this.$set(this.uploadMsg, 'signalRes', true)
					this.$set(this.uploadMsg, 'faceOpenRes', true)
					this.$set(this.uploadMsg, 'powerRes', true)
					this.$set(this.uploadMsg, 'powerNum', this.powerNum)
					console.log(this.uploadMsg);
					this.uploadTest()
				}
			},
		leftClick() {
			let a = this.stepList.every((item) => {
				return item.error == true
			})
			console.log(a);
			if (a) {
				console.log('模态框');
				uni.hideLoading()
				uni.showModal({
					title: '温馨提示',
					content: '确定取消上传本次测试信息吗？',
					success: res => {
						if (res.confirm) {
							// this.show1 = false
							this.$u.toast(`点击了确定`)
							this.flag = 99
							// console.log(this.flag);
							uni.navigateBack({
								delta: 1
							})
						} else {
							// this.$u.toast(`点击了取消`)
		
		
						}
					}
				})
			} else {
				uni.navigateBack({
					delta: 1
				})
		
			}
		
		},



			// 测试网关连接
			async testGateway() {
				uni.showLoading({
					title: '正在测试网关连接',
					icon: 'none'
				})
				const {
					data: res
				} = await uni.$http.get(`/api/v1/test/gateway/connection/${this.gatewayDeviceId}`)
				uni.hideLoading()
				if (res.data.isConnect == true) {
					this.flag = 1
					console.log('网关连接成功');
				} else {
					console.log('网关连接失败');
					this.$u.toast(`网关连接失败`)
				}
			},
			// 测试信号
			async testSignal() {
				uni.showLoading({
					mask: true,
					title: '信号测试中',
					icon: 'none'
				})
				const {
					data: res
				} = await uni.$http.post('/api/v1/test/bluetooth/checkSignal', this.obj)

				uni.hideLoading()
				if (res.data.isNormal == true) {
					this.current1 = 1
					this.flag = 3
					console.log('信号测试成功')
				} else {
					this.$u.toast(`信号测试失败`)
				}
			},

			// 检测人脸是否上传
			async faceHas() {
				uni.showLoading({
					mask: true,
					title: '检测人脸是否上传',
					icon: 'none'
				})
				let {
					data: res
				} = await uni.$http.get(`/api/v1/test/user/${uni.getStorageSync('userId')}/hasUploadFace`)
				uni.hideLoading()
				if (res.data.hasUploadFace === true) {
					console.log('人脸已上传');
					this.flag = 2
				} else {
					this.$u.toast(`人脸未上传，请去我的页面上传人脸`)
				}
			},
			// confirm() {
			// 	setTimeout(() => {
			// 		// 3秒后自动关闭
			// 		this.show = false;
			// 		uni.switchTab({
			// 			url: "/pages/mine/mine"
			// 		})
			// 	}, 2000)
			// },
			// 猫眼植入特征值
			async uploadFace() {
				uni.showLoading({
					mask: true,
					title: '下发人脸',
					icon: 'none'
				})
				let {
					data: res
				} =
				await uni.$http.post('/api/v1/test/bluetooth/addTestFaceToCam', {
					userId: uni.getStorageSync('userId'),
					deviceId: this.obj.deviceId
				})
				// console.log(res);
				uni.hideLoading()

				if (res.code === 200) {
					this.show1 = true
					console.log('植入成功');
					uni.showToast({

						duration: 1500,
						title: '人脸植入成功',
						icon: 'none'
					})
					this.show1 = true
					this.flag = 4

				} else {
					this.$u.toast(`人脸植入失败`)
					console.log('人脸上传失败');
					this.stepList[1].error = true
				}
			}
		},
		onLoad(option) {
			console.log(111);
			let item = JSON.parse(option.item)
			console.log(item);
				this.signalNum = item.signalNum
			this.obj.deviceId = item.deviceId
			this.uploadMsg.deviceId = item.deviceId
			this.uploadMsg.type = item.type
			this.testGateway().then(() => {
				if (this.flag != 1) {
					return
				}
				this.faceHas().then(() => {
					if (this.flag != 2) {
						return
					}
					this.testSignal().then(() => {
						if (this.flag != 3) {
							return
						}

						this.uploadFace()
					})
				})

			})


		}



	}
</script>

<style lang="scss">
	.steps {
		display: flex;
		justify-content: center;
		align-items: center;

		.u-steps-item {

			height: 25vh;
			width: calc(100vw - 20px);
			margin: 30px 10px;

			// background-color: red;
			.u-steps-item__line {
				height: 100px !important;
			}
		}

		// .u-steps-item {
		// 	width: 100vw;
		// 	height: 25vh;
		// 	background-color: blue;
		// }
	}



	.u-modal__button-group--confirm-button {
		flex-direction: row !important;

	}

	.u-modal .u-button {
		width: 180rpx !important;

	}

	.upload {
		position: fixed;
		bottom: 0;
		width: 100vw;
	}

	.power {
		text-align: center;
		margin: 13px 0 20px 0;
	}

	.inputNum {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 20rpx;
	}

	.u-radio-group {
		margin-top: 20rpx;
	}
</style>