import { PlusOutlined } from '@ant-design/icons';
import type {
  ActionType,
  ProColumns,
  ProFormInstance,
} from '@ant-design/pro-components';
import {
  ProCard,
  ProTable,
} from '@ant-design/pro-components';
import dayjs from 'dayjs'
import { Button, message, Popconfirm, Space } from 'antd';
import { useState, useRef } from 'react';
//引入接口
import { userlogApi } from '@/api/logs/service'

export default () => {
  const [fromType, setFromType] = useState<any>('table')
  const ref = useRef<ProFormInstance>();
  const actionRef = useRef<ActionType>();

  const Reolod = () => {
    ref.current?.resetFields()
    actionRef.current?.reload()
  }
  const columns: ProColumns<userlog.output>[] = [
    {
      title: '主键',
      dataIndex: 'id',
      hideInForm: true,
      search: false,
    },
    {
      title: '权限',
      dataIndex: 'permission',
      ellipsis: true,
      search: false,
    },
    {
      title: '用户名',
      dataIndex: 'userName',

    },
    {
      title: '日志消息',
      dataIndex: 'msgInfo',
      width: 120,
    },
    {
      title: '时间',
      dataIndex: 'createDate',
      render(_, record) {
        return <>{record.createDate ? dayjs(record.createDate).format("YYYY-MM-DD HH:mm:ss") : ""}</>
      },
    },
  ];

  return (
    <>
      <ProCard>
        {fromType === 'form' ? <Button type='link' onClick={() => {
          ref.current?.resetFields();
          setFromType("table")
        }}>⬅返回</Button> : ""}
        <ProTable<userlog.output>
          columns={columns}
          type={fromType as 'table'}
          formRef={ref}
          actionRef={actionRef}
          onSubmit={(params: any) => {
            console.log(params);
            if (fromType === 'form') {
              if (params.id) {
                userlogApi.Update(params).then((res: MyResponse.Content) => {
                  if (res.statusCode === 200) {
                    message.info("更新成功!");
                    setFromType('table')
                    Reolod()
                  } else {
                    message.error(res.errors);
                  }
                })
              } else {
                userlogApi.Add(params).then((res: MyResponse.Content) => {
                  if (res.statusCode === 200) {
                    message.info("新增成功!");
                    setFromType('table')
                    Reolod()
                  } else {
                    message.error(res.errors);
                  }
                })
              }
            }
          }}
          request={async (params = {}) => {
            const jsonData: userlog.queryPage = {
              pageIndex: params.current || 1,
              pageSize: params.pageSize || 10,
              userName: params.userName,
              msgInfo: params.msgInfo
            };
            const response = await userlogApi.PagedList(jsonData);
            if (response.statusCode === 200) {
              return {
                data: response.data.items,
                success: true,
                total: response.data.total,
              };
            } else {
              return {
                data: [],
                success: false,
                total: 0,
              };
            }
          }}
          pagination={{
            pageSize: 5,
            showSizeChanger:false
          }}
          rowKey="id"
          dateFormatter="string"
          headerTitle="列表"
          toolBarRender={false}
        />
      </ProCard>
    </>
  );
};