<template>
	<view class="charts-box">
		<qiun-data-charts ref="lineChart" type="line" :opts="opts" :chartData="chartData" :ontouch="true" canvas2d
			canvasId="canvasId" :disableScroll="true" :onzoom="true" />
	</view>
</template>

<script>
	import {
		nowDate,
		timeToDate
	} from '../../utils/date.js';
	import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/u-charts.js';
	export default {
		props: {
			echartsData: Object
		},
		data() {
			return {
				chartData: {},
				//您可以通过修改 config-ucharts.js 文件中下标为 ['line'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 10, 0, 15],
					enableScroll: true,
					legend: {},
					xAxis: {
						disableGrid: true,
						scrollShow: true,
						itemCount: 2,
						format:"yAxisDemo1"
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2
					},
					extra: {
						line: {
							type: "straight",
							width: 2,
							activeType: "hollow"
						}
					}
				},
				time: [],
				series: []
			};
		},
		mounted() {
			this.getEmit()
		},
		methods: {
			getServerData() {
				const chartData = this.echartsData.chartItemList

				const timeData = chartData[0].valueTimeList
				const time = timeData.map(item => {
					return item.substring(0, 19).replaceAll('-', "")
				})
				this.time = time
				this.series = series
				const series = chartData.map(item => {
					return {
						name: item.describe ? item.describe : item.itemName,
						data: item.valueList
					}
				})
				let res = {
					categories: time,
					series: series
				};
				this.chartData = JSON.parse(JSON.stringify(res));
			},
			getEmit() {
				const {
					bucketType,
					bucketValue
				} = this.echartsData
				let time = null
				switch (bucketType) {
					case 0:
						time = 86400000 * bucketValue

						break;
					case 1:
						time = 3600000 * bucketValue

						break;
					case 2:
						time = 60000 * bucketValue

						break;
					case 3:
						time = 1000 * bucketValue

						break;
				}
				this.getServerData()
				this.lineTime = setInterval(() => {
					const currentTime = nowDate();
					let lastTime;
					uni.getStorage({
						key: 'sTime',
						success: (o) => {
							lastTime = o.data;
						}
					})
					let chartParams = {
						id: this.echartsData.id,
						startTime: lastTime,
						endTime: currentTime
					}
					uni.setStorage({
						key: 'sTime',
						data: currentTime
					})
					uni.$http.get('/chart/getChartById', chartParams).then(res => {
						let chartItemList = res.data.data.chartItemList;
						chartItemList.forEach((item, index) => {
							if (item.itemId === this.echartsData.chartItemList[index].itemId) {
								if (item.valueList.length !== 0) {
									//执行将新数据添加进去，旧数据去除一个
									this.echartsData.chartItemList[index].valueList.push(...item
										.valueList)
									this.echartsData.chartItemList[index].valueList.splice(0, item
										.valueList.length)
									// this.echartsData.chartItemList[index].valueList.slice(item
									// 	.valueList.length, this.echartsData.chartItemList[
									// 		index].valueList.length)
									this.echartsData.chartItemList[index].valueTimeList.push(...
										item
										.valueTimeList)
									this.echartsData.chartItemList[index].valueTimeList.splice(0,
										item
										.valueTimeList.length)
									// this.echartsData.chartItemList[index].valueTimeList.slice(
									// 	item
									// 	.valueTimeList.length, this.echartsData.chartItemList[
									// 		index].valueTimeList.length)
								}
							}
						})
					})
				}, time)
			}
		}
	};
</script>

<style scoped>
	/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
	.charts-box {
		width: 100%;
		height: 540rpx;
	}
</style>