<template>
    <div>
      <div class="n-layout-page-header">
        <n-card :bordered="false" title="异常记录" >
          各个游戏服的异常记录
        </n-card>
      </div>
      <n-card :bordered="false" class="proCard">
        <BasicForm
          @register="register"
          @reset="handleReset"
          ref="formRef"
          @submit="handleSubmit"
          @keyup.enter="handleSubmit"
        >
          <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"
          :scroll-x="1090"
          :flex-height="false"
        />
      </n-card>
    </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 { getGameDataAlarmList } from '@/api/account/gameDataAlarm';
  import { columns } from './columns';
  import { delNullProperty } from '@/utils/array';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { playerAccountBan } from '@/api/account/list';
  const useUserStore = useUserStoreWidthOut();

  const params = ref({
    server_id:1,
    pageSize: 10,
    //ratio:0,
  });
  
  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;
        },
      },
    },
    {
      field: 'player_id',
      component: 'NInput',
      label: '玩家ID',
      componentProps: {
        placeholder: '请输入玩家ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ message: '请输入玩家ID', trigger: ['blur'] }],
    },
    {
      field: 'time',
      component: 'NDatePicker',
      label: '触发时间',
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        //shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  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: handleBan.bind(null, record),
            ifShow: () => {
              return record.ban === 0;
            },
          },
        ],
        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 getGameDataAlarmList(
      delNullProperty({ ...params.value, ...res,...formRef.value.formModel })
    );
  };
  
  function handleReset(values: Recordable) {
    console.log(values);
    params.value.server_id = 1;
    reloadTable();
  }

  function handleBan(values: Recordable) {
    console.log(values);
    playerAccountBan({ ...{ playerid: Number(values.userId), ban: 1 } }).then((res) => {
      console.log('res:' + JSON.stringify(res));
    });
  }


  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }
  
  function reloadTable() {
    actionRef.value.reload();
  }
  </script>
  
  <style lang="less" scoped></style>
  