<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 ref="print" id="myReport" class="printContainer">
				<table
					border="1"
					cellspacing="0"
					:key="timeStamp"
					id="myTable"
					class="myTable"
				>
					<thead class="myTableHead">
						<tr>
							<th colspan="12">全厂能源统计</th>
						</tr>
						<tr>
							<th rowspan="2">月份</th>
							<th rowspan="2">工业总产值（万元）</th>
							<th rowspan="2">工业增加值（万元）</th>
							<th colspan="3">综合能源消费</th>
							<th colspan="3">单位总产值能耗</th>
							<th colspan="3">单位工业增加值能耗</th>
						</tr>
						<tr>
							<th>当期值（tce）</th>
							<th>环比增长率（%）</th>
							<th>同比增长率（%）</th>
							<th>当期值（tce/万元）</th>
							<th>环比增长率（%）</th>
							<th>同比增长率（%）</th>
							<th>当期值（tce/万元）</th>
							<th>环比增长率（%）</th>
							<th>同比增长率（%）</th>
						</tr>
					</thead>
					<tr v-for="(item, index) in tableDatas" :key="index">
						<td class="numCell" v-for="item1 in item" :key="item1">
							{{ item1 }}
						</td>
					</tr>
				</table>
			</div>
		</el-card>
	</div>
</template>
<script>
export default {
	data() {
		return {
			timeStamp: 0,
			year_s: "",
			tableDatas: [],
		};
	},
	mounted() {
		let that = this;
		var myDate = new Date();
		let year = myDate.getFullYear();
		that.year_s = year;
		this.getData();
	},
	methods: {
		getData() {
			let that = this;
			let wrapArr = [],
				wrapArr2 = [];
			let obj = {};
			obj.year_s = this.year_s - 1;
			obj.page = 0;
			that.$API.enm.enstat2.req(obj).then((res) => {
				if (res.length > 0) {
					res.forEach((item) => {
						let ind = item.month_s - 1;
						wrapArr[ind] = item;
					});
				} else {
				}
				let obj2 = {};
				obj2.year_s = this.year_s;
				obj2.page = 0;
				that.$API.enm.enstat2.req(obj2).then((res2) => {
					if (res2.length > 0) {
						res2.forEach((item2) => {
							let ind2 = item2.month_s - 1;
							wrapArr2[ind2] = item2;
						});
					} else {
					}
					for (let i = 0; i < wrapArr2.length; i++) {
						if (Boolean(wrapArr2[i])) {
							if (this.tableDatas[i]) {
							} else {
								this.tableDatas[i] = [];
							}
							let time =
								wrapArr2[i].year_s + "." + wrapArr2[i].month_s;
							this.tableDatas[i][0] = time;
							this.tableDatas[i][1] =
								wrapArr2[i].industry_total_val;
							this.tableDatas[i][2] =
								wrapArr2[i].industry_add_val;
							this.tableDatas[i][3] = wrapArr2[i].en_consume;
							let tValue3 = 0,
								hValue3 = 0,
								tPercent3 = 0,
								hPercent3 = 0;
							if (wrapArr2[i].month_s > 1) {
								tValue3 = wrapArr2[i - 1]
									? wrapArr2[i - 1].en_consume
									: "/";
							} else {
								tValue3 = wrapArr[12]
									? wrapArr[12].en_consume
									: "/";
							}
							if (tValue3 !== "/" && tValue3 !== 0) {
								tPercent3 =
									((wrapArr2[i].en_consume - tValue3) /
										tValue3) *
									100;
							} else {
								tPercent3 = "/";
							}
							hValue3 = wrapArr[i] ? wrapArr[i].en_consume : "/";
							if (hValue3 !== "/" && hValue3 !== 0) {
								hPercent3 =
									((wrapArr2[i].en_consume - hValue3) /
										hValue3) *
									100;
							} else {
								hPercent3 = "/";
							}
							this.tableDatas[i][4] = hPercent3;
							this.tableDatas[i][5] = tPercent3;
							this.tableDatas[i][6] = wrapArr2[i].en_consume_unit;
							let tValue6 = 0,
								hValue6 = 0,
								tPercent6 = 0,
								hPercent6 = 0;
							if (wrapArr2[i].month_s > 1) {
								tValue6 = wrapArr2[i - 1]
									? wrapArr2[i - 1].en_consume_unit
									: "/";
							} else {
								tValue6 = wrapArr[12]
									? wrapArr[12].en_consume_unit
									: "/";
							}
							if (tValue6 !== "/" && tValue6 !== 0) {
								tPercent6 =
									((wrapArr2[i].en_consume_unit - tValue6) /
										tValue6) *
									100;
							} else {
								tPercent6 = "/";
							}
							hValue6 = wrapArr[i]
								? wrapArr[i].en_consume_unit
								: "/";
							if (hValue6 !== "/" && hValue6 !== 0) {
								hPercent6 =
									((wrapArr2[i].en_consume_unit - hValue6) /
										hValue6) *
									100;
							} else {
								hPercent6 = "/";
							}
							this.tableDatas[i][7] = hPercent6;
							this.tableDatas[i][8] = tPercent6;
							this.tableDatas[i][9] =
								wrapArr2[i].en_add_consume_unit;
							let tValue9 = 0,
								hValue9 = 0,
								tPercent9 = 0,
								hPercent9 = 0;
							if (wrapArr2[i].month_s > 1) {
								tValue9 = wrapArr2[i - 1]
									? wrapArr2[i - 1].en_add_consume_unit
									: "/";
							} else {
								tValue9 = wrapArr[12]
									? wrapArr[12].en_add_consume_unit
									: "/";
							}
							if (tValue9 !== "/" && tValue9 !== 0) {
								tPercent9 =
									((wrapArr2[i].en_add_consume_unit -
										tValue9) /
										tValue9) *
									100;
							} else {
								tPercent9 = "/";
							}
							hValue9 = wrapArr[i]
								? wrapArr[i].en_add_consume_unit
								: "/";
							if (hValue9 !== "/" && hValue9 !== 0) {
								hPercent9 =
									((wrapArr2[i].en_add_consume_unit -
										hValue9) /
										hValue9) *
									100;
							} else {
								hPercent9 = "/";
							}
							this.tableDatas[i][10] = hPercent9;
							this.tableDatas[i][11] = tPercent9;
						} else {
						}
					}
				});
			});
		},
		exportExcel() {
			this.exportLoading = true;
			this.$XLSX("#myTable", this.tableName);
			this.exportLoading = false;
		},
		handlePrint() {
			this.$PRINT("#myReport");
		},
	},
};
</script>
<style></style>
