<template>
  <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
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="actionRef"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
      :pagination="false"
      :resizeHeightOffset="-1000"
    />
  </n-card>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { GradeDistribution } from '@/api/echarts/GradeDistribution';
  import { columns } from './columns';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { FormSchema, useForm } from '@/components/Form';
  import {defRangeShortcuts, timestampToDate} from "@/utils/dateUtil";
  const useUserStore = useUserStoreWidthOut();

  // 不支持自然玩家渠道
  const channelOption = ref([]);
  for (let i = 0; i < useUserStore.config.channel_option.length; i++) {
    if (
      useUserStore.config.channel_option[i].value !== '0' &&
      useUserStore.config.channel_option[i].value !== '1'
    ) {
      channelOption.value.push(useUserStore.config.channel_option[i]);
    }
  }

  const actionRef = ref();

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

  const schemas: FormSchema[] = [
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        defaultValue: params.value.server_id,
        options: useUserStore.config.server_simple_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 * 90, new Date()],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e[0]);
          params.value.endDay = timestampToDate(e[1]);
        },
      },
    },
  ];

  const formParams = reactive({
    name: '',
    address: '',
    date: null,
    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) => {
    return await GradeDistribution({ ...formParams, ...params.value, ...res });
  };

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

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

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

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