import React, { useEffect, useState } from 'react';
import { __ } from '@wordpress/i18n';
import { pathOr } from 'ramda';
import NumberFlow from '@number-flow/react';

// api
import DashboardService from '../../service/dashboard-service';

// components
import LatestBandiTableAsync from '../Dashboard/components/LatestBandiTableAsync';
import AllDomandeTableAsync from '../Domande/components/AllDomandeTableAsync';
import DraftApplicationsTableAsync from '../Dashboard/components/DraftApplicationsTableAsync';
import LatestPecEmailsTable from './components/LatestPecEmailsTable';

const DashboardDirector = () => {
    const [mainStats, setMainStats] = useState({});

    const getStatValue = (key, fallback = '') => {
        return pathOr(fallback, [key], mainStats);
    }

    const getStats = (data) => {
        if (data.status === 'SUCCESS') {
            setMainStats(data.data.widget1);
        }
    }

    const errGetStats = () => {
    }

    useEffect(() => {
        DashboardService.getAdminStats(getStats, errGetStats);
    }, []);

    return (
        <div className="appPage">
            <div className="appPage__pageHeader">
                <h1>{__('Dashboard', 'gepafin')}</h1>
            </div>

            <div className="appPage__spacer"></div>

            <div className="appPageSection statsBigBadges">
                <h2>{__('Panoramica di Sistema', 'gepafin')}</h2>
                <div className="statsBigBadges__grid">
                    <div className="statsBigBadges__gridItem">
                        <span>{__('Bandi attivi', 'gepafin')}</span>
                        <span><NumberFlow
                            value={getStatValue('numberOfActiveCalls', 0)}
                            format={{ notation: 'compact' }}
                            locales="it-IT"/></span>
                    </div>
                    <div className="statsBigBadges__gridItem">
                        <span>{__('Utenti registrati', 'gepafin')}</span>
                        <span><NumberFlow
                            value={getStatValue('numberOfResgisteredUsers', 0)}
                            format={{ notation: 'compact' }}
                            locales="it-IT"/></span>
                    </div>
                    <div className="statsBigBadges__gridItem">
                        <span>{__('Domande in pre-istruttoria', 'gepafin')}</span>
                        <span><NumberFlow
                            value={getStatValue('numberOfSubmittedApplications', 0)}
                            format={{ notation: 'compact' }}
                            locales="it-IT"/></span>
                    </div>
                    <div className="statsBigBadges__gridItem">
                        <span>{__('Domande in bozza', 'gepafin')}</span>
                        <span><NumberFlow
                            value={getStatValue('numberOfDraftApplications', 0)}
                            format={{ notation: 'compact' }}
                            locales="it-IT"/></span>
                    </div>
                    <div className="statsBigBadges__gridItem">
                        <span>{__('Aziende', 'gepafin')}</span>
                        <span><NumberFlow
                            value={getStatValue('numberOfCompany', 0)}
                            format={{ notation: 'compact' }}
                            locales="it-IT"/></span>
                    </div>
                    <div className="statsBigBadges__gridItem">
                        <span>{__('Totale finanziamenti attivi', 'gepafin')}</span>
                        <span><NumberFlow
                            value={getStatValue('totalActiveFinancing', 0)}
                            format={{
                                notation: 'compact',
                                compactDisplay: 'short',
                                roundingMode: 'trunc',
                                style: 'currency',
                                currency: 'EUR',
                                currencyDisplay: 'symbol'
                            }}
                            locales="pl-PL"/></span>
                    </div>
                </div>
            </div>

            <div className="appPage__spacer"></div>

            <div className="appPageSection">
                <h2>{__('Autorizazzioni', 'gepafin')}</h2>
                <LatestPecEmailsTable/>
            </div>

            <div className="appPage__spacer"></div>

            <div className="appPageSection">
                <h2>{__('Ultimi bandi pubblicati', 'gepafin')}</h2>
                <LatestBandiTableAsync disableActions={true}/>
            </div>

            <div className="appPage__spacer"></div>

            <div className="appPageSection">
                <h2>{__('Ultime domande pubblicate', 'gepafin')}</h2>
                <AllDomandeTableAsync disableActions={true}/>
            </div>

            <div className="appPage__spacer"></div>

            <div className="appPageSection">
                <h2>{__('Domande in bozza', 'gepafin')}</h2>
                <DraftApplicationsTableAsync disableActions={true}/>
            </div>

            <div className="appPage__spacer"></div>
        </div>
    )
}

export default DashboardDirector;