// Copyright (C) 2024 Robert J.A. Wagner; Am Kiefernwald 49b; D-64297 Darmstadt - All Rights Reserved 

const XFonClick = (ctx) => {
    // only needed for "alertify view"
    let title = ctx.item.FE_Title || ctx.item.FGM_Key;
    XRW.alert.dialog({ tag: "IMG", _src: ctx.arg[ctx.item.FGM_Key] }, { title, ynConfirmOnly: 'Y', ynMaximize: 'Y' });
}

export default class toApp {
    async formCheckImage(arg) {
        this.pageXOffset = XRW.pageXOffset;
        this.pageYOffset = XRW.pageYOffset;
        XRW.mForm = new Map();
        let formCheckImage = XRW.mkForm({ FID: 'formCheckImage', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', renderType: 'Form', pageX: 25, minPixelPerPoint: 20, anchor: 'grForm' });
        formCheckImage.setFEG("UploadImage",
            { FE_Prefix: 'HTML', FGM_Format: 'W25,Vitem,N', html: `<h3>zum Überprüfen des Bildes nach unten scrollen</h3><hr>` },
            { FE_Prefix: 'Image', FGM_Key: 'src', FGM_Format: 'dY1,W25,Vitem,N' },
            { FE_Prefix: 'GenericCheck', FGM_Key: 'ynChecked', FGM_Format: "dY1,f10,dX1,W6,B15,Vitem,N", FE_Title: 'Bild ist OK' },
            { FE_Prefix: 'GenericButton', FGM_Format: "dY1,f10,dX1,W6,Vitem,N", FE_Title: 'Mit der Berarbeitung fortfahren', caption: "Zurück", ynClearCaption: 'N', icon: 'arrowRight', iconColor: 'blue', iconSize_px: 16, SEonClick: `#A; await oApp.formUpload();` },
        );
        formCheckImage.setSection("S1",
            { FEG_Prefix: "UploadImage", FEG_Title: "Bilt überprüfen", FD_ixForm: arg._FIX, ynVolatile: 'Y' },
        );
        await XRW.windowResizeCallBack();
    }
    async formUpload() {
        XRW.mForm = new Map();
        let formUpload = XRW.mkForm({ FID: 'formUpload', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', renderType: 'Form', pageX: 25, minPixelPerPoint: 20, anchor: 'grForm' });
        this.folder = formUpload.folder;
        formUpload.setFEG('HWPA_MRN_Handwerkskarte_Front',
            { FE_Prefix: 'HTML', FE_Title: 'HTML-Text', FGM_Format: 'dY2,dX1,W6,Vitem', html: 'Tragen Sie die Betriebsnummer Ihrer Handwerkskarte hier ein', ynfHidden: 'XRW.LOG_Role!="USER" && XRW.LOG_Role!="EDIT"', ynSkipSave: 'Y', ynfOptional: 'Y' },
            { FE_Prefix: 'GenericUpload', refXForm: "UploadImage", ynMultipleUpload: 'Y', FE_Title: 'In die Fläche unten klicken und Bild auswählen oder Bild auf die Fläche ziehen.', FGM_Format: 'dX1,W23,Vitem,N', select: ['IMG', 'PDF'], caption: 'Upload', FGM_TPos: 8, compress: { quality: 0.8, maxWidth: 600 }, sNotValid: 'Es wurde kein Bild hochgeladen', fileUploadAccept: ".png, .jpg, .jpeg" },
        );
        formUpload.setFEG('UploadImage',
            { FE_Prefix: 'Image', FGM_Key: 'src', FGM_Format: 'dX1,W4,Vitem', SEonClick: `#A; await oApp.formCheckImage(ctx.arg)` },
            { FE_Prefix: 'GenericString', FGM_Key: 'fileName', FGM_Format: 'dX1,W4,Vitem' },
            { FE_Prefix: 'GenericString', FGM_Key: 'type', FGM_Format: 'dX1,W2,Vitem' },
            { FE_Prefix: 'GenericInteger', FGM_Key: 'size', FGM_Format: 'dX1,W2,Vitem' },
            { FE_Prefix: 'GenericCheck', FGM_Key: 'ynChecked', FGM_Format: "dX1,W4,Vitem", ynReadOnly: 'Y', FE_Title: 'Bild ist Geprüft' },
            { FE_Prefix: 'GenericButton', FGM_Key: 'btDelete', FE_Title: 'Löschen', FGM_Format: 'dX1,W1,cText?color=red,Vitem,N', icon: 'delete', SEonClick: '$deleteForm()', FGM_TPos: 4, iconColor: 'red', ynfHidden: "$.FD_ixForm==0 || item.option.ynInSummaryMode=='Y'", ynReadOnly: 'Y', ynSkipSave: 'Y', iconSize_px: 25, ynClearCaption: 'Y', ynSkipOnSummary: 'Y', ynTableStyle: 'Y' },
            // { FE_Prefix: 'GenericInteger', FGM_Key: 'wx', FGM_Format: 'dX1,W6,Vitem' },
            // { FE_Prefix: 'GenericInteger', FGM_Key: 'wy', FGM_Format: 'dX1,W6,Vitem, N' },
        );
        formUpload.setSection("S1",
            { FEG_Prefix: "HWPA_MRN_Handwerkskarte_Front", FEG_Title: "Upload", ynVolatile: 'Y' },
            { FEG_Prefix: "UploadImage", FEG_Title: "Upload", ynVolatile: 'Y', ynAllowEmptyArg: 'Y' }
        );
        await XRW.windowResizeCallBack();
        if (this.pageYOffset)window.scrollBy(this.pageXOffset, this.pageYOffset);
    }
    async Start() {
        this.folder = XRW.mkFolder();
        this.pageXOffset = null;
        this.pageYOffset = null;
        await this.formUpload();
    }
}
globalThis.oApp = new toApp();
