<template>
    <!-- 位移监测 -->
    <div style="height: 100%">
        <ChartsBox :boxTitle="`位移监测`">
            <div ref="chartContainer" class="container"> </div>
        </ChartsBox>
    </div>
</template>

<script>
import ChartsBox from '@/components/ChartsBox/index'
import * as echarts from 'echarts'
// require('echarts/theme/macarons')
import 'echarts/theme/macarons'
import { getAttrListAPI } from '@/api/home-high-formwork.js'
export default {
    components: { ChartsBox },
    props: {
        queryLineId: {
            type: [String, Number],
            default: () => '',
        },
    },
    data() {
        return {
            chart: null,
            xData: [],
            yData: [],
        }
    },
    mounted() {},
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null

        window.removeEventListener('resize', this.handleResize)
        if (this.chart) {
            this.chart.dispose()
            this.chart = null
        }
    },
    methods: {
        initChart() {
            // 使用 ref 获取 container 元素
            const container = this.$refs.chartContainer
            this.chart = echarts.init(container, 'macarons')

            this.chart.setOption({
                xAxis: {
                    type: 'category',
                    data: this.xData,
                    boundaryGap: false,
                    axisTick: { show: true },
                    axisLabel: { color: '#fff' },
                    axisLine: {
                        lineStyle: { color: '#4494db' },
                    },
                    // 添加x轴网格线虚线样式
                },
                yAxis: {
                    axisTick: { show: false },
                    axisLine: {
                        lineStyle: { color: '#4494db' },
                    },
                    axisLabel: { color: '#fff' },
                    // 添加y轴网格线虚线样式
                    splitLine: {
                        lineStyle: {
                            type: 'dashed',
                            color: 'rgba(123, 95, 75, 0.3)',
                            width: 1,
                        },
                    },
                },
                grid: {
                    left: 10,
                    right: 15,
                    bottom: 5,
                    top: 12,
                    containLabel: true,
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { type: 'cross' },
                    padding: [5, 10],
                },
                series: [
                    {
                        name: '数量',
                        type: 'line',
                        smooth: true, // 启用平滑曲线
                        data: this.yData,
                        itemStyle: {
                            color: '#1AD8E6 ',
                        },

                        areaStyle: {
                            color: new echarts.graphic.LinearGradient(
                                0,
                                0,
                                0,
                                1,
                                [
                                    {
                                        offset: 0,
                                        color: 'rgba(26, 216, 230, 0.2)',
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(266, 156, 20, 0.1)',
                                    },
                                ],
                            ),
                        },
                        // 添加线条动画效果
                        animationDuration: 3000,
                        animationEasing: 'cubicInOut',
                        symbol: 'circle',
                        symbolSize: 6,
                        showSymbol: false, // 初始不显示点
                        // 高亮动画配置
                        emphasis: {
                            scale: true,
                            itemStyle: {
                                color: '#ffeb3b',
                                borderColor: '#fff',
                                borderWidth: 2,
                            },
                        },
                        // 线条动画效果
                        lineAnimation: {
                            duration: 3000,
                            easing: 'cubicInOut',
                            delay: function (idx) {
                                return idx * 100
                            },
                        },
                        effect: {
                            show: true,
                            period: 4, // 动画周期
                            trailLength: 0.2, // 动画轨迹长度
                            symbol: 'arrow', // 使用箭头符号
                            symbolSize: 10, // 设置箭头大小
                            loop: true, // 循环播放
                            // 动画的方向和效果
                            constantSpeed: 15, // 设定动画的速度
                        },
                    },
                ],
                // 添加全局动画配置
                animation: {
                    duration: 3000,
                    easing: 'cubicInOut',
                    seriesKey: 'line',
                    animationDelay: function (idx) {
                        return idx * 100
                    },
                },
            })

            // 添加窗口大小变化时的自适应
            window.addEventListener('resize', this.handleResize)

            setTimeout(() => {
                this.chart.dispatchAction({
                    type: 'highlight',
                    seriesIndex: 0,
                })
            }, 500)
        },
        handleResize() {
            if (this.chart) {
                this.chart.resize()
            }
        },

        async getAttrListData(devId) {
            const { data: res } = await getAttrListAPI({
                type: 1,
                devId,
            })

            console.log(
                `%c🔍 高支模监测 ----> 位移监测(中一) 数据出参 %c`,
                'background: linear-gradient(90deg, #FF6B6B, #4ECDC4); color: white; padding: 5px 10px; border-radius: 5px; font-weight: bold;',
                '',
                res,
            )

            if (res.length > 0) {
                this.xData = res.map((e) => e.name)
                this.yData = res
            } else {
                this.xData = ['暂无数据']
                this.yData = [
                    {
                        name: '暂无数据',
                        value: 0,
                    },
                ]
            }

            this.$nextTick(() => {
                this.initChart()
            })
        },
    },

    watch: {
        queryLineId: {
            handler(newValue) {
                this.getAttrListData(newValue)
            },
            deep: true,
        },
    },
}
</script>

<style lang="scss" scoped>
.container {
    height: 100%;
    width: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    color: #fff;
    z-index: 9999;
}
</style>
