<template>
	<div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons'); // echarts theme
import resize from '@/utils/chartMixins/resize';

const animationDuration = 1000;

export default {
	mixins: [resize],
	props: {
		className: {
			type: String,
			default: 'chart',
		},
		title: {
			type: String,
			default: '',
		},
		width: {
			type: String,
			default: '100%',
		},
		height: {
			type: String,
			default: '300px',
		},
		histogram: {
			type: Array,
			default: () => [],
		},
	},
	data() {
		return {
			chart: null,
		};
	},
	beforeDestroy() {
		if (!this.chart) {
			return;
		}
		this.chart.dispose();
		this.chart = null;
	},
	methods: {
		initChart() {
			var nameArr = [];
			var valueArr = [];
			var dayArr = [];
			if (this.histogram.length > 0) {
				this.histogram.forEach((item) => {
					nameArr.push(item.goodsName);
					valueArr.push(item.num);
					dayArr.push(item.day);
				});
			}
			this.chart = echarts.init(this.$el, 'macarons');

			this.chart.setOption({
				title: {
					text: this.title
						? '{space|}{tip|}{space|}{value|' + this.title + '}'
						: '',
					left: '0%',
					top: '0%',
					textStyle: {
						rich: {
							tip: {
								width: 4,
								height: 18,
								backgroundColor: '#0B58FF',
								marginRight: 6,
							},
							space: {
								width: 8,
							},
							value: {
								fontSize: 16,
								fontWeight: 'bold',
								color: 'black',
							},
						},
					},
				},
				color: ['#288AFF', '#8EF0AB', '#FFDC94'],
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						// 坐标轴指示器，坐标轴触发有效
						type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
					},
				},
				grid: [
					{
						containLabel: true,
						top: 40,
						left: '2%',
						right: '2%',
						bottom: '3%',
						bottom: '55%',
					},
					{
						containLabel: true,
						top: 40,
						left: '2%',
						right: '2%',
						bottom: '3%',
						top: '55%',
					},
				],
				xAxis: [
					{
						type: 'category',
						data: nameArr,
						gridIndex: 0,
						axisTick: {
							alignWithLabel: true,
						},
						axisLabel: {
							color: '#979797',
						},
						axisLine: {
							lineStyle: {
								color: '#979797',
							},
						},
					},
					{
						type: 'category',
						data: nameArr,
						gridIndex: 1,
						axisTick: {
							alignWithLabel: true,
						},
						axisLabel: {
							color: '#979797',
						},
						axisLine: {
							lineStyle: {
								color: '#979797',
							},
						},
					},
				],
				yAxis: [
					{
						gridIndex: 0,
						axisTick: {
							show: false,
						},
						splitArea: {
							show: false,
						},
						axisLabel: {
							color: '#979797',
						},
					},
					{
						gridIndex: 1,
						axisTick: {
							show: false,
						},
						splitArea: {
							show: false,
						},
						axisLabel: {
							color: '#979797',
						},
					},
				],
				series: [
					{
						name: '数量',
						type: 'bar',
						barWidth: '20',
						data: valueArr,
						animationDuration,
					},
					{
						name: '天数',
						type: 'bar',
						barWidth: '20',
						data: dayArr,
						animationDuration,
						xAxisIndex: 1,
						yAxisIndex: 1,
					},
				],
			});
		},
	},
};
</script>
