import React, {Component} from 'react';
import OrderListItem from './OrderListItem.jsx';
import {WiaasTable, WiaasTableHeader, WiaasTableBody} from '../../../mainComponents/table/WiaasTable.jsx';
import {orderTexts} from '../../../constants/ordersConstants';

class OrderList extends Component {
    constructor(props) {
        super(props);
        this.getHeadersByType = this.getHeadersByType.bind(this);
    }

    getHeadersByType(type) {
        const activeOrdersHeader = [
            orderTexts.headers.ORDER,
            orderTexts.headers.REFERENCE,
            orderTexts.headers.ORDER_DATE,
            orderTexts.headers.ESTIMATED_DATE,
            orderTexts.headers.COMMERCIAL_LEAD,
            orderTexts.headers.AMOUNT,
            orderTexts.headers.STATUS,
            ''
        ];

        const historyOrdersHeader = [
            orderTexts.headers.ORDER,
            orderTexts.headers.REFERENCE,
            orderTexts.headers.ORDER_DATE,
            orderTexts.headers.DELIVERY_DATE,
            orderTexts.headers.COMMERCIAL_LEAD,
            orderTexts.headers.AMOUNT,
            orderTexts.headers.STATUS,
            orderTexts.headers.END_OF_LIFE,
            ''
        ];

        if(this.props.showOrderCustomer) {
            activeOrdersHeader.splice(2, 0, orderTexts.headers.PLACED_BY);
            activeOrdersHeader.join();
            historyOrdersHeader.splice(2, 0, orderTexts.headers.PLACED_BY);
            historyOrdersHeader.join();
        }

        return type === 'active' ? activeOrdersHeader : historyOrdersHeader;
    }

    render() {
        const {orders, type} = this.props;

        return (
            <div>
                <WiaasTable>
                    <WiaasTableHeader headers={this.getHeadersByType(type)}/>
                    <WiaasTableBody>
                        {
                            orders.map(order => <OrderListItem key={order.id} order={order} type={type} />)
                        }
                    </WiaasTableBody>
                </WiaasTable>
            </div>
        );
    }
}
export default OrderList;
