<template>
	<el-container>
		<el-main>
			<el-container>
				<el-header style="height: 40%">
					<el-container>
						<el-header> <h2>棒数据</h2> </el-header>
						<el-main id="bangTable">
							<el-table
								id="hourBaseTable"
								:data="tableData11"
								style="width: 1200px"
								:height="bangHeight"
								border
							>
								<el-table-column label="版型" prop="版型">
								</el-table-column>
								<el-table-column label="7号">
									<el-table-column
										label="合格数"
										prop="合格数"
									>
									</el-table-column>
								</el-table-column>
								<el-table-column label="8号">
									<el-table-column
										label="仓库库存"
										prop="仓库库存"
									>
									</el-table-column>
								</el-table-column>
								<el-table-column label="6号">
									<el-table-column label="库存" prop="库存">
									</el-table-column>
									<el-table-column label="平头" prop="平头">
									</el-table-column>
									<el-table-column label="粘头" prop="粘头">
									</el-table-column>
									<el-table-column
										label="粗中细"
										prop="粗中细"
									>
									</el-table-column>
									<el-table-column label="抛光" prop="抛光">
									</el-table-column>
									<el-table-column label="开槽" prop="开槽">
									</el-table-column>
									<el-table-column label="配管" prop="配管">
									</el-table-column>
								</el-table-column>

								<el-table-column label="合计" prop="合计">
								</el-table-column>
							</el-table>
						</el-main>
					</el-container>
				</el-header>
				<el-main style="padding: 8px 0 0 0">
					<el-container>
						<el-header> <h2>管数据</h2> </el-header>
						<el-main id="guanTable" style="background: #ffffff">
							<el-table
								:data="tableData22"
								style="width: 1200px"
								border
								id="hourBaseTable"
								:height="guanHeight"
							>
								<el-table-column label="版型" prop="版型">
								</el-table-column>
								<el-table-column label="规格" prop="规格">
								</el-table-column>
								<el-table-column label="10号">
									<el-table-column
										label="合格数"
										prop="合格数"
									>
									</el-table-column>
								</el-table-column>
								<el-table-column label="8号">
									<el-table-column
										label="仓库库存"
										prop="仓库库存"
									>
									</el-table-column>
									<el-table-column label="退火" prop="退火">
									</el-table-column>
								</el-table-column>
								<el-table-column label="6号">
									<el-table-column label="库存" prop="库存">
									</el-table-column>
									<el-table-column label="开槽" prop="开槽">
									</el-table-column>
									<el-table-column label="倒角" prop="倒角">
									</el-table-column>
									<el-table-column label="配棒" prop="配棒">
									</el-table-column>
								</el-table-column>
								<el-table-column label="合计" prop="合计">
								</el-table-column>
							</el-table>
						</el-main>
					</el-container>
				</el-main>
			</el-container>
		</el-main>
	</el-container>
</template>
<script>
// import mockData from "./mock.json";
export default {
	name: "total_statistics",
	data() {
		return {
			// mockData: mockData,
			tableData11: [],
			tableData22: [],
			bangHeight: 300,
			guanHeight: 500,
		};
	},
	mounted() {
		this.getSourceData();
		this.bangHeight =
			document.getElementById("bangTable").offsetHeight - 20;
		this.guanHeight =
			document.getElementById("guanTable").offsetHeight - 20;
	},
	created() {},
	methods: {
		//获取表格原始数据
		getSourceData() {
			let that = this;
			that.$API.bi.dataset.exec.req("gz_zh").then((res) => {
				// let res = that.mockData;
				let data_b = [...res.data2.ds0, ...res.data2.ds2];
				let data_g = [...res.data2.ds1, ...res.data2.ds3];
				this.data_b = data_b;
				this.data_g = data_g;
				that.getDataB(data_b);
				that.getDataG(data_g);
			});
		},
		//棒数据
		getDataB(data) {
			let that = this;
			let models = [],
				dataArr = [];
			//按版型（型号）分组
			data.forEach((item) => {
				let index = 0;
				if (item.型号.indexOf("+") > -1) {
					let model = item.型号.split("+")[0];
					index = models.indexOf(model);
					item.型号 = model;
				} else {
					index = models.indexOf(item.型号);
				}
				if (index > -1) {
					dataArr[index].push(item);
				} else {
					models.push(item.型号);
					let arr = [];
					arr.push(item);
					dataArr.push(arr);
				}
			});
			//将数组组成table要用的格式
			dataArr.forEach((itemArr, index) => {
				//itemArr 是型号相通数据的一个对象数组
				let obj = {
					版型: "",
					合格数: 0,
					仓库库存: 0,
					库存: 0,
					平头: 0,
					粘头: 0,
					粗中细: 0,
					抛光: 0,
					开槽: 0,
					配管: 0,
					合计: 0,
				};
				itemArr.forEach((item1) => {
					obj.版型 = item1.型号;
					let key = "";
					if (item1.车间 == "7车间" && item1.工序 == "棒料成型") {
						key = "合格数";
					} else if (!item1.车间 && item1.工序 == "棒料成型") {
						key = "仓库库存";
					} else if (
						item1.车间 == "6车间" &&
						item1.工序 == "棒料成型"
					) {
						key = "库存";
					} else if (item1.车间 == "6车间" && item1.工序 == "平头") {
						key = "平头";
					} else if (item1.车间 == "6车间" && item1.工序 == "粘头") {
						key = "粘头";
					} else if (
						item1.车间 == "6车间" &&
						item1.工序 == "粗中细磨"
					) {
						key = "粗中细";
					} else if (item1.车间 == "6车间" && item1.工序 == "抛光") {
						key = "抛光";
					} else if (item1.车间 == "6车间" && item1.工序 == "开槽") {
						key = "开槽";
					} else if (item1.工序 == "配管") {
						key = "配管";
					}
					obj[key] = item1.数量;
				});
				obj.合计 =
					obj.合格数 +
					obj.仓库库存 +
					obj.库存 +
					obj.平头 +
					obj.粘头 +
					obj.粗中细 +
					obj.抛光 +
					obj.开槽 +
					obj.配管;
				that.tableData11[index] = obj;
			});
		},
		//管数据
		getDataG(data) {
			let that = this;
			let models = [],
				modelArr = [];
			//modelArr按型号分组
			data.forEach((item) => {
				let index = 0;
				if (item.型号.indexOf("+") > -1) {
					let model = item.型号.split("+")[1];
					index = models.indexOf(model);
					item.型号 = model;
				} else {
					index = models.indexOf(item.型号);
				}

				if (index > -1) {
					modelArr[index].push(item);
				} else {
					models.push(item.型号);
					let arr = [];
					arr.push(item);
					modelArr.push(arr);
				}
			});
			//specArr按规格的分组
			let specArr = [];
			modelArr.forEach((itemArr, index0) => {
				specArr[index0] = [];
				let specifica = []; //规格数组
				let ind = 0;
				//itemArr是同一规格的对象数组[{},{}]
				itemArr.forEach((item1, index1) => {
					//item1==>对象{}
					if (item1.规格.indexOf("+") > -1) {
						let spec = item1.规格.split("+")[1];
						ind = specifica.indexOf(spec);
					} else {
						ind = specifica.indexOf(item1.规格);
					}
					if (ind > -1) {
						specArr[index0][ind].push(item1);
					} else {
						specifica.push(item1.规格);
						let arr = [];
						arr.push(item1);
						specArr[index0].push(arr);
					}
				});
			});
			//specArr已将同型号的数组中按规格分组
			specArr.forEach((item3, index3) => {
				item3.forEach((item4) => {
					let obj = {
						版型: "",
						规格: "",
						合格数: 0,
						仓库库存: 0,
						退火: 0,
						库存: 0,
						开槽: 0,
						倒角: 0,
						配棒: 0,
						合计: 0,
					};
					item4.forEach((item5) => {
						let key = "";
						obj.版型 = item5.型号;
						obj.规格 = item5.规格;

						if (
							item5.车间 == "10车间" &&
							item5.工序 == "管料成型"
						) {
							key = "合格数";
						} else if (!item5.车间 && item5.工序 == "管料成型") {
							key = "仓库库存";
						} else if (
							item5.车间 == "8车间" &&
							item5.工序 == "管料成型"
						) {
							key = "仓库库存";
						} else if (
							item5.车间 == "8车间" &&
							item5.工序 == "管料退火"
						) {
							key = "退火";
						} else if (
							item5.车间 == "6车间" &&
							item5.工序 == "管料成型"
						) {
							key = "库存";
						} else if (
							item5.车间 == "6车间" &&
							item5.工序 == "开槽"
						) {
							key = "开槽";
						} else if (
							item5.车间 == "6车间" &&
							item5.工序 == "倒角"
						) {
							key = "倒角";
						} else if (
							item5.车间 == "6车间" &&
							item5.工序 == "配棒"
						) {
							key = "配棒";
						}
						obj[key] = obj[key] + item5.数量;
					});
					obj.合计 =
						obj.合格数 +
						obj.仓库库存 +
						obj.退火 +
						obj.库存 +
						obj.开槽 +
						obj.倒角 +
						obj.配棒;
					that.tableData22.push(obj);
				});
			});
		},
	},
};
</script>

<style scoped></style>
