<template>
  <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>

    <BasicTable
      :openChecked="true"
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    />
  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getLogList } from '@/api/log/log';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';
  import { defRangeShortcuts } from '@/utils/dateUtil';

  const batchDeleteDisabled = ref(true);
  const checkedIds = ref([]);
  const schemas: FormSchema[] = [
    {
      field: 'admin_id',
      component: 'NInput',
      label: '操作人',
      componentProps: {
        placeholder: '请输入操作人员ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ trigger: ['blur'] }],
    },
    {
      field: 'path',
      component: 'NInput',
      label: '访问路径',
      componentProps: {
        placeholder: '请输入手机访问路径',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'ip',
      component: 'NInput',
      label: '访问IP',
      componentProps: {
        placeholder: '请输入IP地址',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'created_at',
      component: 'NDatePicker',
      label: '记录时间',
      componentProps: {
        type: 'datetimerange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'method',
      component: 'NSelect',
      label: '请求方式',
      componentProps: {
        defaultValue: 'POST',
        placeholder: '请选择请求方式',
        options: [
          {
            label: 'GET',
            value: 'GET',
          },
          {
            label: 'POST',
            value: 'POST',
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.method = e;
        },
      },
    },
  ];

  const router = useRouter();
  const actionRef = ref();
  const formParams = ref({});
  const params = ref({
    method: 'POST',
    pageSize: 10,
  });

  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),
          },
        ],
      });
    },
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
    batchDeleteDisabled.value = rowKeys.length <= 0;

    checkedIds.value = rowKeys;
  }

  const loadDataTable = async (res) => {
    return await getLogList({ ...formParams.value, ...params.value, ...res });
  };

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

  function handleEdit(record: Recordable) {
    router.push({ name: 'log-view', params: { id: record.id } });
  }

  function handleSubmit(values: Recordable) {
    formParams.value = values;
    reloadTable();
  }

  function handleReset(values: Recordable) {
    formParams.value = {};
    params.value.method = 'POST';
    reloadTable();
  }
</script>

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