<template>
  <div>
    <n-card :content-style="{ padding: '10px' }" :header-style="{ padding: '10px' }">
      <div class="n-layout-page-header">
        <n-card :bordered="false" title="每天每个小段位人数分布">
          X轴是段位；Y轴是对应段位的人数。
        </n-card>
      </div>
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>

      <Chart :loading="loading" :options="options3" style="width: 100%; min-height: 600px" />
    </n-card>
  </div>
</template>

<script setup lang="ts">
  import Chart from '@/views/echarts/adv/components/Chart.vue';
  import { BasicForm, FormSchema, useForm } from '@/components/Form';
  import {
    defaultStatisticsTimeOptions,
    defRangeShortcuts,
    timestampToDate,
  } from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import {
    GrandmasterDan,
    GrandmasterSeconds,
    GrandmasterWaves,
    TreasurePlayerLevel,
  } from '@/api/echarts/Duel';
  import { onMounted, ref } from 'vue';
  import { useMessage } from 'naive-ui';

  let options3 = ref({});
  const useUserStore = useUserStoreWidthOut();
  const loading = ref(false);
  const message = useMessage();
  const params = ref<any>({
    date: defaultStatisticsTimeOptions(),
    pageSize: 10,
    startSeconds: 0,
    endSeconds: 60,
    channel_id: '',
    server_id: null,
  });
  const schemas: FormSchema[] = [
    {
      field: 'date',
      component: 'NDatePicker',
      label: '选择日期',
      defaultValue: defaultStatisticsTimeOptions(),
      componentProps: {
        type: 'date',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.date = e; //timestampToDate(e);
        },
      },
    },
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        defaultValue: params.value.server_id,
        options: useUserStore.config.server_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.server_id = e;
        },
      },
    },
    // {
    //   field: 'channel_id',
    //   component: 'NTreeSelect',
    //   label: '推广渠道',
    //   componentProps: {
    //     placeholder: '请选择推广渠道',
    //     clearFilterAfterSelect: true,
    //     clearable: true,
    //     filterable: true,
    //     defaultExpandAll: true,
    //     options: useUserStore.config.channel_tree_option,
    //     onUpdateValue: (e: any) => {
    //       console.log(e);
    //       params.value.channel_id = e;
    //     },
    //   },
    // },
    // {
    //   field: 'waves',
    //   component: 'NInput',
    //   label: '战斗时长(秒)',
    //   componentProps: {
    //     pair: true,
    //     separator: '~',
    //     clearable: true,
    //     placeholder: ['从', '到'],
    //     defaultValue: [params.value.startSeconds, params.value.endSeconds],
    //     onInput: (e: any) => {
    //       console.log(e);
    //       params.value.startSeconds = parseInt(e[0]);
    //       params.value.endSeconds = parseInt(e[1]);
    //     },
    //   },
    // },
  ];

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 100,
    schemas,
  });

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = {
      pageSize: 10,
      date: defaultStatisticsTimeOptions(),
      channel_id: '',
      server_id: null,
      startSeconds: 0,
      endSeconds: 60,
    };
    reloadTable();
  }

  function reloadTable() {
    loadDataTable({});
  }

  function draw(player_data) {
    options3.value = {
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      tooltip: {
        trigger: 'axis',
      },
      toolbox: {
        feature: {
          saveAsImage: {},
        },
      },
      xAxis: {
        type: 'category',
        data: player_data.map(function (item) {
          return item.danName;
        }),
      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '人数',
          type: 'bar',
          data: player_data.map(function (item) {
            return item.total;
          }),
          itemStyle: {
            normal: {
              label: {
                show: true, //开启显示数值
                position: 'top', //数值在上方显示
                textStyle: {
                  //数值样式
                  color: '#D1DBFF', //字体颜色
                  fontSize: 12, //字体大小
                },
              },
            },
          },
          colorBy: 'data', //每个颜色根据data不一样
          rippleEffect: {
            brushType: 'stroke', //波纹的绘制方式
          },
          markPoint: {
            data: [
              { type: 'max', name: '最大值' }, // 标记出最大值
              { type: 'min', name: '最小值' }, // 标记出最小值
            ],
          },
        },
      ],
    };
  }

  const loadDataTable = async (res) => {
    loading.value = true;
    new Promise((_resolve, _reject) => {
      GrandmasterDan({ ...params.value, ...res })
        .then((res) => {
          console.log(JSON.stringify(res));
          draw(res);
          loading.value = false;
        })
        .catch((error) => {
          message.error(error.toString());
          loading.value = false;
        });
    });
  };

  onMounted(async () => {
    await loadDataTable({});
  });
</script>

<style scoped lang="less"></style>
