<template>
  <div v-if="store.isDetailsModalVisible" class="modal-overlay" @click="store.closeDetailsModal">
    <div class="modal-content" @click.stop>
      <img class="model-title" :src="titleImage" alt="">
      <button class="close-btn" @click="store.closeDetailsModal">×</button>
      <div ref="detailsChartRef" class="details-chart-container"></div>
      <img class="model-bottom-icon" src="@/assets/images/icon/dialog_bottom_icon.png" alt="">
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onUnmounted, nextTick, defineProps } from 'vue';
import { useKeyNodeStore2 } from '@/store/keyNodeStore2';
import * as echarts from 'echarts/core';
import { SVGRenderer } from 'echarts/renderers';
import { LineChart } from 'echarts/charts';
import { GridComponent, TooltipComponent } from 'echarts/components';
import shixuTitle from '@/assets/images/head/event_shixu_hotmap_title.png';
import leijiTitle from '@/assets/images/head/event_leiji_hotmap_title.png';
import { computed } from 'vue';


const props = defineProps({
  activeChartTab: {
    type: String,
    default: 'trend'
  }
})

const titleImage = computed(() => {
  return props.activeChartTab === 'trend' ? shixuTitle : leijiTitle;
});

echarts.use([SVGRenderer, LineChart, GridComponent, TooltipComponent]);
const store = useKeyNodeStore2();
const detailsChartRef = ref(null);
let myChart = null;

watch(() => store.isDetailsModalVisible, async (isVisible) => {
  if (isVisible && store.detailsModalChartConfig) {
    await nextTick();
    if (detailsChartRef.value) {
        myChart = echarts.init(detailsChartRef.value);
        const option = {
          ...store.detailsModalChartConfig.option
        }
        if(option.yAxis) {
          const yAxis = Array.isArray(option.yAxis) ? option.yAxis[0] : option.yAxis;
          yAxis.name = '数量';
          yAxis.nameLocation = 'end'; // 显示在y轴最上方
          yAxis.nameTextStyle = {
            color: '#a7c5d4',
            fontSize: 12,
            padding: [0, 0, 0, -68] // 调整位置，避免与坐标轴重叠
          };
          if(Array.isArray(option.yAxis)) {
            option.yAxis[0] = yAxis;
          }else {
            option.yAxis = yAxis;
          }
        }
        // 添加以下代码将实心圆点改为空心圆点
        /* if(option.series && Array.isArray(option.series)) {
          option.series.forEach(series => {
            if(series.itemStyle) {
              series.itemStyle.color = 'transparent'; // 设置内部透明
              // 确保边框颜色和宽度
              if(!series.itemStyle.borderColor) series.itemStyle.borderColor = '#45B8FD';
              if(!series.itemStyle.borderWidth) series.itemStyle.borderWidth = 2;
            } else {
              series.itemStyle = {
                color: 'transparent',
                borderColor: '#00baff',
                borderWidth: 2
              };
            }
          });
        } */
         myChart.setOption(option);
        // myChart.setOption(store.detailsModalChartConfig.option);
    }
  } else if (!isVisible && myChart) {
    myChart.dispose();
    myChart = null;
  }
});

onUnmounted(() => {
  if (myChart) {
    myChart.dispose();
  }
});
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  border-radius: 2px;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}
.modal-content {
  position: relative;
  background-color: #0d1b38;
  padding: 20px;
  border-radius: 2px;
  border: 1px solid #3a95ff;
  box-shadow: 0 0 25px rgba(58, 149, 255, 0.5);
  width: 50vw;
  height: 45vh;
  display: flex;
  flex-direction: column;
}
.modal-content .model-title {
  margin-top: -28px;
  margin-left: -21px;
  width: 30%;
  height: 15%;
}
.close-btn {
  position: absolute;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  top: 10px;
  right: 15px;
  border: none;
  color: #a7c5d4;
  background: none;
  font-size: 24px;
  cursor: pointer;
}
.details-chart-container {
  width: 100%;
  flex-grow: 1;
}
.modal-content .model-bottom-icon {
  position: absolute;
  bottom: -12px;
  left: 50%;
  width: 6%;
  height: 6%;
}
</style>