<template>
    <div class="card-chart" ref="chartRef"></div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import * as echarts from "echarts";
import { galleryIndicatorLine } from "./chartSeting";
import { deepClone } from "@/common/commonFun";
import eventBus from "@/api/eventBus";

const props = defineProps({
    indexName: {
        type: String,
        default: "",
    },
    type: {
        type: String,
        default: "",
    },
    chartData: {
        type: Object,
        default: () => {},
    },
    isDark: {
        type: Boolean,
        default: false
    },
});

let myChart = ref(null);
const chartRef = ref();
const options = ref({});

onMounted(() => {
    myChart.value = echarts.init(chartRef.value);
    options.value = deepClone(galleryIndicatorLine);
    updateValue(false)
});

eventBus.on("chart:updateOption",(data)=>{
    updateValue(true, data)
})

const updateValue = (isUpdate, data) =>{
    let chartList = {}
    if (isUpdate) {
        // const filterData = filterDataAfterJanuary(data)
        chartList = data
    } else {
        chartList = props.chartData
    }
    myChart.value.clear()
    if(chartList.dates?.length){
        options.value.xAxis.data = chartList.dates.map(item => item.slice(5))
        options.value.series[0].name = chartList.diseaseRiskName
        options.value.legend.data[0] = chartList.diseaseRiskName
        options.value.series[0].data = chartList.diseaseRisk
        options.value.series[1].name = chartList.growthRiskName
        options.value.legend.data[1] = chartList.growthRiskName
        options.value.series[1].data = chartList.growthRisk
        options.value.series[2].name = chartList.phenoParam1Name
        options.value.legend.data[2] = chartList.phenoParam1Name
        options.value.series[2].data = chartList.phenoParam1
        options.value.series[3].name = chartList.phenoParam2Name
        options.value.legend.data[3] = chartList.phenoParam2Name
        options.value.series[3].data = chartList.phenoParam2
        options.value.series[4].name = chartList?.phenoParam3Name
        options.value.legend.data[4] = chartList.phenoParam3Name
        options.value.series[4].data = chartList?.phenoParam3
        if (props.isDark) {
            options.value.yAxis[0].splitLine.lineStyle.color = "#333333"
            options.value.yAxis[0].axisLabel.textStyle.color = "#999999"
            options.value.xAxis.axisLine.lineStyle.color = "#333333"
            options.value.xAxis.axisLabel.textStyle.color = "#999999"
            options.value.legend.textStyle.color = "#fff"
        }
        myChart.value.setOption(options.value);
    }
}

const processData = () => {
    const today = new Date();
    const todayString = today.toISOString().split("T")[0]; // 获取今天的日期，格式为YYYY-MM-DD
    const datePattern = /(\d+)\/(\d+)/; // 日期格式匹配，假设为MM/DD

    props.chartData.forEach((item) => {
        const match = item.date.match(datePattern);
        if (match) {
            const month = parseInt(match[1], 10);
            const day = parseInt(match[2], 10);
            const itemDate = new Date(today.getFullYear(), month - 1, day); // 月份从0开始，所以需要减1
            if (itemDate > today) {
                item.feature = true;
            }
        }
    });
};
</script>

<style lang="scss" scoped>
.card-chart {
    padding: 8px 4px 0 4px;
    width: 100%;
    height: 180px;
    box-sizing: border-box;
}
</style>
