<template>
  <div>
    <n-card
      title="查询金榜题名数据"
      :content-style="{ padding: '10px' }"
      :header-style="{ padding: '10px' }"
    >
      <Chart :loading="loading" :options="options3" style="width: 100%; min-height: 600px" />
    </n-card>

    <n-card :bordered="false" class="proCard" title="金榜题名每日 / 人均数据">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>

      <BasicTable
        :loading="loading"
        :columns="columns"
        :dataSource="dataSource"
        :row-key="(row) => row.id"
        ref="actionRef"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
        :resizeHeightOffset="-2000"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { IdiomInfo } from '@/api/echarts/ExpeditionInfo';
  import Chart from '@/views/echarts/adv/components/Chart.vue';
  import {defRangeShortcuts, timestampToDate} from '@/utils/dateUtil';
  import { reverse } from '@/utils/array';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  const useUserStore = useUserStoreWidthOut();
  let options3 = ref({});

  const columns = [
    {
      title: '日期',
      key: 'days',
      width: 120,
    },
    {
      title: '参与人数',
      key: 'players',
      width: 120,
    },
    {
      title: '闯关次数 / 人均',
      render(row) {
        return row.breakCount + ' / ' + row.avgBreakCount;
      },
      width: 150,
    },
    {
      title: '使用锦囊次数 / 人均',
      render(row) {
        return row.tipsCount + ' / ' + row.avgTipsCount;
      },
      width: 180,
    },
    {
      title: '墨水获取(广告)次数 / 人均',
      render(row) {
        return row.inkAdvCount + ' / ' + row.avgInkAdvCount;
      },
      width: 200,
    },
    {
      title: '墨水获取(钻石)次数 / 人均',
      render(row) {
        return row.inkDrillCount + ' / ' + row.avgInkDrillCount;
      },
      width: 200,
    },
    {
      title: '领取每日奖励次数 / 人均',
      render(row) {
        return row.receiveCount + ' / ' + row.avgReceiveCount;
      },
      width: 200,
    },
    {
      title: '查看排行榜次数 / 人均',
      render(row) {
        return row.rankingCount + ' / ' + row.avgRankingCount;
      },
      width: 200,
    },
    {
      title: '查看排行榜奖励次数 / 人均',
      render(row) {
        return row.awardCount + ' / ' + row.avgAwardCount;
      },
      width: 200,
    },
    {
      title: '每日奖励各个档位领取次数',
      render(row) {
        return row.gearData;
      },
      width: 200,
    },
  ];

  const dataSource = ref([]);

  const params = ref<any>({
    pageSize: 10,
    day: timestampToDate(new Date() - 86400000 * 30),
    endDay: timestampToDate(new Date()),
    channel_id: '',
    server_id: null,
  });

  const schemas: FormSchema[] = [
    {
      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: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      componentProps: {
        type: 'daterange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        defaultValue: [new Date() - 86400000 * 30, new Date()],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e[0]);
          params.value.endDay = timestampToDate(e[1]);
        },
      },
    },
  ];

  const loading = ref(false);
  const message = useMessage();
  const actionRef = ref();
  const formParams = reactive({
    day: '',
    endDay: '',
    channel_id: '',
  });

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

  const loadDataTable = async (res) => {
    loading.value = true;
    new Promise((_resolve, _reject) => {
      IdiomInfo({ ...formParams, ...params.value, ...res })
        .then((res) => {
          dataSource.value = res.rows;
          draw(res.info);
          loading.value = false;
        })
        .catch((error) => {
          message.error(error.toString());
          loading.value = false;
        });
    });
  };
  onMounted(async () => {
    loadDataTable({});
  });

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

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

  function draw(values) {
    if (values == undefined) {
      values = {};
    }

    options3.value = {
      tooltip: {
        trigger: 'axis',
      },
      legend: {
        data: [
          '参与玩家',
          '闯关次数',
          '使用锦囊次数',
          '墨水获取(广告)次数',
          '墨水获取(钻石)次数',
          '领取每日奖励次数',
          '查看排行榜次数',
          '查看排行榜奖励次数',
        ],
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      toolbox: {
        feature: {
          saveAsImage: {},
        },
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: reverse(values.days),
      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '参与玩家',
          type: 'line',
          data: reverse(values.players),
        },
        {
          name: '闯关次数',
          type: 'line',
          data: reverse(values.breakCount),
        },
        {
          name: '使用锦囊次数',
          type: 'line',
          data: reverse(values.tipsCount),
        },
        {
          name: '墨水获取(广告)次数',
          type: 'line',
          data: reverse(values.inkAdvCount),
        },
        {
          name: '墨水获取(钻石)次数',
          type: 'line',
          data: reverse(values.inkDrillCount),
        },
        {
          name: '领取每日奖励次数',
          type: 'line',
          data: reverse(values.receiveCount),
        },
        {
          name: '查看排行榜次数',
          type: 'line',
          data: reverse(values.rankingCount),
        },
        {
          name: '查看排行榜奖励次数',
          type: 'line',
          data: reverse(values.awardCount),
        },
      ],
    };
  }

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

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = {
      pageSize: 10,
      day: timestampToDate(new Date() - 86400000 * 30),
      endDay: timestampToDate(new Date()),
      channel_id: '',
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
