import React, { useRef, useState } from 'react';
import { __ } from '@wordpress/i18n';

// tools
import set404FromErrorResponse from '../../../../helpers/set404FromErrorResponse';

// api
import ApplicationService from '../../../../service/application-service';

// store
import { useStoreValue } from '../../../../store';

import { Toast } from 'primereact/toast';
import { Button } from 'primereact/button';
import { Dialog } from 'primereact/dialog';

const EvaluationReAdmit = ({ id, status, statusUpdateFn }) => {
    const [isSendingRequest, setIsSendingRequest] = useState(false);
    const [isVisibleConfirmDialog, setIsVisibleConfirmDialog] = useState(false);
    const toast = useRef(null);
    const role = useStoreValue('getRole');

    const doReAdmit = () => {
        setIsSendingRequest(true);

        ApplicationService.reAdmitApplication(id, reAdmitCallback, errReAdmitCallback);
    }

    const reAdmitCallback = (resp) => {
        if (resp.status === 'SUCCESS') {
            if (toast.current && resp.message) {
                toast.current.show({
                    severity: 'success',
                    summary: '',
                    detail: resp.message
                });
            }
            if (statusUpdateFn) {
                statusUpdateFn(resp.data.status);
            }
        }
        setIsSendingRequest(false);
    }

    const errReAdmitCallback = (resp) => {
        if (toast.current && resp.message) {
            toast.current.show({
                severity: resp.status === 'SUCCESS' ? 'info' : 'error',
                summary: '',
                detail: resp.message
            });
        }
        set404FromErrorResponse(resp);
        setIsSendingRequest(false);
    }

    const headerConfirmDialog = () => {
        return <span>{__('Richiesta di conferma', 'gepafin')}</span>;
    }

    const hideConfirmDialog = () => {
        setIsVisibleConfirmDialog(false);
    }

    const footerConfirmDialog = () => {
        return <div>
            <Button type="button" label={__('No', 'gepafin')} onClick={() => setIsVisibleConfirmDialog(false)} outlined/>
            <Button
                type="button"
                label={__('Si', 'gepafin')} onClick={doConfirm}/>
        </div>
    }

    const doConfirm = () => {
        setIsVisibleConfirmDialog(false);
        doReAdmit();
    }

    return (
        ['ROLE_SUPER_ADMIN', 'ROLE_INSTRUCTOR_MANAGER'].includes(role) && ['REJECTED'].includes(status)
            ? <>
                <Toast ref={toast}/>
                <Button
                    type="button"
                    disabled={isSendingRequest}
                    severity="warning"
                    onClick={() => setIsVisibleConfirmDialog(true)}
                    label={__('Riammetti', 'gepafin')}
                    icon="pi pi-arrow-circle-up" iconPos="right"/>
                <Dialog
                    visible={isVisibleConfirmDialog}
                    modal
                    header={headerConfirmDialog}
                    footer={footerConfirmDialog}
                    style={{ maxWidth: '600px', width: '100%' }}
                    onHide={hideConfirmDialog}>
                    <div className="appForm__field">
                        <p>{__('È autorizzato dal direttore e autorizzazione caricata su portale a seguito del quale parte l\'email?', 'gepafin')}</p>
                    </div>
                </Dialog>
            </> : null
    )
}

export default EvaluationReAdmit;