<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>
      <n-alert :show-icon="false"
        >统计数据从2023年6月12日开始，存在一定延迟和少量误差，请以玩家实际的消费记录为准</n-alert
      >
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="actionRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
        :resizeHeightOffset="-10000"
      />
    </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';
  import { itemRanking } from '@/api/echarts/rechargeList';
  import UserModal from '@/views/account/accountList/modal.vue';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { consumptionRankTypes } from '@/enums/consumptionEnum';
  import { endOfToday, startOfToday } from 'date-fns';
  import { defRangeShortcuts } from '@/utils/dateUtil';
  import { getOptionLabel } from '@/utils';

  // function typeMap() {
  //   const att: any = [];
  //   for (const key in consumptionRankTypes) {
  //     att.push({ value: key, label: consumptionRankTypes[key] + '(' + key + ')' });
  //   }
  //   return att;
  // }
  const defSource = ref(79);

  const params = ref({
    pageSize: 10,
    source: defSource.value,
  });

  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: 'source',
      key: 'source',
      render(row) {
        return h(
          NTag,
          {
            style: {
              marginRight: '6px',
            },
            type: 'info',
            bordered: false,
          },
          {
            default: () => getOptionLabel(consumptionRankTypes, row.source),
          }
        );
      },
      width: 150,
    },
    {
      title: '消耗/产出数量',
      dataIndex: 'total',
      key: 'total',
      render(row) {
        return h(
          NTag,
          {
            style: {
              marginRight: '6px',
            },
            type: 'info',
            bordered: false,
          },
          {
            default: () => 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);
        },
      },
    },
    {
      field: 'source',
      component: 'NSelect',
      label: '操作来源',
      componentProps: {
        placeholder: '请选择',
        options: consumptionRankTypes,
        defaultValue: params.value.source,
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      defaultValue: [startOfToday().getTime() - 86400 * 1000 * 6, endOfToday().getTime()],
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const formRef: any = ref(null);
  const message = useMessage();
  const actionRef = ref();
  const formParams = reactive({});

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

  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,
      source: defSource.value,
    };
    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>
