<template>
    <div class="chart">
        <div ref="chartRef" class="my-chart" />
    </div>
</template>

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, defineExpose, watch, nextTick } from 'vue'
// import echarts from './echarts'
import * as echarts from 'echarts' // 全引入
import 'echarts-gl'
import 'echarts-liquidfill'
import 'echarts/lib/component/dataZoom'
import { color, gradeColor3 } from './color'
import { useConfig } from '@/stores/config'
import { saveAs } from 'file-saver'
// import { nextTick } from 'process'

const config = useConfig()
color[0] = config.layout.elementUiPrimary[0]
const chartRef = ref<HTMLDivElement>()

const props = defineProps(['options'])
let chart: echarts.ECharts | any = null
const resizeHandler = () => {
    // 不在视野中的时候不进行resize
    if (!chartRef.value) return
    if (chartRef.value.offsetHeight == 0) return
    chart.getZr().painter.getViewportRoot().style.display = 'none'
    requestAnimationFrame(() => {
        chart.resize()
        chart.getZr().painter.getViewportRoot().style.display = ''
    })
}
const initChart = () => {
    chart?.dispose()

    chart = echarts.init(chartRef.value as HTMLDivElement)
    const options = {
        title: {
            left: 'center',
            // textStyle: {
            color: '#000',
            fontSize: 18,
            // },
            ...(props.options?.title || null)
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow',
                label: {
                    color: '#fff',
                    fontSize: 16
                }
            },
            textStyle: {
                color: '#fff',
                fontStyle: 'normal',
                opacity: 0.35,
                fontSize: 14
            },
            backgroundColor: 'rgba(0,0,0,0.35)',
            borderWidth: 0,
            ...(props.options?.tooltip || null)
        },
        legend: {
            right: 20,
            top: 0,
            itemGap: 10,
            itemStyle: {},
            // textStyle: {
            fontSize: 12,
            padding: [2, 0, 0, 0], //[上、右、下、左]
            // },
            itemWidth: 15,
            itemHeight: 10,
            ...(props.options?.legend || null)
        },
        grid: {
            top: '50px',
            left: '30px',
            right: '70px',
            bottom: props.options?.options?.dataZoom === null ? '10px' : '40px',
            containLabel: true,
            ...(props.options?.grid || null)
        },
        xAxis: props.options?.xAxis ? handlerXAxis() : null,
        yAxis: props.options?.yAxis ? handlerYAxis() : null,
        dataZoom: props.options?.dataZoom || [
            {
                type: 'inside',
                height: 13,
                start: 0,
                bottom: '20px',
                end: 100
            },
            {
                start: 0,
                height: 13,
                bottom: '20px',
                end: 100
            }
        ],
        color: props.options?.color || color,
        series: props.options?.series,
        ...props.options?.options
    }
    handlerBar(options)
    // 处理柱状图
    chart.setOption(options)
    setTimeout(() => {
        chart.resize()
    }, 0)
}
const handlerBar = (options: any) => {
    if (Array.isArray(options.series)) {
        options.series.forEach((item: any) => {
            if (item.type === 'bar') {
                item.barMinHeight = 10
                item.barMaxWidth = 20
                item.itemStyle = Object.assign(
                    {
                        color: (params: any) => {
                            if (params.value == 0 || params.value == 3.14159) {
                                return '#ccc'
                            } else {
                                return props.options?.color
                                    ? props.options?.color[params.seriesIndex]
                                    : color[params.seriesIndex]
                            }
                        }
                    },
                    item.itemStyle
                )
            }
        })
    }
}
const handlerYAxis = () => {
    let temp = {
        type: 'value',
        nameGap: 15,
        nameTextStyle: {
            color: '#000'
        },
        splitNumber:5,
        minInterval: 1,
        axisLine: {
            show: true,
            lineStyle: {
                color: '#000'
            }
        },
        axisLabel: {
            color: '#000',
            fontSize: 14,
            formatter: function (value) {
                return value.toFixed(0); // 格式化显示为一位小数
            }
        },
        splitLine: {
            lineStyle: {
                // 使用深浅的间隔色
                color: ['#ccc'],
                type: 'dashed',
                opacity: 0.5
            }
        },

    }
    // props.options?.xAxis 是数组还是对象
    if (Array.isArray(props.options?.yAxis)) {
        return props.options?.yAxis.map((item: any) => {
            return {
                ...temp,
                ...item
            }
        })
    } else {
        return {
            ...temp,
            ...props.options?.yAxis
        }
    }
}
const handlerXAxis = () => {
    let temp = {
        type: 'category',
        axisTick: { show: false },
        axisLine: {
            // lineStyle: {
            color: '#000'
            // }
        },
        axisLabel: {
            // textStyle: {
            fontFamily: 'dinproRegular',
            color: '#000',
            fontSize: '12'
            // }
        }
    }
    // props.options?.xAxis 是数组还是对象
    if (Array.isArray(props.options?.xAxis)) {
        return props.options?.xAxis.map((item: any) => {
            return {
                ...temp,
                ...item
            }
        })
    } else {
        return {
            ...temp,
            ...props.options?.xAxis
        }
    }
}
let throttle: ReturnType<typeof setTimeout>
// 动态计算table高度
const resizeObserver = new ResizeObserver(entries => {
    for (const entry of entries) {
        if (throttle) {
            clearTimeout(throttle)
        }
        throttle = setTimeout(() => {
            resizeHandler()
        }, 100)
    }
})
onMounted(() => {
    initChart()
    resizeObserver.observe(chartRef.value!)
})
defineExpose({ initChart })
onBeforeUnmount(() => {
    resizeObserver.unobserve(chartRef.value!)
    chart?.dispose()
})
watch(
    () => props.options,
    (newVal, oldVal) => {
        initChart()
    }
)
</script>

<style lang="scss" scoped>
.chart {
    width: 100%;
    height: 100%;
    position: relative;

    .el-button {
        position: absolute;
        right: 0px;
        top: -60px;
    }

    .my-chart {
        height: 100%;
        width: 100%;
    }
}
</style>
