<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item>虫情测报</el-breadcrumb-item>
		</el-breadcrumb>
		<search-bar @fun="getDisplayType" @fun2="getIDName" @fun3="getIsOnline">
			<template slot="search-box">
				<template v-if="role=='admin'">
					<el-select
						v-model="queryInfo.dver"
						class="select01"
						clearable
						size="mini"
						placeholder="请选择设备版本"
						@change="searchChange()"
					>
						<el-option
							v-for="item in dverGather"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</template>
				<!-- <template v-if="role=='admin'">
					<el-select
						v-model="queryInfo.selectUser"
						class="select01"
						clearable
						size="mini"
						filterable
						placeholder="请选择用户"
						@change="searchChange()"
					>
						<el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
				</template> -->
			</template>
		</search-bar>
		<template v-if="displayType==1">
			<el-row :gutter="10">
				<el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="6" v-for="item in equipList" :key="item.imei">
					<el-card class="box-card">
						<equip-item :is_online="item.device_status">
							<span
								v-show="userType==1"
								slot="set"
								class="superOperate el-icon-setting"
								@click="equipOperation(item.d_id)"
							></span>
							<span slot="title">虫情测报</span>
							<div slot="content" class="content">
								<p>
									<i class="iconfont icon-yonghu"></i>
									设备ID：{{item.device_id}}
								</p>
								<p>
									<i class="iconfont icon-biaoqian"></i>
									设备名称：{{item.device_name==""?"无":item.device_name}}
								</p>
								<p>
									<i class="iconfont icon-diannao"></i>在线状态：
									<span
										:class="{red:item.device_status==0,green:item.device_status==1}"
									>{{item.device_status==0?"离线":"在线"}}</span>
								</p>
								<p>
									<i class="iconfont icon-shijian"></i>
									最新上报时间 : {{item.status_time*1000 | formatTime}}
								</p>
								<p>
									<i class="iconfont icon-dizhi"></i>
									地址 : {{item.address || "无"}}
								</p>
								<p class="btns">
									<i class="iconfont icon-yemiancaozuo"></i>
									<el-button size="mini" type="info" @click="viewPhotoDialog(item.device_id,item.d_id)">查看图片</el-button>
									<el-button size="mini" type="warning" @click="EquipControl(item.d_id)">设备控制</el-button>
									<el-button size="mini" type="success" @click="showTimeControlDialog(item.device_id)">害虫统计</el-button>
									<el-button size="mini" type="danger" @click="dataDetails(item.device_id,item.d_id)">数据详情</el-button>
								</p>
							</div>
						</equip-item>
					</el-card>
				</el-col>
			</el-row>
		</template>
		<template v-if="displayType==2">
			<el-card class="box-card">
				<el-table :data="equipList" stripe style="width: 100%">
					<el-table-column prop="device_id" label="设备ID" width="180">
						<template slot-scope="scope">{{scope.row.device_id.slice(-8)}}</template>
					</el-table-column>
					<el-table-column prop="device_name" label="设备名称" width="180">
						<template slot-scope="scope">{{scope.row.device_name==""?"无":scope.row.device_name}}</template>
					</el-table-column>
					<el-table-column prop="device_status" label="在线状态">
						<template slot-scope="scope">
							<p style="color:#17bb89" v-if="scope.row.device_status==1">在线</p>
							<p style="color:#eb6765" v-if="scope.row.device_status==0">离线</p>
						</template>
					</el-table-column>
					<el-table-column prop="dver_num" label="设备版本"></el-table-column>
					<el-table-column prop="status_time" label="最新上报时间">
						<template slot-scope="scope">{{scope.row.status_time*1000 | formatTime}}</template>
					</el-table-column>
					<el-table-column label="操作" width="400">
						<template slot-scope="scope">
							<el-button size="mini" type="info" @click="viewPhotoDialog(scope.row.device_id,scope.row.d_id)">查看图片</el-button>
							<el-button size="mini" type="warning" @click="EquipControl(scope.row.d_id)">设备控制</el-button>
							<el-button
								size="mini"
								type="success"
								@click="showTimeControlDialog(scope.row.device_id)"
							>害虫统计</el-button>
							<el-button
								size="mini"
								type="danger"
								@click="dataDetails(scope.row.device_id,scope.row.d_id)"
							>数据详情</el-button>
						</template>
					</el-table-column>
				</el-table>
			</el-card>
		</template>
		<!-- 暂无数据 -->
		<div class="expertDiagnosis_referral_units_not" v-if="equipList.length<=0 && displayType==1">
			<img
				src="@/assets/images/zanwu.png"
				alt
				class="expertDiagnosis_referral_units_notImg"
			/>
		</div>
		<el-pagination
			v-if="equipList.length>0"
			background
			:page-size="8"
			layout="prev, pager, next"
			:total="totalNum"
			:current-page="queryInfo.page"
			@current-change="changePage"
		></el-pagination>
		<!-- 设备控制对话框 -->
		<el-dialog
			title="设备控制"
			:visible.sync="equipControlDialogVisible"
			@close="resetEquipControlDialogClosed"
			width="600px"
		>
			<div class="handAddForm">
				<el-form
					ref="equipContrlRef"
					:model="equipContrlForm"
					label-position="right"
					label-width="120px"
				>
					<el-form-item label="工作模式：" prop="ws">
						<el-select v-model="ws" placeholder="请选择害虫名称" @change="wsChange()">
							<el-option label="节能" :value="0"></el-option>
							<el-option label="常规" :value="1"></el-option>
							<el-option label="加强" :value="2"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="定时模式：" prop="ts">
						<el-select v-model="equipContrlForm.ts" placeholder="请选择害虫名称">
							<el-option label="光控" :value="0"></el-option>
							<el-option label="时控" :value="1"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item v-if="equipContrlForm.ts==1" label="开始结束时间：" prop="st">
						<el-col :span="11">
							<el-time-select
								placeholder="起始时间"
								v-model="equipContrlForm.st"
								:picker-options="{
								start: '00:00',
								step: '01:00',
								end: '24:00'
								}"
							></el-time-select>
						</el-col>
						<el-col class="line" :span="2">-</el-col>
						<el-col :span="11">
							<el-time-select
								placeholder="结束时间"
								v-model="equipContrlForm.et"
								:picker-options="{
								start: '00:00',
								step: '01:00',
								end: '24:00',
								}"
							></el-time-select>
						</el-col>
					</el-form-item>
					<el-form-item  v-if="equipContrlForm.ts!=1" label="定时时长：" prop="tt">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.tt" show-input :min="1" :max="10"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="落虫时间：" prop="collt">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.collt" show-input :min="1" :max="20"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="加热时间：" prop="htim">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.htim" show-input :min="1" :max="20"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="加热温度：" prop="hst">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.hst" show-input :min="75" :max="120"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="高温保护阈值:" prop="tph">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.tph" show-input :min="50" :max="70"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="低温保护阈值：" prop="tpl">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.tpl" show-input :min="1" :max="10"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="数据上传间隔：" prop="datt">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.datt" :step="10" show-input :min="10" :max="60"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="图片分辨率：" prop="imgres">
						<el-select v-model="equipContrlForm.imgres" placeholder="请选择害虫名称">
							<el-option label="高" :value="0"></el-option>
							<el-option label="中" :value="1"></el-option>
							<el-option label="低" :value="2"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item class="handAddFormBtn">
						<el-button type="primary" size="mini" @click="equipControlSubm()">确定</el-button>
						<el-button size="mini" @click="equipControlDialogVisible = false">取消</el-button>
					</el-form-item>
				</el-form>
			</div>
		</el-dialog>
		<!-- 管理员操作弹框 -->
		<el-dialog title="信息" :visible.sync="operationDialogVisible" width="30%">
			<el-divider content-position="left">强制操作</el-divider>
			<el-row>
				<el-button type="primary" size="mini" @click="equipBtnControl('takephoto')">拍照</el-button>
				<el-button type="primary" size="mini" @click="equipBtnControl('update')">升级</el-button>
				<el-button type="primary" size="mini" @click="equipBtnControl('reboot')">重启</el-button>
				<el-button type="primary" size="mini" @click="equipBtnControl('open_shake')">震动开启</el-button>
				<el-button type="primary" size="mini" @click="equipBtnControl('close_shake')">震动关闭</el-button>
			</el-row>
			<el-divider content-position="left">板子设置</el-divider>
			<el-row>
				<el-button type="primary" size="mini">查看原始imei</el-button>
				<el-button type="primary" size="mini">更改imei</el-button>
			</el-row>
		</el-dialog>
	</div>
</template>
<script>
import SearchBar from '@/components/SearchBar'
import EquipItem from '@/components/EquipItem'
export default {
	data() {
		return {
			//图表和表格切换
			displayType: '1',
			//设备列表
			equipList: [],
			role: 'admin',
			dverGather: [
				{ label: '全部', value: '' },
				{ label: '版本1', value: '1' },
				{ label: '版本2', value: '2' },
				{ label: '版本3', value: '3' },
				{ label: '版本4', value: '4' },
				{ label: '版本5', value: '5' }
			],
			userList: [
				{ label: '用户1', value: '用户1' },
				{ label: '用户2', value: '用户2' }
			],
			d_id: '',
			device_id: '',
			ws: 0, //工作模式
			equipContrlForm: {
				st: '',
				et: '',
				ts: 0,
				tt: null,
				collt: null,
				htim: null,
				hst: null,
				tph: null,
				tpl: null,
				datt: null,
				imgres: 0
			},
			//参数
			queryInfo: {
				page: 1,
				is_online: null,
				dver: null,
				selectUser: '',
				f_id: '',
				ename: ''
			},
			totalNum: 0,
			//设备控制对话框
			equipControlDialogVisible: false,
			operationDialogVisible: false
		}
	},
	mounted() {
		this.getEquipList()
	},
	computed: {
		userType: function () {
			//获取用户类型
			return window.sessionStorage.getItem('myuser_type')
		}
	},
	methods: {
		getIsOnline(data) {
			this.queryInfo.is_online = data
			this.queryInfo.page = 1
			this.getEquipList()
		},
		getIDName(data) {
			this.queryInfo.f_id = data.f_id
			this.queryInfo.ename = data.ename
			this.queryInfo.page = 1
			this.getEquipList()
		},
		getDisplayType(data) {
			this.displayType = data
		},
		//获取设备列表
		getEquipList() {
			this.$axios({
				method: 'POST',
				url: '/api/api_gateway?method=forecast.worm_lamp.lamp_list',
				data: this.qs.stringify({
					device_type_id: 3,
					page_size: 8,
					page: this.queryInfo.page,
					device_status: this.queryInfo.is_online,
					device_name: this.queryInfo.ename,
					device_id: this.queryInfo.f_id,
					dver_num: this.queryInfo.dver
				})
			}).then((res) => {
				if (res.data.message == '') {
					this.equipList = res.data.data.data
					this.totalNum = res.data.data.counts
				}
			})
		},
		//改变page
		changePage(val) {
			this.queryInfo.page = val
			this.getEquipList()
		},
		// 数据详情
		dataDetails(e_id, d_id) {
			this.device_id = e_id
			this.$router.push(`/index/cbdDataDetails/${e_id}/${d_id}`)
		},
		// 查看图片
		viewPhotoDialog(id,d_id) {
			this.device_id = id
			this.$router.push({path:`/index/cbdDataPhotos/${id}`,query: {d_id:d_id}})
		},
		//设备控制
		EquipControl(d_id) {
			this.d_id = d_id
			this.$axios({
				method: 'POST',
				url:
					'/api/api_gateway?method=forecast.send_control.device_control_info',
				data: this.qs.stringify({ d_id, get_type: 1 })
			}).then((res) => {
				if (Object.keys(res.data.data).length > 0) {
					let data = res.data.data
					this.equipContrlForm = data
				}
				this.equipControlDialogVisible = true
			})
		},
		//设备控制提交
		equipControlSubm() {
			this.$axios({
				method: 'POST',
				url: '/api/api_gateway?method=forecast.send_control.device_control',
				data: this.qs.stringify({
					device_type_id: 3,
					d_id: this.d_id,
					config: JSON.stringify(this.equipContrlForm)
				})
			}).then((res) => {
				if (res.data.data) {
					this.$message.success('设备控制修改成功')
				} else {
					his.$message.error('设备控制修改失败')
				}
				this.equipControlDialogVisible = false
			})
		},
		resetEquipControlDialogClosed() {
			this.$refs.equipContrlRef.resetFields()
			this.ws = 0
		},
		//害虫统计
		showTimeControlDialog(id) {
			this.device_id = id
			this.$router.push(`/index/pestsStats/${id}`)
		},
		//设备控制->工作模式切换
		wsChange() {
			this.$confirm('确定修改工作模式?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.$axios({
						method: 'POST',
						url: '/api/api_gateway?method=forecast.send_control.device_control',
						data: this.qs.stringify({
							device_type_id: 3,
							d_id: this.d_id,
							work_type: this.ws
						})
					}).then((res) => {
						if (res.data.message == '') {
							this.$message({
								type: 'success',
								message: '修改成功!'
							})
						} else {
							this.$message({
								type: 'error',
								message: res.data.message
							})
						}
					})
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消修改'
					})
				})
		},
		equipOperation(d_id) {
			this.d_id = d_id
			this.operationDialogVisible = true
		},
		equipBtnControl(cmd) {
			this.$axios({
				method: 'POST',
				url:
					'/api/api_gateway?method=forecast.send_control.admin_device_control',
				data: this.qs.stringify({
					cmd,
					device_type_id: 3,
					d_id: this.d_id
				})
			}).then((res) => {
				if (res.data.message == '') {
					this.$message.success('指令下发成功!')
				}
			})
		},


		// 设备版本、用户筛选
		searchChange() {
			console.log(this.queryInfo.selectUser)
			this.getEquipList()
		},
	},
	components: {
		SearchBar,
		EquipItem
	}
}
</script>
<style lang="less" scoped>
.handAddForm {
	/deep/.el-form-item {
		margin-bottom: 8px;
	}
	.handAddFormBtn {
		text-align: right;
	}
}
.sliderParent {
	display: flex;
	.block {
		flex: 1;
		margin-right: 10px;
		/deep/.el-slider__runway,
		/deep/.el-slider__bar {
			height: 10px;
		}
	}
	> span {
		width: 40px;
	}
}
.swi-box {
	text-align: center;
	padding: 10px 0;
	label {
		color: #333;
		margin-right: 8px;
		font-size: 14px;
	}
	i.el-icon-edit {
		margin-right: 8px;
	}
}

.real-time {
	display: flex;
	justify-content: left;
	align-items: center;
	padding: 20px 0;
	.left {
		margin-right: 10px;
		.icon-box {
			border-radius: 50%;
			width: 46px;
			height: 46px;
			padding: 5px;
			box-sizing: border-box;
			margin-bottom: 3px;
			img {
				width: 100%;
				height: auto;
			}
		}
		.aisle {
			text-align: center;
			font-size: 14px;
			color: #666;
		}
	}
	.right {
		color: #333;
		.num {
			font-weight: 600;
			font-size: 20px;
			margin-bottom: 3px;
		}
		.unit {
			font-size: 14px;
			color: #666;
		}
	}
}
.checkData {
	text-align: right;
	font-size: 14px;
	span {
		cursor: pointer;
	}
}
.red {
	color: rgb(235, 103, 101);
}
.green {
	color: rgb(23, 187, 137);
}
.el-date-editor--time-select {
	width: 100% !important;
}
.el-form .line {
	text-align: center;
}
.el-select {
	width: 100%;
}
// 暂无数据
.expertDiagnosis_referral_units_not {
	width: 272px;
	margin: 0 auto;
}
</style>
