<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <div class="n-layout-page-header">
        <n-card :bordered="false" title="看广告排行" />
      </div>
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset" ref="formRef">
        <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"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
        :resizeHeightOffset="-10000"
        :pagination="false"
      />
    </n-card>
    <UserModal ref="userModalRef" :userId="userId" />
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { FormSchema, useForm } from '@/components/Form';
  import { advRanking } from '@/api/echarts/rechargeList';
  import { columns } from './columns';
  import UserModal from '@/views/account/accountList/modal.vue';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { defRangeShortcuts } from '@/utils/dateUtil';
  import { endOfToday, startOfToday } from 'date-fns';

  const formRef: any = ref(null);
  const useUserStore = useUserStoreWidthOut();
  const schemas: FormSchema[] = [
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        options: useUserStore.config.server_simple_option,
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      defaultValue: [startOfToday().getTime() - 86400 * 1000 * 29, endOfToday().getTime()],
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const message = useMessage();
  const actionRef = ref();
  const formParams = reactive({
    name: '',
    address: '',
    date: 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 advRanking({ ...formParams, ...res, ...formRef.value.formModel });
  };

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

  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    // fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            type: 'default',
            label: '查询玩家信息',
            onClick: handleEdit.bind(null, record),
            ifShow: () => {
              return true;
            },
          },
        ],
        select: (key) => {
          message.info(`您点击了，${key} 按钮`);
        },
      });
    },
  });

  function handleEdit(record: Recordable) {
    openUserModal(record.user_id);
  }

  const userModalRef = ref();
  const userId = ref(0);
  function openUserModal(id) {
    userId.value = id;
    const { openDrawer } = userModalRef.value;
    openDrawer();
  }

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

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

  function reloadTable() {
    actionRef.value.reload();
  }
</script>

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