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

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

const props = defineProps({
    title: {
        type: String,
        default: '管理费已支出与剩余预算曲线图'
    },
    color: {
        type: Array,
        default: () => (['#4095E5', '#C25750'])
    },
    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)

//创建月份
const monthData = ref([])
for (let i = 1; i < 13; i++) {
    monthData.value = [...monthData.value, i + '月']
}

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
    setDataFormat()
}, { deep: true })


//处理数据
const setDataFormat = () => {
    const data = datas.value
    let legend = [], series = [];
    for (let i = 0; i < data.length; i++) {
        if (data[i].name) {
            legend.push(data[i].name)
            series.push({
                name: data[i].name,
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                data: data[i].value
            })
        }
    }
    setOptions(legend, series)
}


//设置图表
const setOptions = (legend = [], series = []) => {
    setOption.value = {
       legend: {
        left: 'top',
         orient: 'vertical',
  },
  toolbox: {
    show: false,
    feature: {
      mark: { show: true },
      dataView: { show: true, readOnly: false },
      restore: { show: true },
      saveAsImage: { show: true }
    }
  },
  series: [
    {
      name: 'Nightingale Chart',
      type: 'pie',
      radius: [50, 250],
      center: ['50%', '50%'],
      roseType: 'area',
      itemStyle: {
        borderRadius: 8
      },
      data: [
        { value: 40, name: '商务佣金' },
        { value: 38, name: '业务招待费' },
        { value: 32, name: '工资' },
        { value: 30, name: '五险一金' },
        { value: 28, name: '房租' },
        { value: 26, name: '设备采购' },
        { value: 22, name: '礼品费' },
        { value: 18, name: '设备维护费' }
      ]
    }
  ]
    }
}

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