import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { Table, Alert, Button , Row , Col } from 'react-bootstrap';
import { Bert } from 'meteor/themeteorchef:bert';
import { Capitalize  } from '../../../modules/utilities';
import { SortReturnRecentOne, StripHtml  } from '../../../modules/utilities2';
import Loading from '../../components/Loading/Loading';
import Uploader from '../Uploader/Uploader';
import { createContainer } from 'meteor/react-meteor-data';
import { Meteor } from 'meteor/meteor';
import './UploadPdfCopy.scss';


const removePdfCopy = ( paperId , paperToRemove ) => {
   const obj = paperToRemove;
   const askMe =  confirm('Are you sure ');
   if (askMe){
      Meteor.call('paper.deletePdfCopy' , paperId , obj , (error , response) => {
        if ( error ){
            Bert.alert(`Error : || ${error}` , danger );
        }
        else{
            Bert.alert(`Pdf copy deleted` , 'success');
        }
    })
   }

   
   
}


const UploadPdfCopy = ({loading , paper}) => (!loading ? (
   
      <Row>
        <Col md={3} mdOffset={3}>
        {paper && paper.pdf ? 
        ( <div><p> 
            <a download className="downloadAnchor"
             href= { paper.pdf.paperUrl }>
              <img src="/image/pdf.jpg" className="img img-responsive" />
              <br/>
             download pdf
            </a>
          <br/>
          <span><b>File Name :{SortReturnRecentOne( paper && paper.paperUploads).fileName}</b></span>
       
        <br/>    
         <span> 
              <Button onClick={()=>removePdfCopy(paper && paper._id , 
                                 SortReturnRecentOne(paper && paper.paperUploads))}
                 bsSize="xsmall"
                 bsStyle="danger">
                  delete pdf
               </Button>
         </span>
         </p>
       
        </div>
       ) : <p> 
             <a download className="downloadAnchor"
              href= {SortReturnRecentOne( paper && paper.paperUploads).paperUrl}>
               <img src="/image/word.png"/>
               <br/>
               download approved paper
              </a>
              <br/>
        <span><b>File Name :{SortReturnRecentOne( paper && paper.paperUploads).fileName}</b></span>
      </p>
    }
        </Col>

        <Col md={3}>
           {paper && paper.pdf ? (
              <Alert bsStyle="success">Pdf copy available</Alert>
           ) : 
              <Uploader
                paperId={paper && paper._id}
                slingshot='uploadPdfDocument'
                methodName='paper.uploadPdfCopy'
                uploadMessage="upload pdf copy of approved paper" />
           }
          
        </Col>
      </Row>
)
    

 : <Loading /> )

export default UploadPdfCopy


    
