import { Ref, defineComponent, reactive, ref } from 'vue';
import styles from '../index.module.less';
import { NNumberAnimation } from 'naive-ui';
import numeral from 'numeral';
import { useECharts } from '@/hooks/web/useECharts';
export default defineComponent({
  name: 'home-trainData',
  setup() {
    const chartRef = ref<HTMLDivElement | null>(null);
    const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
    const qualifiedFlag = ref(true);
    const unqualifiedFlag = ref(true);
    const payForm = reactive({
      height: '360px',
      width: '100%',
      studentNum: 0,
      paymentAmount: 0,
      dateList: [
        '2022-10-10',
        '2022-10-11',
        '2022-10-12',
        '2022-10-13',
        '2022-10-14',
        '2022-10-15',
        '2022-10-16'
      ],
      studentList: [22, 23, 25, 26, 27, 6, 7],
      payInfoList: [100, 200, 300, 450, 330, 200, 10]
    });

    const setChart = () => {
      setOptions({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              width: 2,
              color: '#A9C7FF'
            }
          }
        },
        legend: {
          show: false,
          selected: {
            //在这里设置默认展示就ok了
            合格人数: qualifiedFlag.value,
            不合格人数: unqualifiedFlag.value
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          axisLabel: {
            show: true,
            interval: 0
          },
          data: payForm.dateList
          // splitLine: {
          //   show: true,
          //   lineStyle: {
          //     width: 2,
          //     type: 'solid',
          //     color: 'rgba(226,226,226,0.5)'
          //   }
          // }
          // axisTick: {
          //   show: false
          // }
        },
        yAxis: [
          {
            type: 'value',
            axisTick: {
              show: false
            },
            splitArea: {
              show: false,
              areaStyle: {
                color: ['rgba(255,255,255,0.2)']
              }
            }
          }
        ],
        grid: {
          left: '1%',
          right: '1%',
          top: '2  %',
          bottom: 0,
          containLabel: true
        },
        series: [
          {
            data: payForm.payInfoList,
            type: 'line',
            name: '合格人数',
            symbolSize: 10,
            symbol: 'circle',
            smooth: true,
            itemStyle: {
              color: '#198CFE',
              borderColor: '#fff',
              borderWidth: 3
            },
            lineStyle: {
              width: 2 //设置线条粗细
            },
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(212, 231, 255, 1)'
                    // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: 'rgba(221, 235, 254, 0)' // 100% 处的颜色
                  }
                ]
              }
            },
            emphasis: {
              disabled: true
            }
          },
          {
            // smooth: true,
            data: payForm.studentList,
            symbolSize: 10,
            type: 'line',
            name: '不合格人数',
            symbol: 'circle',
            smooth: true,
            itemStyle: {
              color: '#FF7AA7',
              borderColor: '#fff',
              borderWidth: 3
            },
            lineStyle: {
              width: 3 //设置线条粗细
            },
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(255, 243, 246, 1)'
                    // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    // 100% 处的颜色
                    color: 'rgba(255, 246, 248, 0)'
                  }
                ]
              }
            },
            emphasis: {
              disabled: true
            }
          }
        ],

        formatter: (item: any) => {
          if (Array.isArray(item)) {
            return [
              item[0].axisValueLabel,
              ...item.map(
                (d: any) =>
                  `<br/>${
                    d.marker
                  }<span style="margin-top:10px;margin-left:5px;font-size: 13px;font-weight: 500;
                  color: #333333;
                  line-height: 18px;">${d.seriesName}: ${
                    d.value
                  }${'人'} </span>`
              )
            ].join('');
          } else {
            return item;
          }
        }
        // dataZoom: [
        //   {
        //     type: 'slider',
        //     start: 5,
        //     end: 100,
        //     filterMode: 'empty'
        //   }
        // ]
      });
    };
    setChart();
    return () => (
      <>
        <div class={styles.homeTrainData}>
          <div class={styles.TrainDataTop}>
            <div class={styles.TrainDataTopLeft}>
              <div class={styles.TrainDataItem}>
                <p class={styles.TrainDataItemTitle}>
                  <span>
                    <NNumberAnimation from={0} to={6}></NNumberAnimation>
                  </span>
                  次
                </p>
                <p class={styles.TrainDataItemsubTitle}>训练次数</p>
              </div>
              <div class={styles.TrainDataItem}>
                <p class={styles.TrainDataItemTitle}>
                  <span>
                    <NNumberAnimation from={0} to={100}></NNumberAnimation>
                  </span>
                  人次
                </p>
                <p class={styles.TrainDataItemsubTitle}>应交总人次</p>
              </div>
              <div class={styles.TrainDataItem}>
                <p class={styles.TrainDataItemTitle}>
                  <span>
                    <NNumberAnimation from={0} to={40}></NNumberAnimation>
                  </span>
                  人次
                </p>
                <p class={styles.TrainDataItemsubTitle}>提交总人次</p>
              </div>
              <div class={styles.TrainDataItem}>
                <p class={styles.TrainDataItemTitle}>
                  <span>
                    {' '}
                    <NNumberAnimation from={0} to={30}></NNumberAnimation>
                  </span>
                  人次
                </p>
                <p class={styles.TrainDataItemsubTitle}>合格总人次</p>
              </div>
              <div class={styles.TrainDataItem}>
                <p class={styles.TrainDataItemTitle}>
                  <span>
                    <NNumberAnimation from={0} to={40}></NNumberAnimation>%
                  </span>
                </p>
                <p class={styles.TrainDataItemsubTitle}>训练提交率</p>
              </div>
              <div class={styles.TrainDataItem}>
                <p class={styles.TrainDataItemTitle}>
                  <span>
                    <NNumberAnimation from={0} to={30}></NNumberAnimation>%
                  </span>
                </p>
                <p class={styles.TrainDataItemsubTitle}>训练合格率</p>
              </div>
            </div>
            <div class={styles.TrainDataTopRight}>
              <div
                onClick={() => {
                  qualifiedFlag.value = !qualifiedFlag.value;
                  setChart();
                }}
                class={[
                  styles.DataTopRightItem,
                  qualifiedFlag.value ? '' : styles.DataTopRightItemDis
                ]}>
                <div class={styles.DataTopRightDot}></div>
                <p>合格人数</p>
              </div>
              <div
                onClick={() => {
                  unqualifiedFlag.value = !unqualifiedFlag.value;
                  setChart();
                }}
                class={[
                  styles.DataTopRightItem,
                  unqualifiedFlag.value ? '' : styles.DataTopRightItemDis
                ]}>
                <div class={[styles.DataTopRightDot, styles.red]}></div>
                <p>不合格人数</p>
              </div>
            </div>
          </div>
          <div class={styles.chatrs}>
            <div
              ref={chartRef}
              style={{ height: payForm.height, width: payForm.width }}></div>
          </div>
        </div>
      </>
    );
  }
});
