import { Ref, defineComponent, reactive, ref } from 'vue';
import styles from '../index.module.less';
import { NButton, NDataTable, NNumberAnimation } from 'naive-ui';
import numeral from 'numeral';
import { useECharts } from '@/hooks/web/useECharts';
import Pagination from '/src/components/pagination';
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: [50, 100, 80, 180, 70, 99, 300],
      payInfoList: [100, 200, 300, 450, 330, 200, 10]
    });
    const state = reactive({
      loading: false,
      pagination: {
        page: 1,
        rows: 10,
        pageTotal: 4
      },
      tableList: [
        {
          teacherName: '孙忆枫',
          createTime: '2023-06-27',
          endTime: '2023-06-30',
          status: 'ing',
          studentNum: 100,
          submitNum: 100,
          quantityNum: 60,
          submitRate: 100,
          quantityRate: 60
        },
        {
          teacherName: '孙忆枫',
          createTime: '2023-06-27',
          endTime: '2023-06-30',
          status: 'ing',
          studentNum: 100,
          submitNum: 100,
          quantityNum: 60,
          submitRate: 100,
          quantityRate: 60
        },
        {
          teacherName: '孙忆枫',
          createTime: '2023-06-27',
          endTime: '2023-06-30',
          status: 'ing',
          studentNum: 100,
          submitNum: 100,
          quantityNum: 60,
          submitRate: 100,
          quantityRate: 60
        },
        {
          teacherName: '孙忆枫',
          createTime: '2023-06-25',
          endTime: '2023-06-26',
          status: 'end',
          studentNum: 100,
          submitNum: 100,
          quantityNum: 60,
          submitRate: 100,
          quantityRate: 60
        }
      ] as any,
      goCourseVisiable: false
    });
    const columns = () => {
      return [
        {
          title: '布置老师',
          key: 'teacherName'
        },
        {
          title: '布置时间',
          key: 'createTime'
        },
        {
          title: '截止时间',
          key: 'endTime'
        },
        {
          title: '训练状态',
          key: 'status',
          render(row: any) {
            return row.status == 'ing' ? (
              <div class={styles.indDot}>
                {' '}
                <span></span> 进行中
              </div>
            ) : (
              <div class={styles.endDot}>
                <span></span>已结束
              </div>
            );
          }
        },
        {
          title: '布置人数',
          key: 'studentNum'
        },
        {
          title: '提交人数',
          key: 'submitNum'
        },
        {
          title: '合格人数',
          key: 'quantityNum'
        },
        {
          title: '提交率',
          key: 'submitRate',
          render(row: any) {
            return <span>{row.submitRate}%</span>;
          }
        },
        {
          title: '合格率',
          key: 'quantityRate',
          render(row: any) {
            return <span>{row.quantityRate}%</span>;
          }
        },
        {
          title: '操作',
          key: 'id',
          render(row: any) {
            return (
              <NButton text type="primary">
                详情
              </NButton>
            );
          }
        }
      ];
    };
    const getList = () => {
      console.log('1');
    };
    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: true,
          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',
            axisLabel: {
              formatter: '{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: [
          {
            // 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
            }
          },
          {
            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
            }
          }
        ],

        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 class={styles.tableWrap}>
            <NDataTable
              class={styles.classTable}
              loading={state.loading}
              columns={columns()}
              data={state.tableList}></NDataTable>
            <Pagination
              v-model:page={state.pagination.page}
              v-model:pageSize={state.pagination.rows}
              v-model:pageTotal={state.pagination.pageTotal}
              onList={getList}
              sync
              saveKey="orchestraRegistration-key"
            />
          </div>
        </div>
      </>
    );
  }
});
