<template>
  <div>
    <n-card :bordered="false" class="proCard" title="金币消费排行">
      <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="-4000"
      />
    </n-card>

    <UserModal ref="userModalRef" :userId="userId" />
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { NTag, useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { FormSchema, useForm } from '@/components/Form/index';
  import { diamondRanking } from '@/api/echarts/rechargeList';
  import UserModal from '@/views/account/accountList/modal.vue';
  import { UtilsTool } from '@/utils/macd/UtilsTool';
  import { useUserStoreWidthOut } from '@/store/modules/user';

  const type = ref(2);
  const useUserStore = useUserStoreWidthOut();
  const columns = [
    {
      title: '排名',
      dataIndex: 'id',
      key: 'id',
    },
    {
      title: '玩家ID',
      dataIndex: 'user_id',
      key: 'user_id',
    },
    {
      title: '昵称',
      dataIndex: 'nickName',
      key: 'nickName',
    },
    {
      title: '消费金币',
      dataIndex: 'total',
      key: 'total',
      render(row) {
        return h(
          NTag,
          {
            style: {
              marginRight: '6px',
            },
            type: 'info',
            bordered: false,
          },
          {
            default: () => UtilsTool.unitConversion(row.total),
          }
        );
      },
    },
  ];

  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);
        },
      },
    },
  ];

  const formRef: any = ref(null);
  const message = useMessage();
  const actionRef = ref();

  const formParams = reactive({});

  const params = ref({
    pageSize: 10,
  });

  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 diamondRanking({
      ...formParams,
      ...params.value,
      ...res,
      ...formRef.value.formModel,
      ...{ type: type.value },
    });
  };

  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);

    reloadTable();
  }

  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();
  }
</script>

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