import React, { Component } from 'react';
import PropTypes from 'prop-types'
import { IconEdit } from 'patd';
import { $buttonColor } from '../styles/theme.js'
import style from './table-cell-editor.scss';

const editInputStyle = {
  maxWidth: 180,
  width: 180,
  padding: '0 0 2px 0',
  marginBottom: 4,
  lineHeight: '22px',
  border: 0,
  outline: 0,
  borderBottom: `2px solid ${$buttonColor}`,
  borderRadius: 0,
}

class TableCellEditor extends Component {

  render() {

    let { clickedRowIndex, index, content, onEditIconClick, resetClickedIndex, onInputChange, currentEditValue, onContentReset } = this.props
    return (
      <div>
        <div style={{ display: clickedRowIndex === index ? 'none' : 'flex' }}>
          <span>{content}</span>
          <div
            className={style['icon-wrapper']}
            onClick={() => { onEditIconClick(index, content) }}
            onBlur={resetClickedIndex}
          >
            <IconEdit style={{ cursor: 'pointer' }} />
          </div>
        </div>
        <input
          ref={(ipt) => { this.editInput = ipt; }}
          value={currentEditValue}
          style={Object.assign({}, editInputStyle, { display: clickedRowIndex !== index ? 'none' : 'block' })}
          onBlur={onContentReset}
          onChange={(e) => { onInputChange(e) }}
        />
      </div>
    )
  }
}

TableCellEditor.propTypes = {
  // 列表 Id
  rowId: PropTypes.string,
  // 被点击表格行下标，从 0 开始
  clickedRowIndex: PropTypes.number,
  // 遍历表格行下标，从 0 开始
  index: PropTypes.number,
  // 开始的编辑内容 
  content: PropTypes.string,
  // 编辑时的内容
  currentEditValue: PropTypes.string,
  // 点击图标按钮触发
  onEditIconClick: PropTypes.func,
  // 编辑框失去焦点触发
  resetClickedIndex: PropTypes.func,
  // 编辑时触发
  onInputChange: PropTypes.func,
  // 调用接口方法
  onContentReset: PropTypes.func,
}

TableCellEditor.defaultProps = {
  onEditIconClick: () => { },
  resetClickedIndex: () => { },
  onInputChange: () => { },
  onContentReset: () => { }
}

export default TableCellEditor
