<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item>防治系统</el-breadcrumb-item>
			<el-breadcrumb-item :to="{ path: '/index/xycb' }">性诱测报</el-breadcrumb-item>
			<el-breadcrumb-item>数据详情</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- {{options}}--{{options2}}--{{options3}} -->
		<div class="equipInfo">
			<div class="equipMsg">
				<span>设备ID：{{this.queryInfo.e_id}}</span>
				<span>设备名称：{{equipStatus.equip_name || '无'}}</span>
				<span>位置：{{equipStatus.lng}}</span>
			</div>
			<DateSearch @dateChange="dateChange"></DateSearch>
		</div>
		<div class="ctrl-check">
			<el-button type="primary" size="mini" @click="refresh">刷新</el-button>
		</div>
		<!-- 统计图 --> 
		<div>
			<div>
				<el-row :gutter="10">
					<el-col :md="24" :lg="14">
						<el-card class="box-card">
							<div class="charts">
								<template v-if="Object.keys(options).length>0">
									<highcharts :options="options"></highcharts>
								</template>
								<template v-else>
									<div style="text-align:center;font-size:20px;line-height:450px">暂无数据</div>
								</template>
							</div>
						</el-card>
					</el-col>
					<el-col :md="24" :lg="10">
						<el-card class="box-card">
							<div class="pie">
								<template v-if="device_model==1">
									<!-- <highcharts :options="options2" :key="1"></highcharts> -->
									<template v-if="Object.keys(options2).length>0">
										<highcharts :options="options2"></highcharts>
									</template>
									<template v-else>
										<div style="text-align:center;font-size:20px;line-height:450px">暂无数据</div>
									</template>
								</template>
								<template v-if="device_model==2">
									<!-- <highcharts :options="options3" :key="2"></highcharts> -->
									<template v-if="Object.keys(options3).length>0">
										<highcharts :options="options3"></highcharts>
									</template>
									<template v-else>
										<div style="text-align:center;font-size:20px;line-height:450px">暂无数据</div>
									</template>
								</template>
							</div>
						</el-card>
					</el-col>
				</el-row>
				<div class="dataTableSearch">
					<div>
						<el-button type="primary" size="mini">导出</el-button>
					</div>
				</div>
			</div>
			<el-card class="box-card">
				<el-table :data="tableData" stripe style="width: 100%">
					<el-table-column
						v-for="(item,index) in tableHeadTxt"
						:key="index"
						:prop="item[1]"
						:label="item[0]"
						:width="setWidth(item)"
					>
						<template slot-scope="scope">{{scope.row[scope.column.property]}}</template>
					</el-table-column>
				</el-table>
				<el-pagination
					background
					layout="prev, pager, next"
					:total="total"
					:current-page="queryInfo.page"
					@current-change="changePage"
				></el-pagination>
			</el-card>
		</div>
	</div>
</template>

<script>
import DateSearch from '@/components/DateSearch'
export default {
	data() {
		return {
			device_model: this.$route.query.device_model, //性诱1型或II型
			btnSelected: '',
			lineTime: '',
			dataTime: '',
			options: {},
			worms: [
				{
					value: 1,
					label: '蛾子'
				},
				{
					value: 2,
					label: '蚊子'
				}
			],
			options2: {},
			options3: {},
			// 状态信息
			equipStatus: {},
			// 统计图表信息
			equipChartData: [],
			//总数据条数
			total: 0,
			// 动态表头
			tableHeadTxt: [
				['型号', 'proj'],
				['设备开关', 'ds'],
				['工作状态', 'ws'],
				['环境温度', 'at'],
				['环境湿度', 'ah'],
				['充电电压', 'cv'],
				['电池电压', 'bv'],
				['充电状态', 'cs'],
				['电池状态', 'bs'],
				['CPU温度', 'cpu_tmp'],
				['信号强度', 'csq'],
				['版本号', 'dver'],
				['上报时间', 'addtime']
			],
			// 表格数据
			tableData: [],
			timeRange: '',
			queryInfo: {
				begin: '',
				end: '',
				e_id: this.$route.params.e_id,
				d_id: this.$route.params.d_id,
				page: 1
			},
			//图表实时否有数据
			isEchartData: true,
			//表格和图表按钮样式切换参数
			display: '0', //0代表表格、1代表图表
			displayView: true,
			// 表格和图表显示切换控制
			showControl: true,
			//echarts图表参数
			echartOptions: [],
			// 动态表头
			tableHead: [],
			// 表格数据
			hisData: []
		}
	},
	created() {},
	mounted() {
		// this.getChartDataList()
		// this.getHisDataList()
	},
	methods: {
		//获取折线数据
		getChartDataList() {
			this.$axios({
				method: 'POST',
				url: '/api/api_gateway?method=forecast.worm_lamp.device_polyline_data',
				data: this.qs.stringify({
					device_type_id: 4,
					device_id: this.queryInfo.e_id,
					start_time: this.queryInfo.begin,
					end_time: this.queryInfo.end
				})
			}).then((res) => {
				if (res.data.message == '' && res.data.data.length > 0) {
					var ChartData = [
						{
							name: '温度（°C）',
							dat: []
						},
						{
							name: '湿度（%）',
							dat: []
						},
						{
							name: '红外计数值',
							dat: []
						},
						{
							name: '高压计数值',
							dat: []
						},
						{
							name: 'cpu温度（°C）',
							dat: []
						}
					]
					var data = res.data.data
					for (let item of data) {
						ChartData[0].dat.unshift([
							item.addtime * 1000 + 8 * 3600000,
							Number(item.temperature)
						])
						ChartData[1].dat.unshift([
							item.addtime * 1000 + 8 * 3600000,
							Number(item.humidity)
						])
						if (Object.keys(item.others).length == 2) {
							this.device_model = 1 //2型
							ChartData[2].dat.unshift([
								item.addtime * 1000 + 8 * 3600000,
								Number(item.infr_ct)
							])
							ChartData[3].dat.unshift([
								item.addtime * 1000 + 8 * 3600000,
								Number(item.volt_ct)
							])
						} else {
							this.device_model = 2 //1型
							ChartData[4].dat.unshift([
								item.addtime * 1000 + 8 * 3600000,
								Number(item.cpu_tmp)
							])
						}
					}
					this.options = {
						chart: {
							zoomType: 'x'
						},
						title: {
							text: '环境温湿度',
							align: 'left'
						},
						subtitle: {
							text: ''
						},
						xAxis: {
							crosshair: true, //十字基准线
							type: 'datetime',
							dateTimeLabelFormats: {
								//根据时间间距X轴自动显示哪种格式
								millisecond: '%H:%M:%S.%L',
								second: '%H:%M:%S',
								minute: '%H:%M',
								hour: '%H:%M',
								day: '%m-%d',
								week: '%m-%d',
								month: '%Y-%m',
								year: '%Y'
							}
						},
						yAxis: {
							title: {
								text: ''
							}
						},
						legend: {
							// layout: 'vertical',
							align: 'right',
							verticalAlign: 'top'
						},
						plotOptions: {
							series: {
								label: {
									connectorAllowed: false
								},
								pointStart: 2010
							}
						},
						series: [
							{
								type: 'spline',
								name: ChartData[0].name,
								data: ChartData[0].dat
							},
							{
								type: 'spline',
								name: ChartData[1].name,
								data: ChartData[1].dat
							}
						],
						colors: ['#00e29d', '#6cbbff', '#ff3f3f'],
						credits: {
							//去掉默认的highcharts.com
							enabled: false
						},
						tooltip: {
							// crosshairs: true,
							shared: true, //折线共享
							headerFormat: '<b>{point.x:%Y-%m-%e %H:%M:%S}</b><br>'
						}
					}
					this.options2 = {
						chart: {
							zoomType: 'x'
						},
						title: {
							text: '红外计数值 、高压计数值',
							align: 'left'
						},
						subtitle: {
							text: ''
						},
						xAxis: {
							crosshair: true, //十字基准线
							type: 'datetime',
							dateTimeLabelFormats: {
								//根据时间间距X轴自动显示哪种格式
								millisecond: '%H:%M:%S.%L',
								second: '%H:%M:%S',
								minute: '%H:%M',
								hour: '%H:%M',
								day: '%m-%d',
								week: '%m-%d',
								month: '%Y-%m',
								year: '%Y'
							}
						},
						yAxis: {
							title: {
								text: ''
							}
						},
						legend: {
							// layout: 'vertical',
							align: 'right',
							verticalAlign: 'top'
						},
						plotOptions: {
							series: {
								label: {
									connectorAllowed: false
								},
								pointStart: 2010
							}
						},
						series: [
							{
								type: 'spline',
								name: ChartData[2].name,
								data: ChartData[0].dat
							},
							{
								type: 'spline',
								name: ChartData[3].name,
								data: ChartData[1].dat
							}
						],
						colors: ['#00e29d', '#6cbbff', '#ff3f3f'],
						credits: {
							//去掉默认的highcharts.com
							enabled: false
						},
						tooltip: {
							// crosshairs: true,
							shared: true, //折线共享
							headerFormat: '<b>{point.x:%Y-%m-%e %H:%M:%S}</b><br>'
						},
						responsive: {
							rules: [
								{
									condition: {
										maxWidth: 500
									},
									chartOptions: {
										legend: {
											layout: 'horizontal',
											align: 'center',
											verticalAlign: 'bottom'
										}
									}
								}
							]
						}
					}
					this.options3 = {
						chart: {
							zoomType: 'x'
						},
						title: {
							text: 'CPU温度',
							align: 'left'
						},
						subtitle: {
							text: ''
						},
						xAxis: {
							crosshair: true, //十字基准线
							type: 'datetime',
							dateTimeLabelFormats: {
								//根据时间间距X轴自动显示哪种格式
								millisecond: '%H:%M:%S.%L',
								second: '%H:%M:%S',
								minute: '%H:%M',
								hour: '%H:%M',
								day: '%m-%d',
								week: '%m-%d',
								month: '%Y-%m',
								year: '%Y'
							}
						},
						yAxis: {
							title: {
								text: ''
							}
						},
						legend: {
							// layout: 'vertical',
							align: 'right',
							verticalAlign: 'top'
						},
						plotOptions: {
							series: {
								label: {
									connectorAllowed: false
								},
								pointStart: 2010
							}
						},
						series: [
							{
								type: 'spline',
								name: ChartData[4].name,
								data: ChartData[0].dat
							}
						],
						colors: ['#00e29d', '#6cbbff', '#ff3f3f'],
						credits: {
							//去掉默认的highcharts.com
							enabled: false
						},
						tooltip: {
							// crosshairs: true,
							shared: true, //折线共享
							headerFormat: '<b>{point.x:%Y-%m-%e %H:%M:%S}</b><br>'
						},
					}
				}else{
					this.options = {};
					this.options2 = {};
					this.options3 = {};
				}
			})
		},
		//获取表格数据
		getHisDataList() {
			this.$axios({
				method: 'POST',
				url: '/api/api_gateway?method=forecast.worm_lamp.device_history_data',
				data: this.qs.stringify({
					device_type_id: 4,
					device_id: this.queryInfo.e_id,
					start_time: this.queryInfo.begin,
					end_time: this.queryInfo.end,
					page: this.queryInfo.page
				})
			}).then((res) => {
				if (res.data.message == '') {
					this.total = res.data.data.counts
					var data = res.data.data.data
					if (data.length > 0) {
						this.tableData = []
						for (var i = 0; i < data.length; i++) {
							var dht = data[i].d_h_t
							dht.addtime = this.formatTime(dht.addtime*1000)
							console.log(dht.ws)
							dht.ws = dht.ws ? '工作' : '待机'
							dht.cs = dht.cs ? '充电中' : '未充电'
							if(dht.bs == 0){
								dht.bs = '正常'
							}else if(dht.bs == 1){
								dht.bs = '欠压'
							}else if(dht.bs == 0){
								dht.bs = '过压'
							}
							dht.rps = dht.rps ? '正常' : '雨控'
							dht.ds = dht.ds ? '关' : '开'
							dht.tps = dht.tps ? '正常' : '温控'
							dht.lps = dht.lps ? '夜晚' : '白天'
							if (dht.lamp != undefined) {
								if (dht.ws == 1) {
									if (stat.lamp == 1) {
										dht.blbs = '工作中'
									} else if (dht.lux == 0) {
										dht.blbs = 'N/A（未检测到传感器）'
									} else {
										dht.blbs = '异常'
									}
								} else {
									dht.blbs = '关闭'
								}
							} else {
								dht.blbs = '--'
							}
							this.tableData.push(dht)
						}
					}else{
						this.tableData = []
					}
				}
			})
		},
		//改变page
		changePage(val) {
			this.queryInfo.page = val
			this.getHisDataList()
		},
		dateChange(data) {
			this.queryInfo.page = 1
			this.queryInfo.begin = data.begin
			this.queryInfo.end = data.end
			this.getHisDataList()
			this.getChartDataList()
		},
		setWidth(item) {
			if (item[1] == 'addtime') {
				return 180 + 'px'
			}
		},
		refresh() {
			this.$axios({
				method: 'POST',
				url: '/api/api_gateway?method=forecast.send_control.get_device_config',
				data: this.qs.stringify({
					device_type_id: 4,
					d_id: this.queryInfo.d_id,
					control_type: 'data'
				})
			}).then((res) => {
				if (res.data.data) {
					this.$message.success('刷新成功')
				} else {
					this.$message.err('刷新失败')
				}
			})
		}
	},
	components: {
		DateSearch
	}
}
</script>

<style lang='less' scoped>
.box-card {
	position: relative;
}
.equipInfo {
	.equipMsg {
		font-size: 14px;
		color: #3d3d3d;
		i {
			font-size: 16px;
			color: #14a478;
		}
		span {
			margin-left: 5px;
		}
	}
	display: flex;
	justify-content: space-between;
	margin-bottom: 15px;
}

.time-check {
	display: flex;
	justify-content: space-between;
	.el-input__inner {
		margin-left: 10px;
		width: 222px;
	}
}

.chartsSelect {
	text-align: right;
	position: absolute;
	z-index: 999;
	left: 0;
	right: 21px;
}
.charts,
.pie {
	height: 423px;
}
.equipStatus {
	display: flex;
	justify-content: center;
	margin: 10px 0;
	.equipIcon {
		margin-right: 14%;
	}
	.equipParams {
		min-width: 80px;
		line-height: 27px;
		p {
			color: #a6a6a6;
		}
	}
}

.dataTableSearch {
	display: flex;
	justify-content: space-between;
	margin: 20px 0;
	.el-input__inner {
		width: 222px;
	}
}

// .check-btns{display:flex;justify-content: space-between;margin-bottom:20px}
.echarts {
	width: 100%;
	height: 300px;
}
.ctrl-check {
	margin-bottom: 20px;
}
</style>