<template>
	<div class="widgets-home" ref="main">
		<div class="widgets-content">
			<div class="widgets" ref="widgets">
				<div class="widgets-wrapper">
					<!-- <div :class="bindClass()">XXXXXXXX超过预设值，请及时处理</div> -->
					<el-row :gutter="6" style="padding: 10px;">
						<el-col>
							<el-card header="产量及完成度" style="border-radius: 5px;box-shadow: none;">
								<div class="itemsWrap">
									<div class="items">
										<div class="item-number" v-if="monthData.电石渣">{{ monthData.电石渣.total_production
											}}</div>
										<div class="item-name">月电石渣产量</div>
										<el-progress :percentage="90" :text-inside="true" :stroke-width="14">
											<span class="item-percent">90%</span>
										</el-progress>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.原料磨">{{ monthData.原料磨.total_production
											}}</div>
										<div class="item-name">月生料产量</div>
										<el-progress :percentage="95" :text-inside="true" :stroke-width="14">
											<span class="item-percent">95%</span>
										</el-progress>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.回转窑">{{ monthData.回转窑.total_production
											}}</div>
										<div class="item-name">月熟料产量</div>
										<el-progress :percentage="98" :text-inside="true" :stroke-width="14">
											<span class="item-percent">98%</span>
										</el-progress>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.水泥磨">{{ monthData.水泥磨.total_production
											}}</div>
										<div class="item-name">月水泥产量</div>
										<el-progress :percentage="95" :text-inside="true" :stroke-width="14">
											<span class="item-percent">95%</span>
										</el-progress>
									</div>
									<div style="display: inline-block;">
										<div class="items">
											<div class="item-number" v-if="yearData.电石渣">{{
						yearData.电石渣.total_production }}</div>
											<div class="item-name">本年累计电石渣产量</div>
											<el-progress :percentage="75" status="success" :text-inside="true"
												:stroke-width="14">
												<span class="item-percent">75%</span>
											</el-progress>
										</div>
										<div class="items">
											<div class="item-number" v-if="yearData.原料磨">{{
						yearData.原料磨.total_production }}</div>
											<div class="item-name">本年累计生料产量</div>
											<el-progress :percentage="70" status="success" :text-inside="true"
												:stroke-width="14">
												<span class="item-percent">70%</span>
											</el-progress>
										</div>
										<div class="items">
											<div class="item-number" v-if="yearData.回转窑">{{
						yearData.回转窑.total_production }}</div>
											<div class="item-name">本年累计熟料产量</div>
											<el-progress :percentage="80" status="success" :text-inside="true"
												:stroke-width="14">
												<span class="item-percent">80%</span>
											</el-progress>
										</div>
										<div class="items">
											<div class="item-number" v-if="yearData.水泥磨">{{
						yearData.水泥磨.total_production }}</div>
											<div class="item-name">本年累计水泥产量</div>
											<el-progress :percentage="75" status="success" :text-inside="true"
												:stroke-width="14">
												<span class="item-percent">75%</span>
											</el-progress>
										</div>
									</div>
								</div>
							</el-card>
						</el-col>
						<el-col>
							<el-card header="能耗" style="border-radius: 5px;box-shadow: none;">
								<div class="itemsWrap">
									<div class="items">
										<div class="item-number" v-if="monthData.电石渣">{{ monthData.电石渣.elec_consume_unit
											}}</div>
										<div class="item-name">月电石渣分布电耗</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.原料磨">{{ monthData.原料磨.elec_consume_unit
											}}</div>
										<div class="item-name">月生料分布电耗</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.回转窑">{{ monthData.回转窑.elec_consume_unit
											}}</div>
										<div class="item-name">月熟料分布电耗</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.回转窑">{{ monthData.回转窑.coal_consume_unit
											}}</div>
										<div class="item-name">熟料月单位标煤耗</div>
									</div>
									<div style="display: inline-block;">
										<div class="items">
											<div class="item-number" v-if="monthData.回转窑">{{
						monthData.回转窑.celec_consume_unit }}</div>
											<div class="item-name">熟料月综合电耗</div>
										</div>

										<div class="items">
											<div class="item-number" v-if="monthData.回转窑">{{
						monthData.回转窑.cen_consume_unit }}</div>
											<div class="item-name">熟料月综合能耗</div>
										</div>
										<div class="items">
											<div class="item-number" v-if="monthData.水泥磨">{{
						monthData.水泥磨.elec_consume_unit }}</div>
											<div class="item-name">月水泥粉磨分布电耗</div>
										</div>
										<div class="items">
											<div class="item-number" v-if="monthData.水泥磨">{{
						monthData.水泥磨.cen_consume_unit }}</div>
											<div class="item-name">水泥月综合能耗</div>
										</div>
									</div>
								</div>
								<div class="itemsWrap">
									<div class="items">
										<div class="item-number" v-if="yearData.电石渣">{{ yearData.电石渣.elec_consume_unit
											}}</div>
										<div class="item-name">本年度电石渣分布电耗</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="yearData.原料磨">{{ yearData.原料磨.elec_consume_unit
											}}</div>
										<div class="item-name">本年度生料分布电耗</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="yearData.回转窑">{{ yearData.回转窑.elec_consume_unit
											}}</div>
										<div class="item-name">本年度熟料分布电耗</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="yearData.回转窑">{{ yearData.回转窑.coal_consume_unit
											}}</div>
										<div class="item-name">本年度熟料单位标煤耗</div>
									</div>
									<div style="display: inline-block;">
										<div class="items">
											<div class="item-number" v-if="yearData.回转窑">{{
						yearData.回转窑.celec_consume_unit }}</div>
											<div class="item-name">本年度熟料综合电耗</div>
										</div>
										<div class="items">
											<div class="item-number" v-if="yearData.回转窑">{{
						yearData.回转窑.cen_consume_unit }}</div>
											<div class="item-name">本年度熟料综合能耗</div>
										</div>
										<div class="items">
											<div class="item-number" v-if="yearData.水泥磨">{{
						yearData.水泥磨.elec_consume_unit }}</div>
											<div class="item-name">本年度水泥粉磨分布电耗</div>
										</div>
										<div class="items">
											<div class="item-number" v-if="yearData.水泥磨">{{
						yearData.水泥磨.cen_consume_unit }}</div>
											<div class="item-name">本年度水泥综合能耗</div>
										</div>
									</div>
								</div>
							</el-card>
						</el-col>
						<el-col>
							<el-card header="成本统计" style="border-radius: 5px;box-shadow: none;">
								<div class="itemsWrap">
									<div class="items">
										<div class="item-number" v-if="monthData.电石渣">{{
						monthData.电石渣.production_cost_unit }}</div>
										<div class="item-name">月电石渣成本</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.原料磨">{{
						monthData.原料磨.production_cost_unit }}</div>
										<div class="item-name">月生料成本</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.回转窑">{{
											monthData.回转窑.production_cost_unit }}</div>
										<div class="item-name">月熟料成本</div>
									</div>
									<div class="items">
										<div class="item-number" v-if="monthData.水泥磨">{{
											monthData.水泥磨.production_cost_unit }}</div>
										<div class="item-name">月水泥成本</div>
									</div>
								</div>
							</el-card>
						</el-col>
						<el-col>
							<el-card header="报警" style="border-radius: 5px;box-shadow: none;">
								<el-badge :value="warningNum" :max="99" style="margin: 10px;">
									<div class="items" style="margin:0;" @click="interEvent">
										<div class="item-number">能耗超标报警</div>
									</div>
								</el-badge>
								<el-badge :value="warningNum2" :max="99" style="margin: 10px;">
									<div class="items" style="margin:0" @click="interEquipment">
										<div class="item-number">设备检验</div>
									</div>
								</el-badge>
							</el-card>
						</el-col>
					</el-row>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			time: '',
			day: '',
			year_s: '',
			month_s: '',
			yearData: [],
			monthData: [],
			warningNum: 0,
			warningNum2: 0,
		}
	},
	mounted() {
		this.$emit('on-mounted');
		let that = this;
		var myDate = new Date();
		let year = myDate.getFullYear();
		let month = myDate.getMonth() + 1;
		that.year_s = year;
		that.month_s = month;
		that.getYearData();
		that.getMonthData();
		that.getWraningNum();
		// this.showTime();
		// setInterval(()=>{
		// 	this.showTime()
		// },1000)
	},
	methods: {
		// 动态绑定Class
		bindClass(type) {
			let classInfo = {
				socketDom: true, redColor: false,
				orangeColor: false, yellowColor: false, blueColor: true
			}
			if (type == '') {
				classInfo.redColor = true
			}
			return classInfo
		},
		init() {
			var that = this
			if (typeof (WebSocket) === "undefined") {
				alert("您的浏览器不支持socket")
			} else {
				this.ws = new WebSocket(this.path + tool.data.get("TOKEN"))
				//监听是否连接成功
				this.ws.onopen = () => {
					// console.log("socket连接成功");
					setTimeout(() => {
						that.ws.send(JSON.stringify({ 'type': 'event' }))
					}, 500)
				}

				//接听服务器发回的信息并处理展示
				this.ws.onmessage = (msg) => {
					let data = JSON.parse(msg.data)
					console.log(data)
					that.getWraningNum();
					// if(data.type=='event'){
					// that.bindClass();
					// }if(data.type=='ticket'){

					// }if(data.type=='remaind'){

					// }
				};
				// 监听并处理error事件
				this.ws.onerror = function (error) {
					console.log('ws断开,尝试重连')
					setTimeout(() => {
						this.ws = null;
						this.init()
					}, 5000)
				}
			}
		},
		getMessage(msg) {
			console.log(msg);
		},
		close() {
			if (this.ws) {
				this.ws.close();
				this.ws = null;
				console.log("socket已经关闭");
			}
		},
		//获取报警事件数量
		getWraningNum() {
			let that = this;
			that.$API.ecm.event.list.req({ cates__code__in: 'consume_exceed', is_handled: 0, page: 1 }).then((res) => {
				that.warningNum = res.count;
			})
			that.$API.em.equipment.list.req({ tag: 'near_check', type: 20, page: 1 }).then((res) => {
				debugger;
				that.warningNum2 = res.count;
			})
		},
		getYearData() {
			let that = this;
			let obj = {};
			obj.year_s = this.year_s;
			obj.type = 'year_s';
			obj.page = 0;
			this.$API.enm.enstat.req(obj).then((res) => {
				let yearData = {};
				res.forEach(item => {
					let label = item.mgroup_name;
					yearData[label] = item;
				});
				that.yearData = yearData;
			})
		},
		getMonthData() {
			let that = this;
			let obj = {};
			obj.year_s = this.year_s;
			obj.month_s = this.month_s;
			obj.type = 'month_s';
			obj.page = 0;
			that.$API.enm.enstat.req(obj).then((res) => {
				let monthData = {};
				res.forEach(item => {
					let label = item.mgroup_name;
					monthData[label] = item;
				});
				that.monthData = monthData;
			})
		},
		showTime() {
			this.time = this.$TOOL.dateFormat(new Date(), 'hh:mm:ss')
			this.day = this.$TOOL.dateFormat(new Date(), 'yyyy年MM月dd日')
		},
		interEvent() {
			this.$router.replace({ path: '/events' })
		},
		interEquipment() {
			this.$router.replace({ path: '/em/equipmentc' })
		},

	}
}
</script>

<style scoped lang="scss">
.items {
	display: inline-block;
	width: 200px;
	padding: 20px;
	margin: 10px;
	box-sizing: border-box;
	border: 1px solid #cccccc;
	border-radius: 10px;
	box-shadow: 0 0 10px 6px #eeeeee57;
}

.item-number {
	font-weight: 600;
	font-size: 24px;
	color: #1e2126;
	letter-spacing: .1px;
	text-align: left;
	line-height: 36px;
}

.item-name {
	font-size: 14px;
	color: #4e5b71;
	letter-spacing: .1px;
	text-align: left;
	line-height: 20px;
}

.item-percent {
	font-size: 12px;
	color: #ffffff;
	letter-spacing: .08px;
	text-align: left;
	line-height: 12px;
}

.socketDom {
	position: absolute;
	top: 10vh;
	left: 50%;
	font-size: 20px;
	font-weight: bold;
	transform: translateX(-50%);
	z-index: 101;
	color: #ffffff;
	border: 1px solid #99fffe;
	background: rgba(11, 101, 140, 0.451);
	padding: 10px 20px;
	border-radius: 5px;

}

.redColor {
	color: red;
	border: 1px solid red;
	background: rgba(255, 0, 0, 0.2);
}

.orangeColor {
	color: orange;
	border: 1px solid orange;
	background: rgba(255, 165, 0, .2);
}

.yellowColor {
	color: yellow;
	background: rgba(255, 255, 0, .2);
	border: 1px solid yellow;
}

.blueColor {
	color: #2378f7;
	background: rgb(35, 120, 247, .2);
	border: 1px solid #2378f7;
}
</style>
