<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"
        :resizeHeightOffset="-3000"
      />
    </n-card>
  </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 { SevenAward } from '@/api/echarts/seven';
  import {defRangeShortcuts, timestampToDate} from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  const useUserStore = useUserStoreWidthOut();

  const columns = [
    {
      title: '奖励ID',
      dataIndex: 'taskId',
      key: 'taskId',
    },
    {
      title: '奖励完成总人数 / 起始天完成',
      dataIndex: 'sucCount',
      key: 'sucCount',
      render: (rows, _) => {
        return rows.sucCount + ' / ' + rows.firstSucCount;
      },
    },
    {
      title: '完成此奖励的平均天数',
      dataIndex: 'advSucDays',
      key: 'advSucDays',
      render: (rows, _) => {
        return rows.advSucDays.toFixed(2);
      },
    },
  ];

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

  const actionRef = ref();
  const formParams = reactive({
    day: '',
    endDay: '',
  });

  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 SevenAward({ ...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,
      day: timestampToDate(new Date() - 86400000),
      endDay: timestampToDate(new Date() - 86400000),
      server_id: null,
    };
  }
</script>

<style scoped></style>
