/**
 * @file Tbody component
 * @author liangxiaojun(liangxiaojun@derbysoft.com)
 */

import React, {Component} from 'react';
import PropTypes from 'prop-types';

import TableRow from '../_TableRow';

import Util from '../_vendors/Util';
import SelectMode from '../_statics/SelectMode';

export default class Tbody extends Component {

    static SelectMode = SelectMode;

    constructor(props, ...restArgs) {

        super(props, ...restArgs);

        this.isItemChecked = ::this.isItemChecked;

    }

    isItemChecked(rowData) {

        const {selectMode, idProp, value} = this.props;

        if (selectMode === Tbody.SelectMode.NORMAL || !rowData || !value) {
            return false;
        }

        switch (selectMode) {
            case Tbody.SelectMode.MULTI_SELECT:
                return value.findIndex(item => item[idProp] === rowData[idProp]) !== -1;
            case Tbody.SelectMode.SINGLE_SELECT:
                return value[idProp] === rowData[idProp];
        }

    }

    render() {

        const {
            columns, data, startIndex, idProp, disabled,
            onRowTouchTap, onCellTouchTap
        } = this.props;

        return (
            <tbody className="tbody">

                {
                    data.map((row, rowIndex) => {
                        return (
                            <TableRow key={idProp && idProp in row ? row[idProp] : rowIndex}
                                      rowIndex={startIndex + rowIndex}
                                      columns={columns}
                                      data={row}
                                      isChecked={this.isItemChecked(row)}
                                      disabled={disabled || row.disabled}
                                      onRowTouchTap={onRowTouchTap}
                                      onCellTouchTap={onCellTouchTap}/>
                        );
                    })
                }

            </tbody>
        );

    }
};

Tbody.propTypes = {

    columns: PropTypes.array,
    data: PropTypes.array,
    startIndex: PropTypes.number,
    idProp: PropTypes.string,
    selectMode: PropTypes.oneOf(Util.enumerateValue(SelectMode)),
    disabled: PropTypes.bool,

    isItemChecked: PropTypes.func,

    onRowTouchTap: PropTypes.func,
    onCellTouchTap: PropTypes.func

};

Tbody.defaultProps = {
    data: [],
    columns: [],
    startIndex: 0,
    idProp: 'id',
    selectMode: SelectMode.NORMAL,
    disabled: false
};
@@ -614,22 +614,16 @@
 ode === 
-Tbody.
 SelectMo
@@ -653,24 +653,24 @@
 %7C !value) %7B%0A
+
             
@@ -733,38 +733,32 @@
            case 
-Tbody.
 SelectMode.MULTI
@@ -855,16 +855,16 @@
 !== -1;%0A
+
         
@@ -872,22 +872,16 @@
    case 
-Tbody.
 SelectMo
@@ -2424,22 +2424,29 @@
 ectMode.
-NORMAL
+SINGLE_SELECT
 ,%0A    di

optimize Tbody