import React from 'react';


import adobe from '../../../../assets/images/adobe.svg';
import {MonthDeltio, UserDeltia} from "../../../../models/HrSupport/PersonalFolder";
import EmptyList from "../../../UI/DuaList/EmptyList";
import PageSubtitle from "../../../UI/Typography/PageSubtitle";
import PdfPreviewModal from "../../../UI/Modal/PdfPreviewModal/PdfPreviewModal";
import UploadBox from "../../../UI/UploadBox/UploadBox";
import {ALLOWED_PDF_EXTENSIONS, formatDate} from "./AenaTypes";
import DeltioView from "../../../UI/UploadBox/DeltioView";


// ─────────────────────────────────────────────────────────────────────────────

interface Props {
    canActionsA1:boolean;
    activeYear:             UserDeltia[number] | undefined;
    active:                 string;
    activeRole:             string;
    canEdit:                boolean;
    latestUpdateOnPdfs:     string;
    previewUrl:             string | null;
    onClosePreview:         () => void;
    onSetPreviewUrl:        (url: string) => void;
    onDeltioUpload:         (year: string, month: MonthDeltio, file: any) => void;
    onDeltioDelete:         (file_uuid: string) => void;
    onFileLock:             (year: string, monthI: number, isLocked: boolean) => void;
    onMonthPdfDownload:     (monthDeltio: MonthDeltio) => void;
}

const MonthlyDeltiaGrid: React.FC<Props> = ({
    canActionsA1,
                                                activeYear,
                                                active,
                                                activeRole,
                                                canEdit,
                                                latestUpdateOnPdfs,
                                                previewUrl,
                                                onClosePreview,
                                                onSetPreviewUrl,
                                                onDeltioUpload,
                                                onDeltioDelete,
                                                onFileLock,
                                                onMonthPdfDownload,
                                            }) => {
    if (!activeYear?.deltia.length) {
        return <EmptyList>{activeRole !=='dua_user' && <>Παρακαλώ πατήστε <b>Προσθήκη Έτους</b></>}
            {activeRole =='dua_user' && <>Δεν υπάρχουν δεδομένα</>}
            .</EmptyList>;
    }

    return (
        <div className={'mb-5'}>
            {/* Section header */}
            <div className={'mb-5 d-flex justify-content-start gap-2 align-items-center flex-row'}>
                <img className={'me-3'} src={adobe}/>
                <PageSubtitle> <span style={{ color: '--generic-block-border-hover', fontSize: '14px' }}><b>ΥΠΟΓΕΓΡΑΜΜΕΝΑ ΔΕΛΤΙΑ (Α1) {active}</b></span></PageSubtitle>
                <span style={{marginLeft:'20px',marginTop:'5px', color: '#707070', fontSize: '12px' }}>

                    Επικαιροποίηση: <b>{latestUpdateOnPdfs}</b>
                </span>
            </div>

            {previewUrl && (
                <PdfPreviewModal onClose={onClosePreview} pdfUrl={previewUrl}/>
            )}

            <div className={'row g-4'}>
                {activeYear.deltia.map((m) => (
                    <div
                        key={activeYear.year + m.monthI}
                        className={`col-12 col-md-6 col-xl-4 col-xxl-3 order-${m.monthI}`}
                    >
                        <UploadBox
                            accept={ALLOWED_PDF_EXTENSIONS}
                            text={`${activeYear.year} ${m.month}\n${m.projects?.join(', ') ?? ''}`}
                            hasFile={!!(m.file as any)?.uuid}
                            isLocked={m.isLocked || !canActionsA1}
                            onChange={(f) => activeRole !== 'dua_user' && onDeltioUpload(activeYear.year, m, f)}
                            onDelete={() => onDeltioDelete((m.file as any)?.uuid ?? '')}
                            onLock={() => onFileLock(activeYear.year, m.monthI, m.isLocked)}
                            onPreview={() => m.temp_url && onSetPreviewUrl(m.temp_url)}
                            onDownload={() => m.file && onMonthPdfDownload(m)}
                            canEdit={canEdit && canActionsA1}
                        >
                            <DeltioView
                                year={`${activeYear.year} ${m.month}`}
                                project={m.projects?.join(', ') ?? ''}
                                updatedBy={
                                    `${m.updatedBy ?? '--'} ${m.updatedAt ? formatDate(m.updatedAt) : ''}`.trim()
                                }
                                isLocked={m.isLocked || !canActionsA1}
                            />
                        </UploadBox>
                    </div>
                ))}
            </div>
        </div>
    );
};

export default MonthlyDeltiaGrid;
