<template>
	<div class="mod-config">
		<el-form :inline="true">
			<el-form-item :label="'当前设备id'">
				<strong>{{ $route.params.id }}</strong></el-form-item
			>
		</el-form>

		<!-- <base-table :page="pageIndex" :size="pageSize" :data="dataList" :table-head-configs="tableConfigs" :max-height="calcMaxHeight(8)" @operate-event="handleOperations" @refreshDataList="getDataList" /> -->
		<base-table :data="dataList" :table-head-configs="tableConfigs" :max-height="calcMaxHeight(8)" />
	</div>
</template>

<script>
import { calcMaxHeight } from '@/utils'
import { timeFilter } from '@/utils/filters'
import AddOrUpdate from '@/components/base-dialog/addOrUpdate'
import i18n from '@/i18n'
import BaseTable from '@/components/base-table'
import { pick } from 'lodash/object'

export default {
	data() {
		return {
			calcMaxHeight,
			tableConfigs: [],
			dataList: [],
			dataListLoading: false,
			dataListSelections: []
		}
	},
	components: {
		AddOrUpdate,
		BaseTable
	},
	activated() {
		this.getDataList()
	},
	mouted() {
		this.getDataList()
	},
	methods: {
		// 获取数据列表
		getDataList() {
			this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl(`/monitoring/equipmentValueMonitor/runLog/${this.$route.params.id}`),
				method: 'get'
			}).then(({ data: res }) => {
				if (
					res &&
					res.code === 0 &&
					res.data &&
					res.data.length > 0 &&
					res.data[0].nameData &&
					res.data[0].nameData.length > 0 &&
					res.data[0].data &&
					res.data[0].data.length > 0
				) {
					this.setTableProps(res.data[0].nameData)
					this.setTableData(res.data[0].data)
				} else {
					this.dataList = []
				}
				this.dataListLoading = false
			})
		},
		setTableProps(nameData) {
			this.tableConfigs = [
				{
					type: 'index',
					name: i18n.t('index')
				},
				{ prop: 'time', name: '时间', filter: timeFilter },
				{ prop: 'plcCode', name: 'PLC 编码' },
				{ prop: 'equName', name: '设备名称' },
				{ prop: 'equCode', name: '设备编码' },
				// ...['数值1', '数值2', '数值3'].map(name => {
				// 	return { prop: name, name }
				// })
				...Array.from(new Set(nameData.map(item => item.name))).map(name => ({ prop: name, name }))
			]
		},
		setTableData(data) {
			this.dataList = data.map(item => {
				const rowItem = pick(item, ['time', 'plcCode', 'equName', 'equCode'])
				if (item.data && item.data.length > 0) {
					item.data.forEach(param => {
						rowItem[param.dynamicName] = param.dynamicValue
					})
				}
				return rowItem
			})
		}
	}
}
</script>
