<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: () => ([])
    },
    ydata:{
         type: Array,
         default: () => ([])
    },
    xdata:{
        type: Array,
         default: () => ([])
    },
    legend:{
      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 xdatas = ref(props.xdata)
const ydatas = ref(props.ydata)
const legends = ref(props.legend)

watch(() => [
    props.title,
    props.color,
    props.unit,
    props.xdata,
    props.ydata,
    props.legend,
], ([title, color, unit,xdata,ydata,legend]) => {
    titles.value = title
    colors.value = color
    units.value = unit
    xdatas.value=xdata
    ydatas.value=ydata
    legends.value=legend
    setOptions()
})

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


//设置图表
const setOptions = () => {
    setOption.value = {
        color: colors.value,
        title: {
    
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    // data: ['风险暂停计划', '正常计划',]
     data:legends.value
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  // toolbox: {
  //   feature: {
  //     saveAsImage: {}
  //   }
  // },
  xAxis: {
    type: 'category',
    // boundaryGap: false,
   
    data:xdatas.value,
  },
  yAxis: {
    type: 'value',
    
  },

  series:ydatas.value
}
}

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