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

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, 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 { buildExportBaseName, formatExportDateTime } from '@/utils/echartMethod'
// import { nextTick } from 'process'

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

const props = defineProps(['options', 'isInterVal', 'pieInterVal'])
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 = () => {
    if (!props.isInterVal && !props.pieInterVal) {
        chart?.dispose()
    }
    // chart?.dispose()
    chart = echarts.init(chartRef.value as HTMLDivElement)
    const options = {
        title: {
            left: 'center',
            // textStyle: {
            color: '#000',
            top: 5,
            textStyle: {
                color: '#000',
                fontSize: '15'
            },
            // },
            ...(props.options?.title || null)
        },
        tooltip: {
            trigger: 'axis',

            textStyle: {
                color: '#fff',
                fontStyle: 'normal',
                opacity: 0.35,
                fontSize: 14
            },
            backgroundColor: 'rgba(0,0,0,0.55)',
            borderWidth: 0,
            // confine: true,
            ...(props.options?.tooltip || null)
        },
        toolbox: {
            right: 20,
            top: props.options?.title?.text ? 20 : 0,
            feature: {
                saveAsImage: {
                    title: '下载图片',
                    name: props.options?.exportFileName
                        ? buildExportBaseName(props.options.exportFileName)
                        : ((props.options?.title?.text || '图表') + '_' + formatExportDateTime())
                },
                ...(props.options?.toolbox?.featureProps || null)
            },
            // },
            ...(props.options?.toolbox || null)
        },
        legend: {
            right: 100,
            top: props.options?.title?.text ? 25 : 5,
            itemGap: 10,
            itemStyle: {},
            type: 'scroll', // 开启滚动分页
            // textStyle: {
            fontSize: 12,
            // padding: [2, 0, 0, 0], //[上、右、下、左]
            // },
            itemWidth: 15,
            itemHeight: 10,
            ...(props.options?.legend || null)
        },
        grid: {
            top: props.options?.title?.text ? '50px' : '25px',
            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,
                filterMode: 'none'
            },
            {
                start: 0,
                height: 13,
                bottom: '20px',
                end: 100,
                filterMode: 'none'
            }
            // {
            //     show: true,
            //     yAxisIndex: 0,
            //     width: 12,
            //     handleSize: 8,
            //     showDataShadow: false,
            //     right: 12
            // }
        ],
        color: props.options?.color || color,
        series: props.options?.series,
        ...setStep(props.options?.options)
    }
    // console.log(options.series,"获取x轴");
    handlerBar(options)
    // 处理柱状图
    chart.setOption(options, true)
    chart.group = 'group'
    // 添加点击事件
    chart.on('click', function (params: any) {
        emit('chartClick', params)
    })
    setTimeout(() => {
        chart.resize()
    }, 0)
}
const handlerBar = (options: any) => {
    if (Array.isArray(options.series)) {
        options.series.forEach((item: any) => {
            if (item.type === 'bar') {
                item.barMinHeight = 5
                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) {
                // 分类轴为字符串，数值轴才做小数格式化
                if (typeof value !== 'number' || Number.isNaN(value)) return value
                return parseFloat(value.toFixed(1))
            }
        },
        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'
            // }
        }
        // boundaryGap: false,
    }
    // 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
        }
    }
}
// const setStep = (name: string) => {
//     const isShanBian = name.includes('闪变')
//     return isShanBian ? { step: 'end' as const } : { }
// }
const setStep = (options: any) => {
    if(options?.series){
        options.series.forEach((item: any) => {
            if(item.type === 'line' && item.name.includes('闪变')){
                item.step = 'end'
            }
        })
    }

    return options
}

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>
