<template>
    <div class="BarChart-box">
        <div ref="echart" class="BarChart-echarts" :style="`width : ${chart?.clientWidth}px`"/>
    </div>
</template>

<script setup>
import * as echarts from 'echarts'
import { nextTick, onMounted, onUnmounted, ref,watch } from 'vue'
const props = defineProps({
    datas: {
        type: Array,
        default: () => ([])
    }
})


//初始变量
let chart = null;
const echart = ref(null)
const datas = ref(props.datas)

//监听
watch(() => props.datas, (data) => {
    datas.value = data
    setDatas(data)
})

//初始化图表
const initChart = () => {
    chart = echarts.init(echart.value)
    setDatas(props.datas)
}

//设置数据
const setDatas = (data) => {
    let AxisData = [], approvalAmount = [], awaitAmount = [], submitAmount = [];
    for (let i = 0; i < data.length; i++) {
        AxisData.push(data[i].title)
        approvalAmount.push(data[i]?.approvalAmount)
        awaitAmount.push(data[i]?.awaitAmount)
        submitAmount.push(data[i]?.submitAmount)
    }
    setOptions(AxisData,approvalAmount,awaitAmount,submitAmount)
}

//监听浏览器窗口变化
const windowResize = () => {
    window.addEventListener("resize", resizeEvent);
}

const resizeEvent = () => {
    window.requestAnimationFrame(() => {
        chart.resize();
    })
}

//设置图表
const setOptions = (AxisData,approvalAmount,awaitAmount,submitAmount) => {
    chart.setOption({
        color: ['#F7AD2D', '#3187FF', '#1ACC96'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
            top: 'bottom',
            data: ['待审批', '已填报', '已审批']
        },
        xAxis: {
            type: 'value',
            interval: 1000,
        },
        yAxis: {
            type: 'category',
            data: AxisData
        },
        series: [
            {
                name: '待审批',
                type: 'bar',
                data: awaitAmount,
                label: {
                    show: true,
                    position: 'right'
                }
            },
            {
                name: '已填报',
                type: 'bar',
                data: submitAmount,
                label: {
                    show: true,
                    position: 'right'
                }
            },
            {
                name: '已审批',
                type: 'bar',
                data: approvalAmount,
                label: {
                    show: true,
                    position: 'right'
                }
            }
        ]
    })
}

//渲染完成
onMounted(() => {
    nextTick(() => {
        initChart()
        windowResize()
    })
})

//被卸载
onUnmounted(() => {
    window.removeEventListener("resize",resizeEvent);
    chart.dispose()
    chart = null
})


const onResize = () => {
    nextTick(() => {
        chart.resize();
    })
}

// 暴露出去
defineExpose({
    onResize
})
</script>

<style lang="scss" scoped>
.BarChart-box {
    height: 100%;
    overflow: hidden;
    position: relative;
    .BarChart-echarts {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 2;
        width: 100%;
        height: 100%;
    }
}
</style>
