<template>
  <div>
    <div class="n-layout-page-header">
      <n-card :bordered="false" title="拉黑记录" />
    </div>
    <n-card :bordered="false" class="proCard">
      <BasicForm
        @register="register"
        @submit="handleSubmit"
        @reset="handleReset"
        @keyup.enter="handleSubmit"
        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"
        :flex-height="false"
        :pagination="false"
      />
    </n-card>

    <n-modal
      v-model:show="showModal"
      :mask-closable="false"
      preset="dialog"
      title="确认"
      content="删除不可撤销，确认删除此用户吗？"
      positive-text="确认"
      negative-text="取消"
      @positive-click="onPositiveClick"
      @negative-click="onNegativeClick"
    />
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { deletePlayer, getBanList } from '@/api/account/list';
  import { columns } from './ban_logs_columns';
  import { useRouter } from 'vue-router';
  import { delNullProperty } from '@/utils/array';

  const showModal = ref(false);
  const params = ref({
    pageSize: 100,
  });

  const schemas: FormSchema[] = [
    {
      field: 'id',
      component: 'NInput',
      label: '玩家ID',
      componentProps: {
        placeholder: '请输入玩家ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ message: '请输入玩家ID', trigger: ['blur'] }],
    },
    // {
    //   field: 'openId',
    //   component: 'NInput',
    //   label: 'openId',
    //   componentProps: {
    //     placeholder: '请输入openId',
    //     onInput: (e: any) => {
    //       console.log(e);
    //     },
    //   },
    //   rules: [{ message: '请输入openId', trigger: ['blur'] }],
    // },
    {
      field: 'accId',
      component: 'NInput',
      label: '账号ID',
      componentProps: {
        placeholder: '请输入账号ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ message: '请输入账号ID', trigger: ['blur'] }],
    },
  ];

  const router = useRouter();
  const message = useMessage();
  const actionRef = ref();

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

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

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

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

  function handleEdit(record: Recordable) {
    router.push({
      name: 'account-info',
      params: { id: record.playerid, record: JSON.stringify(record) },
    });
  }

  const deletePlayerId = ref(0);

  function onPositiveClick() {
    showModal.value = false;
    deletePlayer({
      ...{ playerid: deletePlayerId.value },
    })
      .then((res) => {
        console.log(res);
        reloadTable();
      })
      .catch((err) => {
        console.log(err);
        message.error(err);
      });
  }

  function onNegativeClick() {
    showModal.value = false;
  }

  function handleDeletePlayer(record: Recordable) {
    showModal.value = true;
    deletePlayerId.value = record.playerid;
  }

  function handleSubmit(values: Recordable) {
    console.log('handleSubmit values：' + JSON.stringify(values));
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
    reloadTable();
  }
</script>

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