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, DatePicker } from 'antd';
import { useState, useRef } from 'react';
//引入接口
import { runlogApi } from '@/api/logs/service'
const { RangePicker } = DatePicker;
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<runlog.output>[] = [
    {
      title: '主键',
      dataIndex: 'id',
      search: false
    },
    {
      title: '日志类别',
      dataIndex: 'logType',
      width: 160
    },
    {
      title: '日志消息',
      dataIndex: 'msgInfo',
    },
    {
      title: '时间',
      dataIndex: 'createDate',
      renderFormItem: (_, { type, defaultRender, ...rest }, form) => {
        return <RangePicker showTime />
      },
      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<runlog.output>
          columns={columns}
          type={fromType as 'table'}
          formRef={ref}
          actionRef={actionRef}
          onSubmit={(params: any) => {
            console.log(params);
            if (fromType === 'form') {
              if (params.id) {
                runlogApi.Update(params).then((res: MyResponse.Content) => {
                  if (res.statusCode === 200) {
                    message.info("更新成功!");
                    setFromType('table')
                    Reolod()
                  } else {
                    message.error(res.errors);
                  }
                })
              } else {
                runlogApi.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: runlog.queryPage = {
              pageIndex: params.current || 1,
              pageSize: params.pageSize || 10,
              logType: params.logType,
              msgInfo: params.msgInfo,
              startTime: params.createDate ? dayjs(params.createDate[0]).add(8, 'h').toDate() : null,
              endTime: params.createDate ? dayjs(params.createDate[1]).add(8, 'h').toDate() : null
            };
            const response = await runlogApi.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>
    </>
  );
};