<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item>测报系统</el-breadcrumb-item>
			<el-breadcrumb-item>糖醋测报</el-breadcrumb-item>
		</el-breadcrumb>
		<search-bar @fun="getDisplayType" @fun2="getIDName" @fun3="getIsOnline">
		</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
								slot="set"
								v-show="userType==1"
								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="lookSIMCode(item.d_id)">SIM卡</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="lookSIMCode(scope.row.d_id)">SIM卡查询</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>
		<el-pagination
			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="180px"
				>
					<el-form-item label="数据上传时间间隔(分钟)：" prop="datt">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.datt" show-input :min="5" :max="60"></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="60"></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="60"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="加液排水轮询时间(天)：" prop="jp">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.jp" show-input :min="1" :max="7"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="水泵打开(秒):" prop="pump_sec">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.pump_sec" show-input :min="10" :max="300"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="电磁阀打开(秒)：" prop="shake_sec">
						<div class="sliderParent">
							<div class="block">
								<el-slider v-model="equipContrlForm.shake_sec" show-input :min="10" :max="300"></el-slider>
							</div>
						</div>
					</el-form-item>
					<el-form-item label="定时开始时间：" prop="st">
						<el-time-select
							placeholder="起始时间"
							v-model="equipContrlForm.st"
							:picker-options="{
								start: '00:00',
								step: '01:00',
								end: '24:00'
								}"
						></el-time-select>
					</el-form-item>
					<el-form-item label="定时结束时间：" prop="et">
						<el-time-select
							placeholder="结束时间"
							v-model="equipContrlForm.et"
							:picker-options="{
								start: '00:00',
								step: '01:00',
								end: '24:00',
								minTime: equipContrlForm.st
								}"
						></el-time-select>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="equipBtnControl('takephoto')" size="mini">拍照</el-button>
						<el-button type="primary" @click="equipBtnControl('reboot')" size="mini">重启</el-button>
						<el-button type="primary" @click="equipBtnControl('paramconf')" size="mini">获取最新配置</el-button>
					</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="300px">
			<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('boot1')">禁用</el-button>
			</el-row>
		</el-dialog>
		<!-- SIM卡对话框 -->
		<el-dialog title="SIM卡" :visible.sync="simCodeVisible" width="30%">
			<div class="simCodeBox">
				<div>
					<span class="simCodeTitle">ICCID：</span>
					<div>{{sim.iccid}}</div>
				</div>
				<div>
					<span class="simCodeTitle">状态：</span>
					<div>{{sim.account_status | simStatus}}</div>
				</div>
				<div>
					<span class="simCodeTitle">套餐：</span>
					<div>
						<el-progress :show-text="false" :percentage="100"></el-progress>
					</div>
					<span>{{sim.data_plan}}</span>
				</div>
				<div>
					<span class="simCodeTitle">已用流量：</span>
					<div>
						<template v-if="!isNaN(data_usage_Per)">
							<el-progress :show-text="false" :percentage="data_usage_Per"></el-progress>
						</template>
					</div>
					<span>{{sim.data_usage}}MB</span>
				</div>
				<div>
					<span class="simCodeTitle">剩余流量：</span>
					<div>
						<template v-if="!isNaN(data_balance_per)">
							<el-progress :show-text="false" :percentage="data_balance_per"></el-progress>
						</template>
					</div>
					<span>{{sim.data_balance}}MB</span>
				</div>
				<div>
					<span class="simCodeTitle">到期时间：</span>
					<div>{{sim.expiry_date*1000 | formatTime}}</div>
				</div>
				<div class="operateBtn">
					<el-button size="mini" @click="simCodeVisible=false">关闭</el-button>
				</div>
			</div>
		</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: '',
			equipContrlForm: {
				datt: null,
				collt: null,
				htim: null,
				jp: null,
				pump_sec: null,
				shake_sec: null,
				st: '',
				et: ''
			},
			//参数
			queryInfo: {
				page: 1,
				is_online: null,
				dver: null,
				selectUser: '',
				f_id: '',
				ename: ''
			},
			totalNum: 0,
			//设备控制对话框
			equipControlDialogVisible: false,
			operationDialogVisible: false,
			simCodeVisible: false,
			sim: {
				iccid: '',
				account_status: 0, //卡状态 0-7 未知 测试期 沉默期 使用中 停机 停机保号 预销号 销号
				data_plan: 0, //套餐大小
				data_usage: 0, //当月用量
				data_balance: 0, //剩余流量
				expiry_date: 0 //到期日期
			}
		}
	},
	mounted() {
		this.getEquipList()
	},
	computed: {
		userType: function () {
			//获取用户类型
			return window.sessionStorage.getItem('myuser_type')
		},
		data_usage_Per: function () {
			let aa = Number(this.sim.data_usage / this.sim.data_plan) * 100
			return aa
		},
		data_balance_per: function () {
			let aa = Number(this.sim.data_balance / this.sim.data_plan) * 100
			return aa
		}
	},
	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: 9,
					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/tcDataDetail/${e_id}/${d_id}`)
		},
		// 查看图片
		viewPhotoDialog(id,d_id) {
			this.device_id = id
			this.$router.push({path:`/index/tcDataPhotos/${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) => {
				console.log(res.data)
				if (Object.keys(res.data.data).length > 0) {
					let data = res.data.data
					console.log(res.data.data)
					data.st =
						String(data.st).length == 1
							? '0' + data.st + ':00'
							: data.st + ':00'
					data.et =
						String(data.et).length == 1
							? '0' + data.et + ':00'
							: data.et + ':00'
					this.equipContrlForm = data
					console.log(data)
				}
				this.equipControlDialogVisible = true
			})
		},
		//设备控制提交
		equipControlSubm() {
			// console.log(this.equipContrlForm.st)
			let st = this.equipContrlForm.st.slice(0, 2)
			let et = this.equipContrlForm.et.slice(0, 2)
			st = st.indexOf('0') == 0 ? st.slice(1) : st
			et = et.indexOf('0') == 0 ? et.slice(1) : et
			this.$axios({
				method: 'POST',
				url:
					'/api/api_gateway?method=forecast.send_control.tccbd_device_control',
				data: this.qs.stringify({
					device_type_id: 9,
					d_id: this.d_id,
					cmd: 'set',
					datt: this.equipContrlForm.datt,
					collt: this.equipContrlForm.collt,
					htim: this.equipContrlForm.htim,
					jp: this.equipContrlForm.jp,
					pump_sec: this.equipContrlForm.pump_sec,
					shake_sec: this.equipContrlForm.shake_sec,
					st: st,
					et: et
				})
			}).then((res) => {
				if (res.data.message == '') {
					this.$message.success('设备控制修改成功')
				} else {
					this.$message.error('设备控制修改失败')
				}
				this.equipControlDialogVisible = false
			})
		},
		resetEquipControlDialogClosed() {
			this.equipContrlForm = {}
		},
		//SIM卡
		lookSIMCode(d_id) {
			this.$axios({
				method: 'POST',
				url: '/api/api_gateway?method=forecast.send_control.device_sim',
				data: this.qs.stringify({
					d_id: d_id
				})
			}).then((res) => {
				if (res.data.message == '') {
					this.sim.iccid = res.data.data[0].iccid
					this.$axios({
						method: 'POST',
						url: '/api/api_gateway?method=forecast.send_control.sim_query',
						data: this.qs.stringify({
							iccid: this.sim.iccid
						})
					}).then((res) => {
						if (res.data.message == '') {
							let data = eval('(' + res.data.data.data + ')')
							let cardInfo = data.data.card_list[0]
							this.sim = cardInfo
						}
						this.simCodeVisible = true
					})
				}
			})
		},
		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.tccbd_device_control',
				data: this.qs.stringify({
					cmd,
					device_type_id: 9,
					d_id: this.d_id
				})
			}).then((res) => {
				if (res.data.message == '') {
					this.$message.success('指令下发成功!')
				}
			})
		}
	},
	filters: {
		simStatus(val) {
			switch (val) {
				case 0:
					return '未知'
				case 1:
					return '测试期'
				case 2:
					return '沉默期'
				case 3:
					return '使用中'
				case 4:
					return '停机'
				case 5:
					return '停机保号'
				case 6:
					return '预销号'
				case 7:
					return '销号'
			}
		}
	},
	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;
	}
}
.simCodeBox {
	> div {
		line-height: 40px;
		display: flex;
		align-content: center;
		.simCodeTitle {
			width: 100px;
			text-align: right;
			font-size: 14px;
			color: #333;
		}
		> div {
			flex: 1;
			.el-progress {
				margin-top: 17px;
			}
		}
		span {
			width: 85px;
			font-size: 12px;
			padding-left: 3px;
			color: #909090;
		}
		.el-input {
			width: 60%;
		}
	}
	.operateBtn {
		display: block;
		text-align: right;
	}
}
.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%;
}
</style>
