<template>
  <div v-loading="loading">
    <MetricsCards v-model="metrics" :metric-items="metricsItems" @change="changeMetric"></MetricsCards>
    <el-radio-group v-model="statDim" class="chart-button-group" @change="changeStatDim">
      <el-radio-button label="day">日</el-radio-button>
      <el-radio-button label="week" :disabled="!props.fetchLineWeek">周</el-radio-button>
      <el-radio-button label="month" :disabled="!props.fetchLineWeek">月</el-radio-button>
    </el-radio-group>
    <div style="height: 350px" ref="chartRef"></div>
  </div>
</template>

<script lang="ts" setup>
import * as echarts from 'echarts';
import { computed, onBeforeUnmount, onMounted, Ref, ref, watch } from 'vue';
import { productListMetricsEnum } from '/@/views/productCenter/productList/utils/enum';
import XEUtils from 'xe-utils';
import MetricsCards from '../MetricsCards/index.vue';
import emitter from '/@/utils/emitter';
import { parseQueryParams } from '/@/views/adManage/utils/tools.js';

defineOptions({
  name: 'DataTendencyChart',
});

interface Props {
  fetchCard: Function;
  fetchLine: Function;
  fetchLineMonth?: Function;
  fetchLineWeek?: Function;
  query: { [key: string]: any };
  initMetric?: ShowMetric[];
  metricEnum?: { [key: string]: string }[];
}

const props = withDefaults(defineProps<Props>(), {
  initMetric: () => [
    { metric: 'Impression', color: '#0085ff', label: '曝光量' },
    { metric: 'Click', color: '#3fd4cf', label: '点击量' },
    { metric: 'Spend', color: '#ff9500', label: '花费' },
  ],
  metricEnum: () => productListMetricsEnum,
});

const metrics = ref(props.initMetric);
const metricColors = {
  Impression: '#0085ff',
  Click: '#3fd4cf',
  Spend: '#ff9500',
  // ... 其他指标的颜色
};
const metricsItems: Ref<MetricData[]> = ref([]);
let chartObj: any;
const chartRef = ref();
const statDim = ref('day');
const option: any = {
  dataset: {
    source: [],
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      label: {
        backgroundColor: '#6a7985',
      },
    },
  },
  legend: {
    selected: {}, // 控制显隐
    show: false,
  },
  grid: {
    top: 50,
    right: 150,
    bottom: 30,
    left: 65,
  },
  xAxis: {
    type: 'category',
  },
  yAxis: [
    {
      id: 0,
      type: 'value',
      name: '',
      splitLine: {
        show: true, // 设置显示分割线
      },
      axisLine: {
        show: true,
        lineStyle: { color: '' },
      },
      show: true,
    },
    {
      id: 1,
      type: 'value',
      name: '',
      position: 'right',
      splitLine: {
        show: false,
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: '',
        },
      },
      show: true,
    },
    {
      id: 2,
      type: 'value',
      position: 'right',
      offset: 90,
      name: '',
      splitLine: {
        show: false,
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: '',
        },
      },
      show: true,
    },
    {
      id: 3,
      type: 'value',
      position: 'right',
      offset: 90,
      name: '',
      splitLine: {
        show: false,
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: '',
        },
      },
      show: true,
    },
  ],
  series: [
    {
      id: 0,
      name: '',
      type: 'bar',
      encode: {
        x: 'Name',
        y: '',
      },
      barWidth: '16px',
      yAxisIndex: 0,
      itemStyle: {
        color: '',
        borderRadius: 4,
      },
    },
    {
      id: 1,
      name: '',
      type: 'line',
      encode: {
        x: 'Name',
        y: '',
      },
      symbolSize: 6,
      symbol: 'circle',
      smooth: true,
      yAxisIndex: 1,
      itemStyle: {
        // color: '#ff9500',
        // borderColor: '#ff9500'
      },
      areaStyle: {
        // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
        //   { offset: 0, color: '#3fd4cf53' },
        //   { offset: 1, color: '#3fd4cf03' },
        // ]),
      },
      emphasis: {
        focus: 'series',
      },
    },
    {
      id: 2,
      name: '',
      type: 'line',
      encode: {
        x: 'Name',
        y: '',
      },
      symbolSize: 6,
      symbol: 'circle',
      smooth: true,
      yAxisIndex: 2,
      itemStyle: {},
      areaStyle: {},
      emphasis: {
        focus: 'series',
      },
    },
    {
      id: 3,
      name: 'NewMetric',
      type: 'line',
      encode: {
        x: 'Name',
        y: metrics.value[1].metric, // TODO: 数据源后续需要修改
      },
      symbolSize: 6,
      symbol: 'circle',
      smooth: true,
      yAxisIndex: 0, // 假设在默认的y轴上
      itemStyle: {
        color: '#1e1e2d',
        borderColor: '#1e1e2d',
      },
      areaStyle: {},
      emphasis: {
        focus: 'series',
      },
    },
  ],
};
const loading = ref(true);
const queryParams = computed(() => parseQueryParams(props.query));

onMounted(() => {
  getMetricsItems();
  addResize();
  // initLine()
  setTimeout(() => {
    initLine();
  }, 0);
});

onBeforeUnmount(() => {
  if (chartObj) {
    chartObj.dispose();
    chartObj = null;
  }
  removeResize();
});

watch(props.query, async () => {
  // console.log("------watch-----queryParams", props.query)
  emitter.emit('DateTendency-changeStatDim'); // 触发DataTable的loading
  loading.value = true;
  await getMetricsItems();
  const items = await getDataset();
  emit('changeStatDim', items); // 向父组件传递数据后再传递给DataTable
  const opt = { dataset: { source: items } };
  chartObj.setOption(opt);
  loading.value = false;
});

// const initLine = async () => {
//   chartObj = echarts.init(chartRef.value)
//   const items = await getDataset()
//   option.dataset.source = items
//
//   XEUtils.arrayEach(option.series, (info: any, index) => {
//     const color = metrics.value[index].color
//     info.name = metrics.value[index].label
//     info.encode.y = metrics.value[index]?.metric
//     if (info.type === 'bar') {
//       info.itemStyle.color = color
//     } else {
//       info.itemStyle = { color: color, borderColor: color }
//       info.areaStyle = {
//         color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
//           { offset: 0, color: color + '53' },
//           { offset: 1, color: color + '03' },
//         ]),
//       }
//     }
//   })
//   XEUtils.arrayEach(option.yAxis, (info: any, index) => {
//     info.name = metrics.value[index].label
//     info.axisLine.lineStyle.color = metrics.value[index].color
//   })
//
//   XEUtils.arrayEach(props.metricEnum, (info) => {
//     option.legend.selected[info.label] = false
//   })
//   for (const info of metrics.value) {
//     option.legend.selected[info.label] = true
//   }
//   // console.log(option)
//   chartObj.setOption(option)
//   loading.value = false
// }

async function initLine() {
  chartObj = echarts.init(chartRef.value);
  option.dataset.source = await getDataset();

  // 获取前三个 metrics
  const limitedMetrics = metrics.value.slice(0, 3);

  // 初始化系列和 y 轴
  const newSeries = limitedMetrics.map((metric, index) => ({
    id: index,
    name: metric.label,
    type: index === 0 ? 'bar' : 'line',
    encode: { x: 'Name', y: metric.metric },
    yAxisIndex: index,
    itemStyle: { color: index === 0 ? '#0085ff' : metric.color, borderRadius: [6, 6, 6, 6] },
    lineStyle: { color: metric.color },
    barMaxWidth: '16px',
    areaStyle:
      index !== 0
        ? {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: metric.color + '53' },
              { offset: 1, color: metric.color + '03' },
            ]),
          }
        : undefined,
  }));

  // 将新的 series 添加到现有的 option.series 中
  option.series = [...newSeries, ...option.series.slice(3)];

  option.yAxis = limitedMetrics.map((metric, index) => ({
    id: index,
    name: metric.label,
    type: 'value',
    position: index === 0 ? 'left' : 'right',
    offset: index > 1 ? (index - 1) * 80 : 0,
    axisLine: { show: true, lineStyle: { color: metric.color } },
    splitLine: {
      show: index === 0,
    },
    show: true,
  }));

  // 初始化图例
  option.legend.selected = props.metricEnum.reduce((acc: any, metric) => {
    acc[metric.label] = limitedMetrics.some((m) => m.metric === metric.value);
    return acc;
  }, {});

  chartObj.setOption(option);
  loading.value = false;
}

// TODO: 新增的线条暂未处理
function changeMetric() {
  const limitedMetrics = metrics.value.slice(0, 3);
  // 更新图例选中状态
  option.legend.selected = props.metricEnum.reduce((acc: any, metric) => {
    acc[metric.label] = metrics.value.some((m) => m.metric === metric.value);
    return acc;
  }, {});

  // 重新创建 series 数组，只包含选中的指标
  option.series = limitedMetrics.map((metric, index) => {
    const baseConfig: any = {
      id: index,
      name: metric.label,
      type: metric.color === '#0085ff' ? 'bar' : 'line',
      encode: { x: 'Name', y: metric.metric },
      yAxisIndex: index,
      itemStyle: { color: metric.color, borderRadius: [6, 6, 6, 6] },
      lineStyle: { color: metric.color },
      barMaxWidth: '16px',
    };

    if (baseConfig.type != 'bar') {
      baseConfig.areaStyle = {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: metric.color + '53' },
          { offset: 1, color: metric.color + '03' },
        ]),
      };
    }

    return baseConfig;
  });

  // 更新 y 轴显示状态
  option.yAxis = limitedMetrics.map((metric, index) => ({
    id: index,
    name: metric.label,
    type: 'value',
    position: index === 0 ? 'left' : 'right',
    offset: index > 1 ? (index - 1) * 80 : 0,
    axisLine: {
      show: true,
      lineStyle: { color: metric.color },
    },
    splitLine: {
      show: index === 0,
    },
    show: true,
  }));

  // 确保只有必要的 y 轴
  while (option.yAxis.length > limitedMetrics.length) {
    option.yAxis.pop();
  }

  chartObj.setOption(option, true); // 使用 true 作为第二个参数，强制完全刷新
}

async function getDataset() {
  if (statDim.value === 'week') {
    if (props.fetchLineWeek) {
      const resp = await props.fetchLineWeek(queryParams.value);
      return resp.data;
    }
  } else if (statDim.value === 'month') {
    if (props.fetchLineMonth) {
      const resp = await props.fetchLineMonth(queryParams.value);
      return resp.data;
    }
  } else {
    const resp = await props.fetchLine(queryParams.value);
    return resp.data;
  }
}

async function getMetricsItems() {
  const resp = await props.fetchCard(queryParams.value);
  const data = resp.data;
  metricsItems.value.length = 0;
  XEUtils.arrayEach(props.metricEnum, (info) => {
    const tmp: MetricData = {
      label: info.label,
      value: info.value,
      metricVal: data[info.value],
      gapVal: data[`gap${info.value}`],
      preVal: data[`prev${info.value}`],
    };
    metricsItems.value.push(tmp);
  });
}

const emit = defineEmits(['changeStatDim']);

async function changeStatDim() {
  emitter.emit('DateTendency-changeStatDim'); // 触发DataTable的loading
  loading.value = true;
  let source = await getDataset();
  if (source.length > 0) {
    chartObj.setOption({ dataset: { source: source } });
  }
  loading.value = false;
  emit('changeStatDim', source); // 向父组件传递数据后再传递给DataTable
}

function resizeChart() {
  chartObj.resize();
}

function addResize() {
  window.addEventListener('resize', resizeChart);
}

function removeResize() {
  window.removeEventListener('resize', resizeChart);
}
</script>

<style scoped>
.metrics-cards {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  width: 100%;
}

.chart-button-group {
  display: flex;
  justify-content: flex-end;
  margin-top: 5px;
}
</style>
