<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 '@/views/dashboard/mixins/resize'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100%'
        },
        barAndLineX: {
            type: Array,
            default: () => ['机具']
        },
        barY1: {
            type: Array,
            default: () => ['0']
        },
        barY2: {
            type: Array,
            default: () => ['0']
        }
    },
    data() {
        return {
            chart: null
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    watch: {
        barAndLineX: {
            deep: true,
            handler() {
                this.initChart()
            }
        },
        barY1: {
            deep: true,
            handler() {
                this.initChart()
            }
        },
        barY2: {
            deep: true,
            handler() {
                this.initChart()
            }
        }
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')

            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow' // 阴影指示器
                    }
                },

                legend: {
                    data: ['需求', '供应'],
                    icon: 'path://M 0,0 h 16 a 6,6 0 0 1 6,6 v 8 a 6,6 0 0 1 -6,6 h -16 a 6,6 0 0 1 -6,-6 v -8 a 6,6 0 0 1 6,-6 z',
                    bottom: 10, // 图例放在 x 轴下方
                    itemWidth: 20, // 图例项宽度
                    itemHeight: 13, // 图例项高度
                    itemGap: 10, // 图例项之间的间距
                    padding: [5, 10], // 图例内边距
                    textStyle: {
                        color: '#333', // 图例文字颜色
                        lineHeight: 13
                    }
                },
                grid: {
                    top: 10,
                    left: '2%',
                    right: '2%',
                    bottom: '12%', // 底部留出空间给图例
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        // data: ['卸扣5t', '卸扣8t', '手扳葫芦', '绞磨', '机具1', '机具2', '机具13'],
                        data: this.barAndLineX,
                        axisTick: {
                            alignWithLabel: true
                        },
                        axisLabel: {
                            rotate: -15, // x 轴标签倾斜 45°
                            margin: 30, // 标签与轴线的距离
                            color: '#333'
                        },
                        axisLine: {
                            lineStyle: {
                                color: '#a8a7a7' // x 轴轴线颜色
                            }
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisTick: {
                            show: false
                        },
                        axisLine: {
                            lineStyle: {
                                color: '#a8a7a7' // x 轴轴线颜色
                            }
                        }
                    }
                ],
                series: [
                    {
                        name: '需求',
                        type: 'bar',
                        barWidth: '20%', // 柱子宽度
                        data: this.barY1,
                        animationDuration: 2000,
                        itemStyle: {
                            color: '#26a8ff' // 自定义颜色
                        }
                    },
                    {
                        name: '供应',
                        type: 'bar',
                        barWidth: '20%', // 柱子宽度
                        data: this.barY2,
                        animationDuration: 2000,
                        itemStyle: {
                            color: '#ff8e9d' // 自定义颜色
                        }
                    }
                ]
            })
        }
    }
}
</script>
