<template>
	<div class="home-page">
		<div>
			<navbar />
		</div>
		<el-row class="main-top" :gutter="16">
			<el-col :span="12" style="position: relative">
				<div class="title">
					<svg-icon icon-class="home-produce" />
					<span class="title-inner">生产总览</span>
					<div class="date-tabs">
						<el-tabs
							v-model="activeName"
							@tab-click="handleClick"
							:stretch="true">
							<el-tab-pane
								:label="'\u2002\u2002日\u2002\u2002'"
								name="日"></el-tab-pane>
							<el-tab-pane
								:label="'\u2002\u2002周\u2002\u2002'"
								name="周"></el-tab-pane>
							<el-tab-pane
								:label="'\u2002\u2002月\u2002\u2002'"
								name="月"></el-tab-pane>
							<el-tab-pane
								:label="'\u2002\u2002年\u2002\u2002'"
								name="年"></el-tab-pane>
						</el-tabs>
					</div>
					<div class="current-date">( {{ showTime }} )</div>
				</div>
				<el-row class="box">
					<el-col :span="6" class="num-box shadow">
						<div class="num-style">{{ homeProduce.quantity }}</div>
						<div class="unit-style">产量/千片</div>
					</el-col>
					<el-col :span="4" class="num-box shadow">
						<div class="num-style">{{ homeProduce.yieldRate }}</div>
						<div class="unit-style">良品率/%</div>
					</el-col>
					<el-col :span="8" class="num-box shadow">
						<div class="num-style">{{ homeProduce.endWorkOrderQuantity }}</div>
						<div class="unit-style">完成工单数量/千片</div>
					</el-col>
					<el-col :span="6" class="num-box">
						<div class="num-style">{{ homeProduce.endOrderQuantity }}</div>
						<div class="unit-style">完成订单数量/个</div>
					</el-col>
				</el-row>
			</el-col>
			<el-col :span="12" style="position: relative">
				<div class="title">
					<svg-icon icon-class="home-device" />
					<span class="title-inner">设备总览</span>
					<!-- <div class="date-tabs">
						<div class="current-time">
							<span>
								{{ topTime + timeZone + ' '+ topDate }}
							</span>
						</div>
					</div> -->
				</div>
				<el-row class="box">
					<el-col :span="6" class="num-box shadow">
						<div class="num-style">{{ homeDevice.allEquipment }}</div>
						<div class="unit-style">设备总数/台</div>
					</el-col>
					<el-col :span="6" class="num-box shadow">
						<div class="num-style">{{ homeDevice.onlineEquipment }}</div>
						<div class="unit-style">在线设备总数/台</div>
					</el-col>
					<el-col :span="6" class="num-box shadow">
						<div class="num-style">{{ homeDevice.errorEquipment }}</div>
						<div class="unit-style">故障设备总数/台</div>
					</el-col>
					<el-col :span="6" class="num-box">
						<div class="num-style">{{ homeDevice.stopEquipment }}</div>
						<div class="unit-style">停机设备总数/台</div>
					</el-col>
				</el-row>
			</el-col>
		</el-row>
		<div class="main-middle">
			<div
				class="item"
				:class="{ shadow: index !== middleMenu.length - 1 }"
				v-for="(item, index) in middleMenu"
				@click="goPage(item)"
				:key="index">
				<div style="width: 160px; margin: 0 auto; padding-top: 20px">
					<svg-icon :icon-class="item.icon" style="font-size: 56px" />
					<div class="title-box">
						<div class="title">{{ item.name }}</div>
						<div class="triangle">
							<span class="c1"></span>
							<span class="c2"></span>
							<span class="c3"></span>
						</div>
					</div>
				</div>
			</div>
		</div>
		<el-row class="main-bottom" :gutter="16">
			<el-col :span="12">
				<div class="box">
					<div class="title-box">
						<svg-icon icon-class="home-order-monitor" />
						<span class="title">工单监控</span>
					</div>
					<div class="order-box">
						<div
							class="order-item"
							v-for="(item, index) in homeProduce.orderList"
							:key="index">
							<div>
								<span class="order-name">{{ item.orderName }}</span>
								<span class="order-value">{{ item.completeRate * 100 }}%</span>
							</div>
							<el-progress
								:percentage="item.completeRate * 100"
								:text-inside="false"
								:stroke-width="15"
								:show-text="false"></el-progress>
						</div>
					</div>
				</div>
			</el-col>
			<el-col :span="12">
				<div class="box">
					<div class="title-box">
						<svg-icon icon-class="home-device-alarm" />
						<span class="title">设备告警</span>
					</div>
					<base-table
						:page="1"
						:limit="100000"
						:table-props="tableProps"
						:table-data="homeDevice.list"
						:max-height="tableH" />
				</div>
			</el-col>
		</el-row>
		<div
			class="main-footer"
			style="
				color: #c7c7c7;
				user-select: none;
				font-size: 14px;
				letter-spacing: 1px;
				height: 30px;
				display: grid;
				place-content: center;
			">
			&copy; 中建材智能自动化研究院有限公司
		</div>
	</div>
</template>

<script>
import Navbar from './components/Navbar';
import moment from 'moment';
import tableHeightMixin from '@/mixins/tableHeightMixin';
import {
	getHomeOrder,
	getHomeOrderNum,
	getHomeEquipment,
	getHomeEquipmentAlarmList,
} from '@/api/home';

export default {
	name: 'Home',
	mixins: [tableHeightMixin],
	components: { Navbar },
	computed: {
		gradientBackground() {
			return {
				'background-image':
					'linear-gradient(90deg, #f0f0f0 25%, rgba(255, 255, 255, 0) 25%, rgba(255, 255, 255, 0) 50%, #f0f0f0 50%, #f0f0f0 75%, rgba(255, 255, 255, 0) 75%, rgba(255, 255, 255, 0))',
				'background-size': '100px 100px', // 调整条纹的大小
			};
		},
	},
	data() {
		return {
			activeName: '日',
			topDate: '',
			topTime: '',
			timeZone: '',
			middleMenu: [
				{ name: '生产管理', url: 'productLine24h', icon: 'home-work-menu' },
				{
					name: '能源管理',
					url: 'EnergyQuantityRealtime',
					icon: 'home-order-menu',
				},
				{ name: '设备管理', url: 'PlanConfig', icon: 'home-device-menu' },
				{ name: '质量管理', url: 'qualityScrapLog', icon: 'home-quality-menu' },
				{ name: '仓库管理', url: 'warehouse-info', icon: 'home-store-menu' },
			],
			heightNum: 640,
			tableProps: [
				{
					prop: 'equipmentName',
					label: '设备名称',
					showOverflowtooltip: true,
				},
				{
					prop: 'alarmContent',
					label: '告警内容',
					showOverflowtooltip: true,
				},
				{
					prop: 'updateTime',
					label: '告警时间',
					width: 180,
					filter: (val) => moment(val).format('yyyy-MM-DD HH:mm:ss'),
				},
			],
			homeProduce: {
				quantity: undefined,
				yieldRate: undefined,
				endWorkOrderQuantity: undefined,
				endOrderQuantity: undefined,
				orderList: [],
			},
			homeDevice: {
				allEquipment: undefined,
				onlineEquipment: undefined,
				errorEquipment: undefined,
				stopEquipment: undefined,
				list: [],
			},
			times: [],
			showTime: '',
		};
	},
	created() {
		this.getTime();
		this.handleClick();
		this.getHomeEquipment();
		this.showTime = moment(new Date()).subtract(0, 'days').format('YYYY-MM-DD');
	},
	beforeDestroy() {
		clearInterval(this.timer);
	},
	methods: {
		handleClick() {
			let start =
      Date.parse(new Date())
			let end =
      Date.parse(new Date())
			switch (this.activeName) {
				case '日':
					this.showTime = moment(new Date())
						.subtract(0, 'days')
						.format('YYYY-MM-DD');
					this.times = [start, end];
					break;
				case '周':
					start =
						moment(new Date()).subtract(6, 'days').format('x')
					this.showTime =
						moment(new Date()).subtract(6, 'days').format('MM-DD') +
						' ~ ' +
						moment(new Date()).subtract(0, 'days').format('MM-DD');
					this.times = [start, end];
					break;
				case '月':
					start =
						moment(new Date()).subtract(1, 'month').format('x')
					this.showTime =
						moment(new Date()).subtract(1, 'month').format('MM-DD') +
						' ~ ' +
						moment(new Date()).subtract(0, 'days').format('MM-DD');
					this.times = [start, end];
					break;
				case '年':
					start =
						moment(new Date()).subtract(1, 'year').format('x')
					this.showTime =
						moment(new Date()).subtract(1, 'year').format('YYYY') +
						' ~ ' +
						moment(new Date()).subtract(0, 'days').format('YYYY');
					this.times = [start, end];
					break;
				default:
					console.log(val);
			}
			this.getHomeOrder();
		},
		getTime() {
			let _this = this;
			this.timer = setInterval(function () {
				_this.topDate = moment().format('YYYY.MM.DD');
				let temp = moment().format('A-hh:mm').split('-');
				_this.timeZone = temp[0];
				_this.topTime = temp[1];
			}, 1000);
		},
		getHomeOrder() {
			getHomeOrder(this.times).then((response) => {
				if (response.hasOwnProperty('data')) {
					this.homeProduce.quantity = response.data.quantity;
					this.homeProduce.yieldRate = response.data.yieldRate * 100;
					this.homeProduce.endWorkOrderQuantity =
						response.data.endWorkOrderQuantity;
					this.homeProduce.orderList = response.data.orderMonitorVO;
				} else {
					this.homeProduce = {
						quantity: undefined,
						yieldRate: undefined,
						endWorkOrderQuantity: undefined,
						endOrderQuantity: undefined,
						orderList: [],
					};
				}
			});
			getHomeOrderNum(this.times).then((response) => {
				if (response.hasOwnProperty('data')) {
					this.homeProduce.endOrderQuantity = response.data.endOrderQuantity;
				} else {
					this.homeProduce.endOrderQuantity = '-';
				}
			});
		},
		getHomeEquipment() {
			getHomeEquipment().then((response) => {
				if (response.hasOwnProperty('data')) {
					this.homeDevice.allEquipment = response.data.allEquipment;
					this.homeDevice.onlineEquipment = response.data.onlineEquipment;
					this.homeDevice.errorEquipment = response.data.errorEquipment;
					this.homeDevice.stopEquipment = response.data.stopEquipment;
				} else {
					this.homeDevice = {
						allEquipment: undefined,
						onlineEquipment: undefined,
						errorEquipment: undefined,
						stopEquipment: undefined,
					};
				}
			});
			getHomeEquipmentAlarmList().then((response) => {
				if (response.hasOwnProperty('data')) {
					this.homeDevice.list = response.data;
				} else {
					this.homeDevice.list = [];
				}
			});
		},
		goPage(val) {
			this.$router.push({ name: val.url });
		},
	},
};
</script>

<style lang="scss" scoped>
.home-page {
	background-image: url('~@/assets/img/home-bg.png');
	background-size: cover;
	background-repeat: no-repeat;
	background-position: center;
	height: 300px;
	width: 100%;
}
.main-top {
	width: 100%;
	padding: 10px 20px 0 40px;
	.title {
		position: absolute;
		left: 34px;
		top: 20px;
		width: 90%;
		font-size: 32px;
		z-index: 10;
		.title-inner {
			position: absolute;
			left: 42px;
			top: 5px;
			font-size: 24px;
		}
		.date-tabs {
			position: absolute;
			right: 100px;
			top: 5px;
			font-size: 18px;
		}
		:deep(.date-tabs) {
			.el-tabs__header {
				margin-bottom: 8px;
				display: inline-block;
			}

			.el-tabs__content {
				overflow: visible;
			}

			.el-tabs__item {
				font-size: 18px;
				padding-left: 0 !important;
				padding-right: 0 !important;
				line-height: 20px !important;
				height: 24px;
			}
			.el-tabs__item.is-active {
				color: #0b58ff;
			}
		}
		.current-date {
			font-size: 15px;
			font-weight: 500;
			position: absolute;
			right: 0px;
			top: 5px;
		}
		// .current-time {
		//   width: 160px;
		// 	font-size: 18px;
		// 	position: absolute;
		// 	right: -20px;
		// 	top: 5px;
		// }
	}
	.box {
		background-color: #fff;
		border-radius: 24px;
		height: 232px;
		padding: 60px 24px 32px 24px;
		.num-box {
			height: 120px;
			padding-top: 26px;
			text-align: center;
			.num-style {
				color: #000;
				font-size: 40px;
			}
			.unit-style {
				color: rgba(0, 0, 0, 0.7);
				font-size: 18px;
			}
		}
		.shadow {
			background: linear-gradient(90deg, #ffffff 80%, #f2f4f9 100%);
		}
	}
}
.main-middle {
	display: flex;
	flex-flow: row nowrap;
	background-color: #fff;
	border-radius: 24px;
	height: 133px;
	margin: 16px 38px 16px 40px;
	padding-top: 15px;
	.item {
		width: 20%;
		height: 100px;
		cursor: pointer;
		.title-box {
			display: inline-block;
			padding-left: 20px;
			vertical-align: bottom;
			.title {
				height: 35px;
				font-size: 18px;
				color: #000;
				padding-top: 6px;
				letter-spacing: 2px;
			}
			.triangle {
				height: 15px;
				span {
					display: inline-block;
					width: 0;
					height: 0;
					border-top: 6px solid transparent;
					border-bottom: 6px solid transparent;
				}
				.c1 {
					border-left: 12px solid rgba(11, 88, 255, 1);
					margin-right: 5px;
				}
				.c2 {
					border-left: 12px solid rgba(11, 88, 255, 0.5);
					margin-right: 5px;
				}
				.c3 {
					border-left: 12px solid rgba(11, 88, 255, 0.2);
				}
			}
		}
	}
	.shadow {
		background: linear-gradient(90deg, #ffffff 90%, #f2f4f9 100%);
	}
}
.main-bottom {
	width: 100%;
	padding: 0 20px 0 40px;
	.box {
		background-color: #fff;
		border-radius: 24px;
		height: calc(100vh - 534px);
		padding: 24px;
		.title-box {
			position: relative;
			font-size: 32px;
			margin-bottom: 16px;
			.title {
				position: absolute;
				left: 44px;
				top: 6px;
				font-size: 24px;
			}
		}
		.order-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			font-size: 18px;
			padding: 0 30px;
			.order-item {
				width: 48%;
				margin-bottom: 16px;
				position: relative;
				.order-name {
					color: #000000;
				}
				.order-value {
					position: absolute;
					right: 0;
					color: #0b58ff;
				}
			}
		}
	}
}
</style>
<style lang="scss">
.home-page {
	.el-progress-bar__inner {
		background-image: url('~@/assets/img/home-progress-bg.png');
		background-size: cover;
	}
}
</style>
