<template>
    <div ref="chartRef" :style="{ height: height, width: width }" />
</template>

<script setup>
import { getCurrentInstance, watch, onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import theme from '../theme/chalk.json'
echarts.registerTheme('theme', theme)
import { CanvasRenderer } from 'echarts/renderers'
echarts.use([CanvasRenderer])
const { proxy } = getCurrentInstance()
const chartRef = ref(null)
const props = defineProps({
    width: {
        type: String,
        default: '100%'
    },
    height: {
        type: String,
        default: '350px'
    }
})
let options = {
    title: {
        text: '',
        left: 'left'
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        data: [],
        boundaryGap: true,
        axisTick: {
            show: false
        }
    },
    yAxis: {
        type: 'value',
        axisTick: {
            show: false
        }
    },
    // 图例
    legend: {
        type: 'scroll',
        top: 'top',
        width: '60%',
        right: '0%'
    },
    series: [
    ]
}
const chart = ref(null)
function initChart() {
    chart.value = echarts.init(proxy.$refs.chartRef, 'theme')
    chart.value.setOption(options)
}
onMounted(() => {
    initChart()
    window.addEventListener('resize', () => {
        chart.value?.resize()
    })
})
/// ============ 修改数据 ===========
import { getBarProcessProductStatistic } from '@/api/smartScreen/ReportScreen/index.js'
function updateData() {
    getBarProcessProductStatistic().then((res) => {
        if (res.code === 200) {
            options.title = res.data.title
            options.xAxis.data = res.data.xAxis.data
            let _series = res.data.series
            _series.forEach((item) => {
                item.label = {
                    show: true,
                    position: 'top',
                    textBorderColor: '#fff',
                    textBorderWidth: 0,
                    formatter: '{c}',
                    color: '#fff'
                }
            })
            options.series = _series
            chart.value.setOption(options)
        }
    })
}
function handleQuery() {
    updateData()
}
handleQuery()
let timer1 = null
const clearSearchTimer = () => {
    clearInterval(timer1)
    timer1 = null
}
const createSearchTimer = () => {
    clearSearchTimer()
    timer1 = setInterval(() => {
        handleQuery()
    }, 1000 * 60 * 5)
}
onMounted(() => {
    createSearchTimer()
})
onUnmounted(() => {
    clearSearchTimer()
})
/// ==========================================
</script>
