<template>
  <div>
    <div class="header">
      <SvgIcon size="19" name="list" />
      <div class="title">日志列表</div>
    </div>
    <BasicTable @register="registerTable" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable } from '@/components/Table';
  import { isObject } from '@/utils/is';
  import dayjs from 'dayjs';
  import * as LogApi from '@/api/system/logApi';
  import { columns, searchFormSchema } from './schema';
  import {SvgIcon} from "@/components/Icon";
  import {onMounted} from "vue";

  const [registerTable] = useTable({
    api: (params) => LogApi.search(handleParams(params)),
    columns,
    formConfig: {
      labelWidth: 0,
      schemas: searchFormSchema,
      showAdvancedButton: false,
    },
    useSearchForm: true,
    showTableSetting: false,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    rowKey: (record: any) => record.id,
  });

  onMounted(() => {
    WebViewService.setIsLoading(false).then(() => {});
  });

  const handleParams = (params, embedded = true) => {
    const { pageNum, pageSize, field = 'id', order = 'descend', ...rest } = params;
    const handledParams: any = { pageNum, pageSize, orderByClause: `${field} ${order === 'descend' ? 'desc' : 'asc'}` };
    Object.keys(rest).forEach((key) => {
      const schema = searchFormSchema.find((item) => item.field === key);
      const value = params[key];
      const paramKey = key;
      if (schema) {
        if (value !== undefined && value !== '') {
          if (schema.component === 'Input') {
            const wildStr = embedded ? "" : "%"
            handledParams[paramKey] = `${wildStr}${value.trim()}${wildStr}`;
          } else if (['Select', 'ApiSelect', 'ApiTreeSelect'].includes(schema.component)) {
            handledParams[paramKey] = isObject(value) ? value.value : value;
          } else if (schema.component === 'RangePicker') {
            handledParams[`${paramKey}From`] = dayjs(value[0]).startOf('d').format('YYYY-MM-DD HH:mm:ss');
            handledParams[`${paramKey}To`] = dayjs(value[1]).endOf('d').format('YYYY-MM-DD HH:mm:ss');
          } else if (schema.component === 'DatePicker') {
            handledParams[paramKey] = dayjs(value).format((schema.componentProps as any).format || 'YYYY-MM-DD');
          } else {
            handledParams[paramKey] = value;
          }
        }
      } else {
        handledParams[paramKey] = value;
      }
    });
    return handledParams;
  };
</script>
<style lang="scss" scoped>
  @use '@/assets/custom.scss';
</style>
