<template>
  <div :id="id" :style="{height: size.height+'px', width: size.width+'px'}" />
</template>

<script>
import echarts from 'echarts';
import { getRelevanceMapList, getSimulationPrdList } from '@/api/statistics';

export default {
	props: {
		id: {
			type: String,
			default: 'chart'
		},
		size: {
			type: Object,
			required: true
		}
	},
	data() {
		return {
			option: {
				backgroundColor: '#F0F2F5',
				title: {
					top: 20,
					text: '仿真统计',
					textStyle: {
						fontWeight: 'normal',
						fontSize: 18,
						color: '#000000'
					},
					left: 'center'
				},
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						type: 'shadow',
						lineStyle: {
							color: '#57617B'
						}
					}
				},
				legend: {
					top: 20,
					orient: 'vertical',
					icon: 'rect',
					itemWidth: 14,
					itemHeight: 5,
					itemGap: 13,
					data: [],
					right: '4%',
					textStyle: {
						fontSize: 12,
						color: '#C0C0C0'
					}
				},
				grid: {
					top: 100,
					left: '2%',
					right: '2%',
					bottom: '2%',
					containLabel: true
				},
				xAxis: [{
					type: 'category',
					boundaryGap: true,
					axisLine: {
						lineStyle: {
							color: '#57617B'
						}
					},
					data: []
				}],
				yAxis: [{
					type: 'value',
					name: '(s)',
					axisTick: {
						show: false
					},
					axisLine: {
						lineStyle: {
							color: '#57617B'
						}
					},
					axisLabel: {
						margin: 10,
						textStyle: {
							fontSize: 14
						}
					},
					splitLine: {
						lineStyle: {
							color: '#57617B'
						}
					}
				}],
				series: []
			},
			chart: null
		};
	},
	watch: {
		size() {
			return this.chart.resize({...this.size, silent: false});
		}
	},
	async created() {
		await this.loadExamData();
	},
	mounted() {
		this.initChart();
	},
	beforeDestroy() {
		if (!this.chart) {
			return;
		}
		this.chart.dispose();
		this.chart = null;
	},
	methods: {
		initChart() {
			this.chart = echarts.init(document.getElementById(this.id));
			this.chart.setOption(this.option);
		},
		async loadExamData() {
			const data = [];
			const resp = await getRelevanceMapList();
			if (resp) {
				const info = resp.data || [];
				for (var i = 0; i < info.length; i++) {
					const item = { ...info[i], data: []};
					const rest = await getSimulationPrdList(info[i].id);
					if (rest) {
						item.data = rest.data || [];
					}
					data.push(item);
				}
			}

			this.option.series = [];
			this.option.legend.data = [];
			this.option.xAxis[0].data = ['综合演练云平台', '司机模拟驾驶系统', 'ATS行调工作站', 'ATS现地工作站'];
			data.forEach(elem => {
				this.option.legend.data.push(elem.name);
				this.option.series.push({
					name: elem.name,
					type: 'bar',
					smooth: true,
					symbol: 'circle',
					symbolSize: 5,
					showSymbol: false,
					lineStyle: {
						normal: {
							width: 1
						}
					},
					data: [0, 0, 0, 0]
				});
			});

			data.forEach((elem, i) => {
				elem.data.forEach(item => {
					const index = this.option.xAxis[0].data.findIndex(name => { return name == item.statsProjectName; });
					if (index >= 0) {
						this.option.series[i].data[index] = item.duration;
					}
				});
			});

			this.chart.setOption(this.option);
		}
	}
};
</script>
