import { Button, Popconfirm } from 'antd';
import { FC, ReactNode } from 'react';

export interface DageTableActionsProps {
  /**
   * 是否显示编辑按钮
   * @default true
   */
  showEdit?: boolean;
  /**
   * 是否显示删除按钮
   * @default true
   */
  showDelete?: boolean;
  /** 前置插槽 */
  renderBefore?: ReactNode;
  /**
   * 按钮大小
   * @default small
   */
  size?: 'large' | 'middle' | 'small';
  /** 编辑回调 */
  onEdit?: () => void;
  /** 删除回调 */
  onDelete?: () => void;
}

export const DageTableActions: FC<DageTableActionsProps> = ({
  renderBefore,
  showDelete = true,
  showEdit = true,
  size = 'small',
  onDelete,
  onEdit,
}) => {
  const handleEdit = () => {
    onEdit?.();
  };

  const handleDelete = async () => {
    await onDelete?.();
  };

  return (
    <>
      {renderBefore}
      {showEdit && (
        <Button className="dage-table-action__edit" size={size} type="link" onClick={handleEdit}>
          编辑
        </Button>
      )}
      {showDelete && (
        <Popconfirm
          title="删除后无法恢复，是否删除?"
          okText="删除"
          cancelText="取消"
          onConfirm={handleDelete}
        >
          <Button className="dage-table-action_del" size={size} type="link" danger>
            删除
          </Button>
        </Popconfirm>
      )}
    </>
  );
};
