<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-modal
          v-model:show="showModal"
          :show-icon="false"
          preset="dialog"
          title="格式化"
          style="width: 95%"
        >
          <n-card
            :bordered="false"
            class="proCard mt-4"
            size="small"
            :segmented="{ content: true }"
            title="数据参数"
          >
            <JsonViewer
              :value="preview"
              :expand-depth="10"
              sort
              style="width: 100%; min-width: 3.125rem"
            />
          </n-card>
          <template #action>
            <n-space>
              <n-button @click="() => (showModal = false)">关闭</n-button>
            </n-space>
          </template>
        </n-modal>
      </n-card>
    </div>
  </template>
  
  <script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { JsonViewer } from 'vue3-json-viewer';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getGameCheatingAlarmList } 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:0,
    pageSize: 10,
  });
  const showModal = ref(false);
  const preview = ref();

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

  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, 1),
          },
          {
            label: '角色格式化',
            onClick: handleEdit.bind(null, record, 2),
          },
          // {
          //   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 getGameCheatingAlarmList(
      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 handleEdit(record: Recordable, type: Number) {
    showModal.value = true;
    if (type == 1) {
      preview.value = record.alarm_data;
    }
    if (type == 2) {
      preview.value = record.role_info;
    }
  }

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