<template>
    <echartsBase :option="setOption"/>
</template>

<script setup>
import { nextTick, onMounted, ref, watch } from 'vue'
import echartsBase from './index.vue'

const props = defineProps({
    title: {
        type: String,
        default: '管理费用项目分摊占比情况'
    },
    color: {
        type: Array,
        default: () => (['#5087EC', '#68BBC4'])
    },
    unit: {
        type: String,
        default: '元'
    },
    datas: {
        type: Array,
        default: () => ([])
    }
})

//初始变量
const setOption = ref({})
const titles = ref(props.title)
const colors = ref(props.color)
const units = ref(props.unit)
const datas = ref(props.datas)

watch(() => [
    props.title,
    props.color,
    props.unit,
], ([title, color, unit]) => {
    titles.value = title
    colors.value = color
    units.value = unit
    setOptions()
})

//深度监听数据变化
watch(() => [
    props.datas,
], ([data]) => {
    datas.value = data
    setOptions()
}, { deep: true })


//设置图表
const setOptions = () => {
    setOption.value = {
        color: colors.value,
        title: {
            text: titles.value,
            left: 'center',
            bottom: '0',
            textStyle: {
                color: '#B3B3B3',
                fontSize: 14,
            }
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            top: 'top'
        },
        series: [
            {
                type: 'pie',
                radius: '70%',
                center: ["50%", "52%"],
                label: {
                    show: true,
                    formatter(param) {
                        return param.name + '\n' +  param.value + units.value;
                    }
                },
                data: datas.value,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    }
}

//渲染完成
onMounted(() => {
    nextTick(() => {
        setOptions()
    })
})
</script>
