import AuthorSelect from '@/components/AuthorSelect';
import { poemListUsingPOST } from '@/services/luigi/poem';
import { PlusOutlined } from '@ant-design/icons';
import ProTable from '@ant-design/pro-table';
import { Button } from 'antd';
import React, { useRef, useState } from 'react';

import type { ProColumns, ActionType } from '@ant-design/pro-table';
const columns: ProColumns<API.PoemBo>[] = [
  {
    dataIndex: 'index',
    valueType: 'indexBorder',
    width: 48,
  },
  {
    title: '标题',
    dataIndex: 'title',
    ellipsis: true,
    hideInSearch: true,
    tip: '标题过长会自动收缩',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '此项为必填项',
        },
      ],
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    valueType: 'select',
    valueEnum: {
      0: {
        text: '未校准',
        status: 'Error',
      },
      1: {
        text: '已校准',
        status: 'Success',
      },
    },
  },
  {
    title: '作者',
    dataIndex: 'authorName',
    hideInSearch: true,
  },
  {
    title: '作者',
    dataIndex: 'author',
    hideInTable: true,
    formItemProps: {
      rules: [
        {
          required: true,
          message: '此项为必填项',
        },
      ],
    },
    renderFormItem: (item, { type, defaultRender, ...rest }, form) => {
      return <AuthorSelect {...rest} />;
    },
  },
  {
    title: '首行',
    dataIndex: 'line',
    hideInSearch: true,
  },
];

export default () => {
  const actionRef = useRef<ActionType>();

  return (
    <ProTable<API.PoemBo>
      columns={columns}
      actionRef={actionRef}
      cardBordered
      request={async (params = {}, sort, filter) => {
        const query: API.QueryParam[] = [];
        if (params.author) {
          let author = params.author;
          if (typeof params.author == 'string') {
            author = JSON.parse(params.author);
          }
          query.push({ key: 'authorId', val: author.value });
        }
        if (params.status) {
          query.push({ key: 'status', val: params.status });
        }
        const response = await poemListUsingPOST({
          current: params.current,
          pageSize: params.pageSize,
          query: query,
        });
        return { data: response.data?.list, total: response.data?.total };
      }}
      editable={{
        type: 'multiple',
      }}
      columnsState={{
        persistenceKey: 'pro-table-singe-demos',
        persistenceType: 'localStorage',
        onChange(value) {
          console.log('value: ', value);
        },
      }}
      rowKey="id"
      search={{
        labelWidth: 'auto',
      }}
      form={{
        // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
        syncToUrl: (values, type) => {
          if (type === 'get') {
            return {
              ...values,
              created_at: [values.startTime, values.endTime],
            };
          }
          return values;
        },
      }}
      pagination={{
        pageSize: 5,
      }}
      dateFormatter="string"
      headerTitle="高级表格"
      toolBarRender={() => [
        <Button key="button" icon={<PlusOutlined />} type="primary">
          新建
        </Button>,
      ]}
    />
  );
};
