<template>
	<base-table
		:table-props="tableProps"
		:table-data="list"
		:max-height="tableH" />
</template>
<script>
import tableHeightMixin from '@/mixins/tableHeightMixin';
import { getRecordInOneDay } from '@/api/monitoring/statisticalData';
import moment from 'moment';
export default {
	name: 'HourData',
	mixins: [tableHeightMixin],
	computed: {
		tableProps() {
			return [
				{
					prop: 'inspectionDetContent',
					label: '检测内容',
				},
				...this.nameData,
			];
		},
	},
	data() {
		return {
			heightNum: 236,
			nameData: [],
			list: [],
		};
	},
	mounted() {
		this.getList();
	},
	methods: {
		async getList() {
			const res = await getRecordInOneDay();
			if (res.data) {
				this.nameData = this.filterNameData(res.data.nameData);
				this.list = this.filterData(res.data.data);
			} else {
				this.nameData = [];
				this.list = [];
			}
		},
		filterNameData(nameData) {
			let arr = [];
			nameData.forEach((ele) => {
				if (ele.tree === 1) {
					arr.push({
						prop: ele.id,
						label: moment(ele.name).format('yyyy-MM-DD HH:mm:ss'),
					});
				}
			});
			nameData.forEach((ele) => {
				arr.forEach((inner) => {
					if (ele.parentId === inner.prop) {
						inner.children = [
							{
								prop: ele.parentId + ele.name,
								label: ele.name,
							},
						];
					}
				});
			});
			return arr;
		},
		filterData(data) {
			let list = [];
			data.forEach((ele) => {
				let obj = {};
				obj.inspectionDetContent = ele.inspectionDetContent;
				ele.data.forEach((inner) => {
					inner.children.forEach((item) => {
						obj[item.parentId + item.dynamicName] = item.dynamicValue;
					});
				});
				list.push(obj);
			});
			return list;
		},
	},
};
</script>
