import React from 'react'
import { Popconfirm, Button, Icon } from 'antd'

function ConfirmButton (props) {
  const colorType = {
    primary: '#347bbd',
    danger: '#f5222d'
  }
  const {
    isConfirm = true,
    onClick,
    type,
    icon,
    size,
    disabled,
    // key,
    title,
    confirmTitle,
    style,
    loading
  } = props
  // key值存在未定义的现象，暂取title使用
  const isRow = size === 'small'
  const disabledClick = disabled ? e => e.stopPropagation() : onClick
  const disabledStyle = disabled ? { color: 'gray', cursor: 'not-allowed' } : { cursor: 'pointer' }
  return (
    isConfirm && !disabled && !loading
      ? (
        <Popconfirm title={confirmTitle || '是否确认执行?'} onConfirm={onClick} key={title}>
          {
            isRow
              ? (
                <div>
                  <Icon
                    type={icon}
                    className='table_rowBtnStyle'
                    title={title}
                    style={{ ...disabledStyle, ...style, color: colorType[type] }}
                    loading={loading}
                  />
                </div>
              )
              : (
                <Button
                  type={type}
                  icon={icon}
                  disabled={disabled}
                  style={style}
                  loading={loading}
                >{title}</Button>
              )
          }
        </Popconfirm>
      )
      : isRow
        ? (
          <div key={title}>
            <Icon
              type={icon}
              className='table_rowBtnStyle'
              onClick={disabledClick}
              title={title}
              style={{ ...disabledStyle, ...style, color: colorType[type] }}
              loading={loading}
            />
          </div>
        )
        : (
          <Button
            type={type}
            key={title}
            icon={icon}
            disabled={disabled}
            onClick={onClick}
            style={style}
            loading={loading}
          >{title}</Button>
        )
  )
}
export default ConfirmButton
