content: import PropTypes from 'prop-types';
  import React from 'react';
  import { connect } from 'react-redux';
  import uuid from 'uuid';
  //
  import * as Basic from '../../components/basic';
  import * as Advanced from '../../components/advanced';
  import * as Utils from '../../utils';
  import { CodeListManager } from '../../redux';
  
  const manager = new CodeListManager();
  
  /**
   * Table of code lists
   *
   * @author Radek Tomiška
   * @since 9.4.0
   */
  export class CodeListTable extends Advanced.AbstractTableContent {
  
    constructor(props, context) {
      super(props, context);
      this.state = {
        filterOpened: props.filterOpened,
        showLoading: true
      };
    }
  
    getContentKey() {
      return 'content.code-lists';
    }
  
    getManager() {
      return manager;
    }
  
    useFilter(event) {
      if (event) {
        event.preventDefault();
      }
      this.refs.table.useFilterForm(this.refs.filterForm);
    }
  
    cancelFilter(event) {
      if (event) {
        event.preventDefault();
      }
      this.refs.table.cancelFilter(this.refs.filterForm);
    }
  
    showDetail(entity) {
      if (entity.id === undefined) {
        const uuidId = uuid.v1();
        this.context.history.push(`/code-lists/${uuidId}/detail?new=1`);
      } else {
        this.context.history.push(`/code-lists/${ entity.id }/detail`);
      }
    }
  
    render() {
      const { filterOpened } = this.state;
      //
      return (
        <Basic.Div>
          <Advanced.Table
            ref="table"
            uiKey={ this.getUiKey() }
            manager={ this.getManager() }
            showRowSelection
            filter={
              <Advanced.Filter onSubmit={this.useFilter.bind(this)}>
                <Basic.AbstractForm ref="filterForm">
                  <Basic.Row className="last">
                    <Basic.Col lg={ 6 }>
                      <Advanced.Filter.TextField
                        ref="text"
                        placeholder={this.i18n('filter.text.placeholder')}/>
                    </Basic.Col>
                    <Basic.Col lg={ 6 } className="text-right">
                      <Advanced.Filter.FilterButtons cancelFilter={ this.cancelFilter.bind(this) }/>
                    </Basic.Col>
                  </Basic.Row>
                </Basic.AbstractForm>
              </Advanced.Filter>
            }
            buttons={
              [
                <Basic.Button
                  level="success"
                  key="add_button"
                  className="btn-xs"
                  onClick={this.showDetail.bind(this, { })}
                  rendered={ this.getManager().canSave() }
                  icon="fa:plus">
                  { this.i18n('button.add') }
                </Basic.Button>
              ]
            }
            afterBulkAction={
              (processedBulkAction) => {
                // clean all codelists in redux state, after delete bulk action ends
                if (processedBulkAction.id === 'core-code-list-delete-bulk-action') {
                  this.context.store.dispatch(this.getManager().clearCodeLists());
                }
              }
            }
            filterOpened={ filterOpened }
            _searchParameters={ this.getSearchParameters() }>
            <Advanced.Column
              header=""
              className="detail-button"
              cell={
                ({ rowIndex, data }) => {
                  return (
                    <Advanced.DetailButton
                      title={this.i18n('button.detail')}
                      onClick={this.showDetail.bind(this, data[rowIndex])}/>
                  );
                }
              }
              sort={false}/>
            <Advanced.Column property="code" width={ 250 } sort/>
            <Advanced.Column property="name" width={ 250 } sort/>
            <Advanced.Column property="description" sort face="text" maxLength={ 30 }/>
          </Advanced.Table>
        </Basic.Div>
      );
    }
  }
  
  CodeListTable.propTypes = {
    uiKey: PropTypes.string.isRequired,
    filterOpened: PropTypes.bool
  };
  
  CodeListTable.defaultProps = {
    filterOpened: true
  };
  
  function select(state, component) {
    return {
      _searchParameters: Utils.Ui.getSearchParameters(state, component.uiKey)
    };
  }
  
  export default connect(select, null, null, { forwardRef: true })(CodeListTable);
  