<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">
			<u-button :disabled="uploadBtn" type="primary" size="large" @click="uploadTestMsg">完成测试并上传信息</u-button>
		</view>
		<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 {
					powerNum: 160,
				subType:null,
				uploadBtn: false,
				uploadMsg: {
					testProjectId: uni.getStorageSync('testProjectId'),
					deviceId: "",
					type: null
				
				},
				radiolist1: [{
						value: '通过',
						disabled: false,
						powerRefs: true

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

				],
				show7: false,
				flag: 0,
				timer: null,
				signalNum: '',
				gatewayDeviceId: uni.getStorageSync('gatewayDeviceId'),

				show: false,
				current1: 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(e);
			},

			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({
					mask: true,
					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(obj) {
				uni.showLoading({
					mask: true,
					title: '正在测试信号',
					icon: 'none'
				})
				const {
					data: res
				} = await uni.$http.post('/api/v1/test/bluetooth/checkSignal', obj)
				uni.hideLoading()
				if (res.data.isNormal == true) {
					this.current1 = 1
					this.flag = 2
					console.log('信号测试成功');
				} else {
					this.stepList[0].error = true
					console.log('信号测试失败');
					this.$u.toast(`信号测试失败`)
				}
			},

			// 测试开门
			async openDoor() {
				uni.showLoading({
					mask: true,
					title: '蓝牙开门测试中',
					icon: 'none'
				})
				const {
					data: res
				} = await uni.$http.post('/api/v1/test/bluetooth/openDoor', {
					deviceId: this.uploadMsg.deviceId
				})
				uni.hideLoading()
				if (res.code == 200) {
					this.current1 = 2
					this.flag = 3
					// uni.$u.toast(`开门成功`)
					this.show7 = true
					console.log('开门成功');
				} else {
					this.stepList[1].error = true
					console.log('开门失败');
				}
			},

			radioChange(n) {
				console.log(n);
				if (n) {
					this.current1 = 3
					this.show7 = false
					this.flag = 4
				} else {
					this.stepList[2].error = true
					this.show7 = false
				}
				// this.show7 = false

			},
			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)
				
					// uni.switchTab({
					// 	url: "/pages/bluetooth/bluetooth"
					// })
				} else {
					uni.$showMsg('上传失败')
				}
			},
			uploadTestMsg() {

				if (this.flag == 1 || this.flag == 0) {
					console.log('失败的案例');
					console.log(this.uploadMsg);
					if(this.subType==1){
						this.$set(this.uploadMsg, 'subType', this.subType)
					}
					this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 0)
					this.$set(this.uploadMsg, 'signalRes', false)
					this.$set(this.uploadMsg, 'doorOpenRes', false)
					this.$set(this.uploadMsg, 'powerRes', false)
					
					
					this.uploadTest()
				}
				if (this.flag == 2) {
					if(this.subType==1){
						this.$set(this.uploadMsg, 'subType', this.subType)
					}
					this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 0)
					this.$set(this.uploadMsg, 'signalRes', true)
					this.$set(this.uploadMsg, 'doorOpenRes', false)
					this.$set(this.uploadMsg, 'powerRes', false)

					this.uploadTest()
				}
				if (this.flag == 3) {
					if(this.subType==1){
						this.$set(this.uploadMsg, 'subType', this.subType)
					}
					this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 0)
					this.$set(this.uploadMsg, 'signlRes', true)
					this.$set(this.uploadMsg, 'doorOpenRes', true)
					this.$set(this.uploadMsg, 'powerRes', false)
					this.uploadTest()
				}
				if (this.flag == 4) {
					if(this.subType==1){
						this.$set(this.uploadMsg, 'subType', this.subType)
					}
					this.$set(this.uploadMsg, 'signalNum', this.signalNum)
					this.$set(this.uploadMsg, 'status', 1)
					this.$set(this.uploadMsg, 'signlRes', true)
					this.$set(this.uploadMsg, 'doorOpenRes', true)
					this.$set(this.uploadMsg, 'powerRes', true)
							this.$set(this.uploadMsg, 'powerNum', this.powerNum)
					console.log(this.uploadMsg);
					this.uploadTest()
				}

				// else {
				// 	console.log('失败的案例');
				// 	console.log(this.uploadMsg);
				// 	this.$set(this.uploadMsg, 'status', 0)

				// 	this.uploadTest()
				// }
			},
			async resetDevice() {
				uni.showLoading({

					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(`重置成功`)
				} else {
					this.$u.toast(`重置失败`)
					
					
				}
			},

		},
		onLoad(option) {
			let item = JSON.parse(option.item)
			console.log(item);
			this.subType=item.subType
			this.signalNum = item.signalNum
			this.uploadMsg.deviceId = item.deviceId
			this.uploadMsg.type = item.type
			// this.room.deviceId = item.deviceId
			const obj = {
				testProjectId: uni.getStorageSync('testProjectId'),
				deviceId: item.deviceId
			}

			this.testGateway().then(() => {
				if (this.flag != 1) {
					return
				}
				this.testSignal(obj).then(() => {
					if (this.flag != 2) {
						return
					}
					this.openDoor().then(() => {
						// if (this.flag != 3) {
						// 	return
						// }

					})


				})
			})

		},

	}
</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;
		// }

	}

	.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>
<!-- 	this.timer = setInterval(() => {
				this.current1++
				console.log(this.current1);
				if (this.current1 === 2) {
					this.btnShow = false
				}
				if (this.current1 === 3) {
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					uni.hideLoading()
					this.stepList[3].error = true
					clearInterval(this.timer)
				}
			}, 3000) -->