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

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

import Tr from '../_Tr';

import HorizontalAlign from '../_statics/HorizontalAlign';
import SelectMode from '../_statics/SelectMode';
import SelectAllMode from '../_statics/SelectAllMode';
import SortingType from '../_statics/SortingType';

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

class Tbody extends Component {

    static Align = HorizontalAlign;
    static SelectMode = SelectMode;
    static SelectAllMode = SelectAllMode;
    static SortingType = SortingType;

    constructor(props, ...restArgs) {
        super(props, ...restArgs);
    }

    isItemChecked = rowData => {

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

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

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

    };

    render() {

        const {
            columns, data, startIndex, idProp, disabled,
            onRowClick, onCellClick
        } = this.props;

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

    }
}

Tbody.propTypes = {

    /**
     * Children passed into table header.
     */
    columns: PropTypes.arrayOf(PropTypes.shape({

        /**
         * width of column
         */
        width: PropTypes.number,

        /**
         * minimum width of column
         */
        minWidth: PropTypes.number,

        /**
         * The class name of header.
         */
        headClassName: PropTypes.string,

        /**
         * Override the styles of header.
         */
        headStyle: PropTypes.object,

        /**
         * align of table header cell
         */
        headAlign: PropTypes.oneOf(Util.enumerateValue(HorizontalAlign)),

        /**
         * The render content in header.
         * (1) string，example： 'id'
         * (2) callback，example：function (colIndex) {return colIndex;}
         */
        headRenderer: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),

        /**
         * The class name of td.
         */
        bodyClassName: PropTypes.string,

        /**
         * Override the styles of td.
         */
        bodyStyle: PropTypes.object,

        /**
         * The render content in table.
         * (1) data key，example： 'id'
         * (2) data key tamplate，example：'${id} - ${name}'
         * (3) callback，example：function (rowData, rowIndex, colIndex) {return rowData.id;}
         */
        bodyRenderer: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),

        /**
         * align of table body cell
         */
        bodyAlign: PropTypes.oneOf(Util.enumerateValue(HorizontalAlign)),

        /**
         * The class name of footer.
         */
        footClassName: PropTypes.string,

        /**
         * Override the styles of footer.
         */
        footStyle: PropTypes.object,

        /**
         * The render content in footer.
         * (1) string，example： 'id'
         * (2) callback，example：function (colIndex) {return colIndex;}
         */
        footRenderer: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),

        /**
         * align of table footer cell
         */
        footAlign: PropTypes.oneOf(Util.enumerateValue(HorizontalAlign)),

        /**
         * If true,this column can be sorted.
         */
        sortable: PropTypes.bool,

        /**
         * Sorting property.
         */
        sortingProp: PropTypes.string,

        defaultSortingType: PropTypes.oneOf(Util.enumerateValue(SortingType)),

        span: PropTypes.func

    })).isRequired,

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

    onRowClick: PropTypes.func,
    onCellClick: PropTypes.func

};

Tbody.defaultProps = {
    selectMode: SelectMode.SINGLE_SELECT,
    startIndex: 0,
    idProp: 'id',
    disabled: false
};

export default Tbody;

@@ -2579,145 +2579,8 @@
 t,%0A%0A
-        /**%0A         * align of table header cell%0A         */%0A        headAlign: PropTypes.oneOf(Util.enumerateValue(HorizontalAlign)),%0A%0A
     
@@ -2819,32 +2819,308 @@
 pTypes.func%5D),%0A%0A
+        /**%0A         * align of table header cell%0A         */%0A        headAlign: PropTypes.oneOf(Util.enumerateValue(HorizontalAlign)),%0A%0A        /**%0A         * column span of table header%0A         */%0A        headSpan: PropTypes.oneOfType(%5BPropTypes.number, PropTypes.func%5D),%0A%0A
         /**%0A    
@@ -3762,32 +3762,169 @@
 zontalAlign)),%0A%0A
+        /**%0A         * column span of table body%0A         */%0A        bodySpan: PropTypes.oneOfType(%5BPropTypes.number, PropTypes.func%5D),%0A%0A
         /**%0A    
@@ -4495,32 +4495,169 @@
 zontalAlign)),%0A%0A
+        /**%0A         * column span of table foot%0A         */%0A        footSpan: PropTypes.oneOfType(%5BPropTypes.number, PropTypes.func%5D),%0A%0A
         /**%0A    
@@ -4919,39 +4919,8 @@
 pe))
-,%0A%0A        span: PropTypes.func
 %0A%0A  

update Tbody