import React, {Component} from 'react';
import WiaasBox from '../../../mainComponents/box/WiaasBox.jsx';
import {API_SERVER} from '../../../config';
import FileDownloader from '../../../helpers/FileDownloader';

const fileHandler = new FileDownloader();

class OrderDocumentsGroup extends Component {
    constructor(props) {
        super(props);

        this.state = {};
    }

    downloadDocument(orderId, itemId, document){
        const fileUrl = `${API_SERVER}/wp-json/wiaas/documents/order/${orderId}/${document.type}?item_id=${itemId}&document_key=${document.key}`;
        fileHandler.download(fileUrl, `${document.name}.${document.extension}`);
    }

    render() {
        const {documentsGroup, orderId} = this.props;

        return (
             <div>
                 {
                     documentsGroup.documents &&
                     documentsGroup.documents.length > 0 &&
                     <WiaasBox mainTitle={documentsGroup.name}>
                         {
                             documentsGroup.documents.map(document => <a id={'document-' + document.key}  key={'order-document-' + document.key}>
                             <div onClick={() => {this.downloadDocument(orderId, documentsGroup.orderItemId, document)}} className="document-link-big">
                                     <i className={`fa fa-4x fa-${document.icon}`} aria-hidden="true"></i>
                                     <div>
                                         {document.name}
                                     </div>
                             </div></a>)
                         }
                     </WiaasBox>
                 }
            </div>)
    }
}

export default OrderDocumentsGroup;
