<template>
  <div>
    <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"
      />
    </n-card>
    <FirstDay />
    <NextDay />
    <SevenDay />
    <Through />
    <Die />
    <Reconnect />
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { chapterSatisfy } from '@/api/echarts/chapterSatisfy';
  import FirstDay from './components/FirstDay.vue';
  import NextDay from './components/NextDay.vue';
  import SevenDay from './components/SevenDay.vue';
  import Through from './components/Through.vue';
  import Die from './components/Die.vue';
  import Reconnect from './components/Reconnect.vue';
  import { defaultStatisticsTimeOptions, timestampToDate } from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';

  const useUserStore = useUserStoreWidthOut();
  const columns = [
    {
      title: '满足条件',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '人数',
      dataIndex: 'val',
      key: 'val',
    },
  ];

  const params = ref<any>({
    channel_id: '',
    pageSize: 10,
    day: timestampToDate(defaultStatisticsTimeOptions()),
    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: 'makeDate',
      component: 'NDatePicker',
      label: '选择日期',
      defaultValue: defaultStatisticsTimeOptions(),
      componentProps: {
        type: 'date',
        clearable: true,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e);
        },
      },
    },
  ];

  const actionRef = ref();
  const formParams = reactive({
    day: '',
    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 chapterSatisfy({ ...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 = {
      channel_id: '',
      pageSize: 10,
      day: timestampToDate(defaultStatisticsTimeOptions()),
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
