import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
  useCurrentPermissions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { Image, Space } from 'antd';
import Create from './modals/Create';
import Show from './modals/Show';

export default function Index(props: MyBetaModalFormProps) {
  const getCurrentPermissions = useCurrentPermissions();
  let toolBarRender = (action: any) => {
    return getCurrentPermissions(
      {
        Create: (
          <Create
            key="Create"
            reload={action?.reload}
            item={props?.item}
            title="跟进记录"
          />
        ),
      },
      'MyFollows',
    );
  };
  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions(
      {
        update: <Show item={item} key="Show" />,
        delete: (
          <MyButtons.Delete
            key="delete"
            onConfirm={() =>
              Apis.Emergency.EmergencyEventFollows.Delete({
                id: item.id,
              }).then(() => action?.reload())
            }
          />
        ),
      },
      'MyFollows',
    );
  };
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          {
            ...params,
            emergency_events_id: props?.item?.id,
          },
          sort,
          Apis.Emergency.EmergencyEventFollows.List,
        )
      }
      search={false}
      headerTitle="跟进记录"
      toolBarRender={(action) => [toolBarRender(action)]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '操作',
          dataIndex: 'node_name',
        },
        {
          title: '内容',
          dataIndex: 'content',
        },
        {
          title: '附件',
          render: (_, item: any) => {
            return (
              <Image.PreviewGroup>
                {item?.attachments?.map((i: any, index: number) => (
                  <Image
                    width={60}
                    height={60}
                    src={i.url}
                    key={`img_${index}`}
                  />
                ))}
              </Image.PreviewGroup>
            );
          },
        },
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>{tableRender(item, action)}</Space>
          ),
        }),
      ]}
    />
  );
}
