<!--
 * @Author: walker.liu
 * @Date: 2022-06-06 17:06:25
 * @Copyright（C）:  2019-2020 ZP Inc.   All rights reserved.
-->
<template>
  <chart-box
    title="Discord"
    name="discord-box"
    :r24h="r24h"
    :token="token"
    :tb="tb"
    :currentPath="currentPath"
  >
    <template #chartSwitch>
      <radio-group type="analysisDate" v-model:current="date" @change="getData">
      </radio-group>
    </template>
    <template #header>
      <div class="onefield">
        <span class="label">{{ $t('analysis.CurrentTotal') }}</span>
        <span class="value">{{ formData.value1 ?? '---' }}</span>
      </div>
      <div class="twofield">
        <span class="label">{{ $t('analysis.Currentpeopleonline') }}</span>
        <span class="value">{{ formData.value2 ?? '---' }}</span>
      </div>
    </template>
    <div class="chart-box" id="chart-box-8"></div>
  </chart-box>
</template>
<script setup lang="tsx">
import echarts from '@/utils/echarts/echarts';
import ChartBox from './ChartBox.vue';
import request from '@/utils/request';
import RadioGroup from '@/components/RadioGroup.vue';
import { useI18n } from 'vue-i18n';
import useEchartNum from '@/hooks/useEchartNum';
const { maxNum, getMaxNum, getMinNum } = useEchartNum();
const { t } = useI18n();
const props = defineProps({
  title: String,
  options: Array,
  token: String,
  tb: String,
  currentPath: String,
  r24h: String,
});
const route = useRoute();
// 初始化折线图
const WatchEcharts = useWatchEcharts();
watch(
  () => WatchEcharts.value,
  (v) => {
    initChart();
    getData();
  }
);
// 当前链
const chain = useChain();
const date = ref('5m');
//
const formData = reactive({
  value1: '',
  value2: '',
});
let myChart: any = null;
watch(
  () => props.token,
  (v) => {
    // 当处于右侧折线图页面时,点击行，切换重新加载折线图
    initChart();
    getData();
  }
);
onMounted(() => {
  nextTick(() => {
    if (route.name !== 'tokenAnalysis') {
      initChart();
      getData();
    }
  });
});
const initChart = () => {
  if (myChart) {
    myChart.dispose();
  }
  const chartDom = document.getElementById('chart-box-8') as HTMLCanvasElement;
  chartDom.style.width = chartDom.offsetWidth + 'px';
  chartDom.style.height = '212px';
  myChart = echarts.init(chartDom, 'vintage');
};
const getData = () => {
  myChart &&
    myChart.showLoading({
      textColor: 'rgb(150,150,150)', //文字颜色
      maskColor: 'rgba(255,255,255,0)',
    });
  request
    .get(`/v1/${chain.value}/analysis/getDiscordData`, {
      params: {
        tag: props.token,
        date: date.value,
      },
    })
    .then((res: any) => {
      myChart && myChart.hideLoading();
      formData.value1 = res?.count?.din;
      formData.value2 = res?.count?.diol;
      // 先计算出双轴各自的最大值
      getMinNum(res.list.din, 'oneMin');
      getMaxNum(res.list.din, 'oneMax');
      getMinNum(res.list.diol, 'twoMin');
      getMaxNum(res.list.diol, 'twoMax');
      renderChart(res.list);
    });
};
const renderChart = (list) => {
  let options = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999',
        },
      },
    },
    legend: {
      data: [t('analysis.Totaladditions'), t('analysis.peopleaddedonline')],
      // show: true,
      // top: '20%',
    },
    grid: {
      bottom: 30,
      left: '50px',
      right: '50px',
      top: 80,
    },
    xAxis: [
      {
        type: 'category',
        data: list.date,
        axisLine: {
          onZero: false,
        },
        axisPointer: {
          type: 'shadow',
        },
      },
    ],
    yAxis: [
      {
        min: maxNum.oneMin,
        max: maxNum.oneMax,
        splitNumber: 5,
        interval: (maxNum.oneMax - maxNum.oneMin) / 5,
        type: 'value',
        name: t('analysis.Totaladditions'),
        axisLabel: {
          formatter: '{value}',
        },
      },
      {
        min: maxNum.twoMin,
        max: maxNum.twoMax,
        splitNumber: 5,
        interval: (maxNum.twoMax - maxNum.twoMin) / 5,
        type: 'value',
        name: t('analysis.peopleaddedonline'),
        axisLabel: {
          formatter: '{value}',
        },
      },
    ],
    series: [
      {
        name: t('analysis.Totaladditions'),
        type: 'line',
        data: list.din,
      },
      {
        name: t('analysis.peopleaddedonline'),
        type: 'line',
        data: list.diol,
        // 右侧y轴
        yAxisIndex: 1,
      },
    ],
  };
  document.querySelector('#chart-box-8').removeAttribute('_echarts_instance_');
  myChart.setOption(options);
};
</script>
<style lang="less" scoped>
.chart-box {
  height: 212px;
}
</style>
