<template>
	<view>
		<view class="status_bar"></view>
		<view class="" style="position: relative;top: 44px;">
			<view style="position: fixed;z-index: 100;width: 100%;">
				<uni-nav-bar @clickLeft="clickLeft" left-icon="back" left-text="返回" title="设备详情"></uni-nav-bar>
			</view>
			<view class="info">
				<view class="info_item">
					<image :src="eqinfo.item.is_online==1?'http://www.hnyfwlw.com:8006/bigdata_app/image/cb/onBg.png':'http://www.hnyfwlw.com:8006/bigdata_app/image/cb/offBg.png'"
					 mode="" class="bgi"></image>
					<p style="font-size: 32rpx;" @click="copy(eqinfo.item.imei)">设备 ID：{{eqinfo.item.imei}}
					<image src="http://www.hnyfwlw.com:8006/bigdata_app/image/environment/fuzhi.png" mode="" class="tishi"></image>
					</p>
					<p>设备名称：{{eqinfo.item.device_name}}</p>
					<p>设备型号：{{eqinfo.item.dtype}}</p>
					<p>最近上报时间：{{eqinfo.item.addtime|timeFormat()}}</p>
					<p>设备地址：{{eqinfo.item.address}}</p>
				</view>
			</view>
			<view class="control">
				<view class="control_item" v-if="kongtf" @click="control">
					<image :src="'http://www.hnyfwlw.com:8006/bigdata_app'+'/image/environment/8.png'" mode=""></image>
					<p>设备控制</p>
				</view>
				<view class="control_item" v-if="shujutf" @click="charts">
					<image :src="'http://www.hnyfwlw.com:8006/bigdata_app'+'/image/environment/10.png'" mode=""></image>
					<p>历史数据</p>
				</view>
				<view class="control_item" v-if="simtf" @click="sim">
					<image :src="'http://www.hnyfwlw.com:8006/bigdata_app'+'/image/environment/9.png'" mode=""></image>
					<p>SIM卡详情</p>
				</view>
				<view class="control_item" @click="repairs">
					<image :src="'http://www.hnyfwlw.com:8006/bigdata_app'+'/image/environment/7.png'" mode=""></image>
					<p>一键报修</p>
				</view>
			</view>
			<view class="realtime">
				<p class="realtime_title">实时数据</p>
				<view class="realtime_text" v-if="newdatesTF">
					<view class="realtime_text_item">
						<view class="realtime_text_item_info" v-for="item,index in olddatas" :key="index">
							<view class="item_info_img">
								<image :src="'http://www.hnyfwlw.com:8006/bigdata_app'+item.icon" mode=""></image>
							</view>
							<view class="item_info_text">
								<p>{{item.txt}}</p>
								<p style="margin-top: 20rpx;">{{item.value}}</p>
							</view>
						</view>
					</view>
				</view>
				<view class="realtime_tishi" v-else>
					暂无数据
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				eqinfo: {},
				olddata: {},
				olddatas: [],
				newdatesTF: false,
				work: "",
				shujutf: false,
				kongtf: false,
				simtf: false
			}
		},
		methods: {
			modification() {
				this.olddatas = [{
						icon: '/image/cb/icon02.png',
						txt: '设备开关',
						value: Number(this.olddata.ds) == 1 ? '开机' : "关机",
					},
					{
						icon: '/image/cb/icon05.png',
						txt: '工作状态',
						value: this.work
					},
					{
						icon: '/image/prevention/44.png',
						txt: '电击次数',
						value: this.olddata.ct
					},
					{
						icon: '/image/prevention/33.png',
						txt: '定时时长',
						value: this.olddata.tt
					}, {
						icon: '/image/cb/icon08.png',
						txt: '环境温度(℃)',
						value: this.olddata.new_tem
					}, {
						icon: '/image/cb/icon07.png',
						txt: '环境湿度(%)',
						value: this.olddata.new_hum
					}, {
						icon: '/image/prevention/icon16.png',
						txt: '信号强度',
						value: this.olddata.csq
					}, {
						icon: '/image/prevention/100.png',
						txt: '清虫间隔(min)',
						value: this.olddata.clt_t
					}, {
						icon: '/image/prevention/101.png',
						txt: '雨控状态',
						value: Number(this.olddata.rps) == 1 ? "雨控" : '正常'
					}, {
						icon: '/image/prevention/102.png',
						txt: '温控状态',
						value: Number(this.olddata.tps) == 1 ? "温控" : '正常'
					},
					{
						icon: '/image/prevention/105.png',
						txt: '充电电压',
						value: this.olddata.cv
					},
					{
						icon: '/image/prevention/106.png',
						txt: '电池电压',
						value: Number(this.olddata.bv)
					}
				]
			},
			async history() { //设备列表
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.worm_lamp.device_history_data',
					data: {
						device_type_id: 2,
						device_id: this.eqinfo.item.imei
					}
				})
				if (res.data.length != 0) {
					this.olddata = res.data[0].d_h_t
					this.newdatesTF = true
					if (Number(this.olddata.ws) == 0) {
						this.work = "待机"
					} else if (Number(this.olddata.ws) == 1) {
						this.work = "工作"
					} else {
						this.work = "充电"
					}
				} else {
					this.newdatesTF = false
				}
				this.modification()
			},
			clickLeft() {
				uni.navigateBack({
					delta: 1
				})
			},
			sim() {
				// sim卡详情
				uni.navigateTo({
					url: "./sim?id=" + this.eqinfo.item.d_id
				})
			},
			repairs(){
				console.log(this.eqinfo.item)
				var device_id = this.eqinfo.item.imei
				uni.navigateTo({
					url: "../afterSale/addafter?device_id="+ device_id +"&device_type="+ 2
				})
			},
			control() { //设备控制
				uni.navigateTo({
					url: "./control?id=" + this.eqinfo.item.d_id
				})
			},
			charts() { //历史数据
				uni.navigateTo({
					url: "./ucharts?d_id=" + this.eqinfo.item.d_id + "&imei=" + this.eqinfo.item.imei
				})
			},
			copy(item){
				uni.setClipboardData({
				    data: item , 
				    success: function () {
				        console.log('success');
				    }
				});
			},
		},
		onLoad(option) {
			this.$forceUpdate()
			this.eqinfo.item = JSON.parse(option.shebei)
			console.log(JSON.parse(option.shebei))
			this.history()
			uni.getStorage({
				key: "jurisdiction",
				success: (res) => {
					console.log(JSON.parse(res.data))
					let items = JSON.parse(res.data).filter((item) => {
						return item.purview_name == "防治系统"
					})
					let items2 = items[0].children.filter((item) => {
						return item.purview_name == "杀虫灯"
					})
					var arr = items2[0].children
					console.log(arr)
					for (var i = 0; i < arr.length; i++) {
						switch (arr[i].purview_name) {
							case "设备操作":
								this.kongtf = true
								break
							case "设备控制":
								this.kongtf = true
								break
							case "SIM卡状态":
								this.simtf = true
								break
							case "SIM卡":
								this.simtf = true
								break
							case "数据详情":
								this.shujutf = true
								break
						}
					}
				},
			})
		}
	}
</script>

<style lang="scss">
	.info {
		width: 100%;
		position: absolute;
		top: 44px;

		.info_item {
			width: 90%;
			margin: 0 auto;
			height: 260rpx;
			padding: 26rpx 50rpx;
			position: relative;
			box-sizing: border-box;

			.bgi {
				width: 100%;
				height: 260rpx;
				position: absolute;
				top: 0;
				left: 0;
				z-index: -1;
			}

			p {
				font-size: 24rpx;
				color: #FFFFFF;
				margin-bottom: 10rpx;
				.tishi{
					width: 30rpx;
					height: 30rpx;
					margin: 0rpx 0 0 20rpx;
				}
			}
		}
	}

	.control {
		width: 90%;
		position: absolute;
		top: 372rpx;
		left: 5%;
		display: flex;
		justify-content: space-between;
		text-align: center;
		padding: 0 30rpx;
		box-sizing: border-box;

		.control_item {
			width: 120rpx;
			height: 120rpx;

			image {
				width: 70rpx;
				height: 70rpx;
			}

			p {
				font-size: 24rpx;
			}
		}
	}

	.realtime {
		width: 100%;
		position: absolute;
		top: 500rpx;

		.realtime_title {
			font-weight: 700;
			width: 90%;
			margin: 0 auto;
		}

		.realtime_tishi {
			width: 90%;
			margin: 0 auto;
			text-align: center;
			font-size: 32rpx;
			padding-top: 40rpx;
		}

		.realtime_text {
			width: 90%;
			margin: 0 auto;

			.realtime_text_item {
				width: 100%;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				margin-bottom: 50rpx;

				.realtime_text_item_info {
					width: 48%;
					box-shadow: 0 0 10rpx #bcb9ca;
					margin-top: 20rpx;
					display: flex;
					padding: 20rpx 20rpx;
					box-sizing: border-box;

					.item_info_img {
						width: 30%;
						text-align: center;
						margin-right: 30rpx;

						image {
							width: 64rpx;
							height: 64rpx;
							margin-top: 10rpx;
						}
					}

					.item_info_text {
						font-size: 24rpx;

					}
				}
			}
		}

	}
</style>
