<template>
	<div class="app-container">
		<el-header>
			<div class="left-panel">
				<el-button
					type="primary"
					@click="exportExcel()"
					:loading="exportLoading"
					>导出xlsx
				</el-button>
				<el-button type="primary" @click="handlePrint">打印 </el-button>
			</div>
		</el-header>
		<el-card style="margin-top: 5px">
			<div class="printWrap">
				<div ref="print" id="myReport" class="printContainer">
					<table
						border="1"
						width="1035"
						cellspacing="0"
						:key="timeStamp"
						class="myTable"
						id="myTable"
					>
						<thead class="myTableHead">
							<tr>
								<th colspan="5">
									电石渣工段主要设备（100KW以上）单位产品电耗数据表
								</th>
							</tr>
							<tr>
								<th>设备名称</th>
								<th>设备编号</th>
								<th>单位</th>
								<th>昨日</th>
								<th>本月</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="item in tableDatas" :key="item">
								<template
									v-for="(item1, ind) in item"
									:key="item1"
								>
									<td
										v-if="ind == 0 || ind == 1 || ind == 2"
										class="numCell"
									>
										{{ item1 }}
									</td>
									<td
										v-if="ind == 3"
										class="numCell"
										@click="itemClick('day_s', item)"
									>
										{{ item1 }}
									</td>
									<td
										v-if="ind == 4"
										class="numCell"
										@click="itemClick('month_s', item)"
									>
										{{ item1 }}
									</td>
								</template>
							</tr>
						</tbody>
					</table>
					<div class="radioWrap">
						<el-radio-group
							v-model="typeRadio"
							@change="typeRadioChange"
						>
							<el-radio-button label="month"
								>本月</el-radio-button
							>
							<el-radio-button label="year">本年</el-radio-button>
						</el-radio-group>
						<div style="position: absolute; right: 20px">
							<el-date-picker
								v-model="searchDate"
								type="month"
								value-format="YYYY-MM"
								formatter="YYYY-MM"
								placeholder="选择月"
								v-if="typeRadio == 'month'"
								@change="dateChange"
							></el-date-picker>
							<el-date-picker
								v-if="typeRadio == 'year'"
								v-model="searchDate"
								type="year"
								value-format="YYYY"
								formatter="YYYY"
								placeholder="选择年"
								@change="dateChange"
							></el-date-picker>
						</div>
					</div>
					<div class="chartWrap" v-show="typeRadio == 'month'">
						<div class="chartTitle">本月生产参数统计图</div>
						<scEcharts
							height="400px"
							width="1033px"
							:option="optionDay"
						></scEcharts>
					</div>
					<div class="chartWrap" v-show="typeRadio == 'year'">
						<div class="chartTitle">本年生产参数统计图</div>
						<scEcharts
							height="400px"
							width="1033px"
							:option="optionMonth"
						></scEcharts>
					</div>
				</div>
			</div>
			<charts
				v-if="asynDialog"
				:type="type"
				:title="title"
				:cate="cate"
				:apiObj="apiObj"
				:mpoint="mpoint"
				:modelValue="modelValue"
				:showClose="showClose"
				:echartType="echartType"
				@closed="asynDialog = false"
			></charts>
		</el-card>
	</div>
</template>
<script>
const colors = ["#647bfe"];
let tooltip = {
	show: true,
	trigger: "axis",
	axisPointer: {
		type: "cross",
	},
	confine: false,
	showContent: true,
	timeStamp: null,
	triggerOn: "mousemove",
};
let grid = {
	right: "3%",
	left: "7%",
	top: "15%",
};
let toolbox = {
	right: "5%",
	feature: {
		dataView: { show: true, readOnly: false },
		saveAsImage: { show: true },
	},
};
let legend = {
	top: "2%",
	data: ["烘干破主电机"],
};
let yAxis = {
	type: "value",
	name: "分布电耗(KW.h/t)",
	alignTicks: true,
	axisLine: {
		show: true,
		lineStyle: {
			color: "#647bfe",
		},
	},
	axisLabel: {
		formatter: "{value}",
	},
};
import scEcharts from "@/components/scEcharts";
import { defineAsyncComponent } from "vue";
export default {
	components: {
		scEcharts,
		charts: defineAsyncComponent(() =>
			import("@/components/scEnm/lineChartsdialog.vue")
		),
	},
	data() {
		return {
			typeRadio: "month",
			searchDate: "",
			exportLoading: false,
			chartShow: false,
			myOption: null,
			optionDay: {},
			optionMonth: {},
			year: 2023,
			month: 1,
			query: {
				mgroup: "",
			},
			mpoint: "",
			tableName: "主要设备（100KW以上）单位产品电耗数据表",
			tableDatas: [["烘干破主电机", "", "kw.h/t", "", "", ""]],
			modelValue: true,
			type: "hour_s",
			title: "电石渣工段",
			cate: "",
			apiObj: this.$API.enm.mpoint.stat,
			showClose: true,
			echartType: "line",
			asynDialog: false,
			allValDays: 0,
			allValMonth: 0,
			option1: {
				color: colors,
				tooltip: tooltip,
				grid: grid,
				toolbox: toolbox,
				legend: legend,
				xAxis: {
					axisTick: {
						show: true,
						length: 5,
						inside: true,
					},
					data: [],
				},
				yAxis: yAxis,
				series: {
					name: "烘干破主电机",
					type: "bar",
					data: [],
				},
			},
			option2: {
				color: colors,
				tooltip: tooltip,
				grid: grid,
				toolbox: toolbox,
				legend: legend,
				xAxis: {
					axisTick: {
						show: true,
						length: 5,
						inside: true,
					},
					data: [
						"一月",
						"二月",
						"三月",
						"四月",
						"五月",
						"六月",
						"七月",
						"八月",
						"九月",
						"十月",
						"十一月",
						"十二月",
					],
				},
				yAxis: yAxis,
				series: {
					name: "烘干破主电机",
					type: "bar",
					data: [],
				},
			},
		};
	},
	mounted() {
		let that = this;
		var myDate = new Date();
		let year = myDate.getFullYear();
		let month = myDate.getMonth() + 1;
		let days = myDate.getDate();
		let hours = myDate.getHours();
		let timeDate = myDate.getTime();
		this.timeStamp = timeDate;
		this.year = year;
		this.month = month;
		this.days = days;

		let dayTime = 24 * 60 * 60 * 1000;
		//昨天的计算
		let year_d = year,
			month_d = month,
			days_d = days;
		if (hours < 21) {
			//21点前，查找昨日数据为前一天数据
			let newDate = timeDate - dayTime;
			let lastDate = new Date(newDate);
			year_d = lastDate.getFullYear();
			month_d = lastDate.getMonth() + 1;
			days_d = lastDate.getDate();
		}
		this.$API.mtm.mgroup.list
			.req({ page: 0, search: "电石渣" })
			.then((res) => {
				console.log("电石渣", res);
				that.query.mgroup = res[0].id;
				//本月
				let params2 = {};
				params2.page = 0;
				params2.year_s = year;
				params2.month_s = month;
				params2.type = "month_s";
				params2.mgroup = this.query.mgroup;
				this.$API.enm.enstat
					.req(params2)
					.then((res2) => {
						if (res2.length > 0) {
							let allValMonth = (that.allValMonth =
								res2[0].total_production); //当前条件下的总产量
							params2.mpoint__ep_monitored__power_kw__gte = 100;
							this.$API.enm.mpoint.stat
								.req(params2)
								.then((res) => {
									let data2 = res;
									for (let i = 0; i < data2.length; i++) {
										that.tableDatas[i] = [];
										that.tableDatas[i][0] =
											data2[i].ep_monitored_name;
										that.tableDatas[i][1] =
											data2[i].ep_monitored_number;
										that.tableDatas[i][2] = "KW·h/t";
										let val =
											allValMonth == 0
												? "/"
												: (
														data2[i].val /
														allValMonth
												  ).toFixed(2);
										that.tableDatas[i][4] = val;
										that.tableDatas[i][5] = data2[i].mpoint;
									}
								});
						}
					})
					.then((res2) => {
						//昨日
						let params3 = {};
						params3.page = 0;
						params3.year_s = year_d;
						params3.month_s = month_d;
						params3.day_s = days_d;
						params3.type = "day_s";
						params3.mgroup = this.query.mgroup;
						this.$API.enm.enstat.req(params3).then((res3) => {
							if (res3.length > 0) {
								let allValDays = (that.allValDays =
									res3[0].total_production);
								params3.mpoint__ep_monitored__power_kw__gte = 100;
								this.$API.enm.mpoint.stat
									.req(params3)
									.then((res) => {
										let data3 = res3;
										for (let j = 0; j < data3.length; j++) {
											let val =
												allValDays == 0
													? "/"
													: (
															data3[j].val /
															allValDays
													  ).toFixed(2);
											that.tableDatas[j][3] = val;
										}
									});
							}
						});
					});
				that.getDayData();
				that.getMonthData();
			});
	},
	methods: {
		typeRadioChange() {
			this.searchDate = "";
		},
		dateChange(val) {
			console.log(val);
			if (this.typeRadio == "month") {
				let year = val.split("-")[0];
				let month = val.split("-")[1];
				this.getDayData(year, month);
			} else {
				this.getMonthData(val);
			}
		},
		//获取天数据
		getDayData() {
			let that = this;
			let query1 = {};
			query1.page = 0;
			query1.year_s = that.year;
			query1.month_s = that.month;
			query1.type = "day_s";
			query1.mgroup = this.query.mgroup;
			query1.mpoint__ep_monitored__power_kw__gte = 100;
			this.$API.enm.enstat.req(query1).then((response) => {
				let seriesData0 = [];
				let data = response;
				data.forEach((item) => {
					let ind = item.day_s;
					let val = 0;
					if (that.allValDays == 0) {
						val = "";
					} else {
						val = (item.val / that.allValDays).toFixed(2);
					}
					if (item.equip_name == "烘干破主电机") {
						seriesData0[ind] = val;
					}
				});
				let options = { ...that.option1 };
				options.series.data = seriesData0;
				let dayXAxis = [];
				for (let i = 1; i <= that.days; i++) {
					let item = i + "日";
					dayXAxis.push(item);
				}
				options.xAxis.data = dayXAxis;
				that.optionDay = options;
			});
		},
		//获取月数据
		getMonthData() {
			let that = this;
			let query2 = {};
			query2.page = 0;
			query2.year_s = that.year;
			query2.type = "month_s";
			query2.mgroup = that.query.mgroup;
			query2.mpoint__ep_monitored__power_kw__gte = 100;
			this.$API.enm.enstat.req(query2).then((response) => {
				let seriesData0 = [];
				let data = response;
				data.forEach((item) => {
					let ind = item.month_s;
					let val = 0;
					if (that.allValMonth == 0) {
						val = "";
					} else {
						val = (item.val / that.allValMonth).toFixed(2);
					}
					if (item.equip_name == "烘干破主电机") {
						seriesData0[ind] = val;
					}
				});
				let options = { ...that.option2 };
				options.series.data = seriesData0;
				that.optionMonth = options;
			});
		},
		itemClick(type, item) {
			this.type = type;
			this.cate = item[0];
			this.mpoint = item[5];
			this.asynDialog = true;
		},
		handlePrint() {
			this.$PRINT("#myReport");
		},
		exportExcel() {
			this.exportLoading = true;
			this.$XLSX("#myTable", this.tableName);
			this.exportLoading = false;
		},
	},
};
</script>
<style scoped>
.printContainer {
	padding-left: 20px;
}
.radioWrap {
	display: flex;
	justify-content: center;
	padding-top: 10px;
	position: relative;
}
</style>
