<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>
      <n-alert :show-icon="false"
        >数据实时更新，客户端排行榜5分钟更新一次，故可能存在排名差异</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"
        :pagination="false"
      />
    </n-card>
    <UserModal ref="userModalRef" :userId="userId" />
  </div>
</template>

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

  const useUserStore = useUserStoreWidthOut();
  const params = ref({
    pageSize: 10,
    server_id: 1,
  });
  const schemas: FormSchema[] = [
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        defaultValue: params.value.server_id,
        options: useUserStore.config.server_simple_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.server_id = e;
        },
      },
    },
  ];
  const dialog = useDialog();
  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 getPVPRankingList({ ...formParams, ...params.value, ...res });
  };

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

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

  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;
            },
          },
          {
            label: '清除排名',
            onClick: handleClearRank.bind(null, record),
            ifShow: () => {
              return true;
            },
          },
        ],
        select: (key) => {
          message.info(`您点击了，${key} 按钮`);
        },
      });
    },
  });

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

  function handleClearRank(record: Recordable) {
    dialog.warning({
      title: '警告',
      content: '你确定要清除玩家 [' + record.user_id + '] 的狭路对决排名吗？',
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: () => {
        ClearPVPRank({ user_id: record.user_id, server_id: params.value.server_id })
          .then((_res) => {
            message.success('操作成功');
            reloadTable();
          })
          .catch((e: Error) => {
            message.error(e.message ?? '操作失败');
          });
      },
      onNegativeClick: () => {
        // message.error('取消');
      },
    });
  }

  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);
    params.value.server_id = 1;
    reloadTable();
  }
</script>

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