<template>
	<div class="app-container">
		<el-header>
			<div class="left-panel">
				<el-date-picker
					v-model="query.year_s"
					type="year"
					value-format="YYYY"
					format="YYYY"
					placeholder="查询年份"
					class="headerSearch"
				/>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
				<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="2400"
						cellspacing="0"
						:key="timeStamp"
						id="myTable"
						class="myTable"
					>
						<thead class="myTableHead">
							<tr>
								<th colspan="24">原料磨工段班组月度对比分析</th>
							</tr>
							<tr>
								<th rowspan="2">月份</th>
								<th rowspan="2">班组</th>
								<th colspan="2">产量</th>
								<th>运转率</th>
								<th>成本</th>
								<th colspan="7">质量</th>
								<th colspan="10">单位产品电耗</th>
								<th rowspan="2">得分</th>
							</tr>
							<tr>
								<th>总产量（t）</th>
								<th>台时产量（t/h）</th>
								<th>运转率（%）</th>
								<th>单位产品成本（元/吨）</th>
								<th>细度（%）</th>
								<th>Fe₂O₃（%）</th>
								<th>水分（%）</th>
								<th>细度（%）</th>
								<th>Fe₂O₃（%）</th>
								<th>水分（%）</th>
								<th>CaO（%）</th>
								<th>循环风机（KW·h/t）</th>
								<th>尾排风机（KW·h/t）</th>
								<th>立磨主电机（KW·h/t）</th>

								<th>当期值（KW·h/t）</th>
								<th>目标值（KW·h/t）</th>
								<th>当期与目标差值（KW·h/t）</th>
								<th>环期值（KW·h/t）</th>
								<th>当期与环期差值（KW·h/t）</th>
								<th>环比增长率（%）</th>
								<th>同比增长率（%）</th>
							</tr>
						</thead>
						<tr v-for="(item, index) in tableDatas" :key="index">
							<td
								v-for="(item0, index0) in item"
								:key="index0"
								class="numCell"
							>
								{{ item0 }}
							</td>
						</tr>
					</table>
				</div>
			</div>
		</el-card>
	</div>
</template>
<script>
export default {
	data() {
		return {
			timeStamp: 0,
			query: {
				year_s: "",
				page: 0,
				type: "month_st",
				mgroup: "",
			},
			tableDatas: [],
			goalDatas: [],
			exportLoading: false,
		};
	},
	mounted() {
		let that = this;
		var myDate = new Date();
		let year = myDate.getFullYear();
		that.query.year_s = year;
		this.$API.mtm.mgroup.list
			.req({ page: 0, search: "原料磨" })
			.then((res) => {
				console.log("原料磨", res);
				that.query.mgroup = res[0].id;
				let params = {};
				params.page = 0;
				params.year = year;
				params.mgroup = that.query.mgroup;
				this.$API.mtm.goal.list.req(params).then((res) => {
					if (res.length > 0) {
						that.goalDatas = res[0];
					}
					that.getData(that.goalDatas);
				});
			});
	},
	methods: {
		getData(goalData) {
			let that = this;
			let query0 = {};
			query0.page = 0;
			query0.type = "month_st";
			query0.year_s = that.query.year_s - 1;
			query0.mgroup = that.query.mgroup;
			let wrapArr = [],
				wrapArr0 = [],
				wrapArrs = [],
				compareArr0 = [];
			this.$API.enm.enstat.req(query0).then((res0) => {
				let data0 = res0;
				if (data0.length > 0) {
					data0.forEach((item0) => {
						//先按月份排序，再按班组排序
						wrapArr0[ind0] = item0;
					});
				} else {
				}
				this.$API.enm.enstat.req(that.query).then((res) => {
					let data = res;
					if (data.length > 0) {
						data.forEach((item) => {
							//先按月份排序，再按班组排序
							let compareArr = [];
							compareArr.push(item.month_s);
							compareArr.push(item.team_name);
							compareArr.push(item.elec_consume_unit);
							compareArr0.push(compareArr);
						});
						data.forEach((item) => {
							//先按月份排序，再按班组排序
							let n = item.month_s;
							let ind = 0;
							let arr = [];
							let time = "" + item.year_s + "." + item.month_s;
							arr.push(time);
							arr.push(item.team_name);
							arr.push(item.total_production);
							arr.push(item.production_hour);
							arr.push(item.run_rate);
							arr.push(item.production_cost_unit);
							//质量
							arr[6] =
								item.辅料_细度_rate_pass != null
									? item.辅料_细度_rate_pass
									: "/";
							arr[7] =
								item.辅料_Fe2O3_rate_pass != null
									? item.辅料_Fe2O3_rate_pass
									: "/";
							arr[8] =
								item.辅料_水分_rate_pass != null
									? item.辅料_水分_rate_pass
									: "/";
							arr[9] =
								item.干混生料_细度_rate_pass != null
									? item.干混生料_细度_rate_pass
									: "/";
							arr[10] =
								item.干混生料_Fe2O3_rate_pass != null
									? item.干混生料_Fe2O3_rate_pass
									: "/";
							arr[11] =
								item.干混生料_水分_rate_pass != null
									? item.干混生料_水分_rate_pass
									: "/";
							arr[12] =
								item.干混生料_CaO_rate_pass != null
									? item.干混生料_CaO_rate_pass
									: "/";
							//设备
							arr[13] =
								item.循环风机_consume_unit != null
									? item.循环风机_consume_unit
									: "/";
							arr[14] =
								item.尾排风机_consume_unit != null
									? item.尾排风机_consume_unit
									: "/";
							arr[15] =
								item.立磨主电机_consume_unit != null
									? item.立磨主电机_consume_unit
									: "/";
							//
							arr[16] = item.elec_consume_unit; //当期值（KW·h/t）
							let keyVale = "goal_val_" + n;
							arr[17] = goalData[keyVale]?goalData[keyVale]:0; //目标值（KW·h/t）//需要接口获取
							arr[18] = item.celec_consume_unit - arr[16]; //当期与目标差值（KW·h/t）
							let ind_pre = 0,
								huanbi = 0;
							if (item.month_s == 1) {
								ind_pre = 12;
								huanbi = wrapArr0[ind_pre]
									? wrapArr0[ind_pre].elec_consume_unit
									: "/";
							} else {
								// 如果 wrapArr数组长度大于0，那么循环遍历wrapArr数组，找到与item.month_s相差1的元素 而 item.name相同，并赋值给huanbi
								compareArr0.forEach((item0) => {
									if (item0[0] == item.month_s - 1 && item0[1] == item.team_name) {
										huanbi = item0[2];
								}
							})
							}
							arr[19] = huanbi; //环期值（KW·h/t）上个月的值
							 //当期与环期差值（KW·h/t）
							if (arr[19] !== "/"){
								arr[20] = (arr[16] - arr[19]).toFixed(2); //当期与环期差值（KW·h/t）
							}else{
								arr[20] = "/"; //当期与环期差值（KW·h/t）
							}
							arr[21] = item.celec_consume_unit; //环比增长率（%）
							if (arr[19] !=="/" && arr[20] !==0 && arr[20] !== "undefined"){
								arr[21] = ((arr[19]/arr[20])*100).toFixed(2);
							}else{
								arr[21] = "/"; //环比增长率（%）= 当期与环期差值（KW·h/t）/环期值（KW·h/t）*100%
							}
							arr[22] = item.celec_consume_unit; //同比增长率（%）
							arr[23] = item.celec_consume_unit; //同比增长率（%）
							wrapArr.push(arr);
						});
						that.tableDatas = wrapArr;
					} else {
					}
				});
			});
		},
		handleQuery() {
			this.tableDatas = [];
			this.getData(this.goalDatas);
		},
		itemClick(type, item) {
			this.type = type;
			this.asynDialog = true;
		},
		handlePrint() {
			this.$PRINT("#myReport");
		},
		exportExcel() {
			this.exportLoading = true;
			this.$XLSX("#myTable", this.tableName);
			this.exportLoading = false;
		},
	},
};
</script>
<style scoped>
.printWrap {
	width: 100%;
	overflow-x: scroll;
}
</style>
