<template>
  <n-card :bordered="false" class="proCard">
    <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"
      :flex-height="false"
      :resizeHeightOffset="-500"
    />
  </n-card>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getLoginList } from '@/api/echarts/rechargeList';
  import { columns } from './columns';
  import { userInfoObj } from '@/views/account/accountList/userInfo';
  import { defRangeShortcuts } from '@/utils/dateUtil';

  interface Props {
    userId?: number | string;
    userInfo?: any;
  }
  const props = withDefaults(defineProps<Props>(), { userId: 0, userInfo: userInfoObj });

  const schemas: FormSchema[] = [
    {
      field: 'id',
      component: 'NInput',
      label: '记录ID',
      componentProps: {
        placeholder: '请输入记录ID',
        onInput: (e: any) => {
          console.log(e);
          params.value.id = e;
        },
      },
    },
    {
      field: 'event_at',
      component: 'NDatePicker',
      label: '选择日期',
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.event_at = e;
        },
      },
    },
  ];

  const actionRef = ref();
  const formParams = reactive({
    playerId: props.userId,
  });

  const params = ref<any>({
    id: 0,
    difficulty: -1,
    chapter_id: 0,
    playerId: props.userId,
    event_at: null,
  });

  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 getLoginList({ ...formParams, ...params.value, ...res });
  };

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

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleSubmit(_values: Recordable) {
    reloadTable();
  }

  function handleReset(values: Recordable) {
    params.value = {
      difficulty: -1,
      chapter_id: 0,
      playerId: props.userId,
      event_at: null,
    };
    console.log(values);
    reloadTable();
  }
</script>

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