// Copyright (C) 2024 Robert J.A. Wagner; Am Kiefernwald 49b; D-64297 Darmstadt - All Rights Reserved 
const FGM_Format_Header = `dY10,
dX20,f40,W140,Sbold,p?T=10,cText?hex=#05b0c6,Vconst?text=Rechnung,
W30,H30,Vimage?path=${encodeURI(`./supply/img/hwk_logo.png`)},N,
dY15,dX20,W170,B8,cDraw?hex=#05b0c6,Vempty,N,
dY8,dX20,Sbold,p?T=2&R=2,f10,W80,cText?hex=#000000,Vconst?text=${encodeURI(`Beispiel Firma`)},N,
dX20,Sitalic,p?T=2&R=2,f8,W80,cText?hex=#000000,Vconst?text=${encodeURI(`Am Wasser\n12345 Am Wasser\nTelefon: 020 123 45 67`)},N,
dY8,dX20,Sbold,p?T=2&R=2,f10,W80,cText?hex=#000000,Vconst?text=${encodeURI(`Beispiel Kunde`)},N,
dX20,Sitalic,p?T=1&R=2,f8,W80,cText?hex=#000000,Vconst?text=${encodeURI(`Am Berg 10\n98765 Am Berg\nDeutschland`)},N`;


export default class toApp {
    formFrame() {
        let formFrame = XRW.mkForm({ FID: 'formFrame', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', ynSkipRedrawOnChange: 'N', renderType: 'Form', pageX: 215, minPixelPerPoint: 2, anchor: 'grForm' });
        formFrame.setFEG('Frame',
            { FE_Prefix: 'HTML', FGM_Format: "dX1,W215,p?L=0.5,Vitem,N,dY1", html: '<h3>Ihr Einkaufswagen</h3>', class: 'w3-card-2' }, // , ynfHidden: hoNotUSER 
            { FE_Prefix: 'GenericButton', FGM_Key: 'btAdd', FE_Title: 'Hinzufügen', FGM_Format: 'dX1,W5,Vitem', icon: 'add', SEonClick: `$newForm("Product");XRW.message("Form created");`, 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: 'Render', FGM_Format: `dX5,Sbold,p?B=5,f20,W80,cText?hex=#000000,Vconst?text=${encodeURI('Artikel hinzufügen')},N,dY5`, ynSkipSave: 'Y', ynfOptional: 'Y' },
            { FE_Prefix: 'Render', FGM_Format: "dX5,W100,B15,Vitem", id: "grBoxForm" },
            { FE_Prefix: 'Render', FGM_Format: "dX5,W100,B15,Vitem,N", id: "grBoxPDF" },
        );
        formFrame.setSection("S1", { FEG_Prefix: "Frame", FEG_Title: "Demo", ynVolatile: 'Y' });
    }
    formProduct() {
        let formProduct = XRW.mkForm({ FID: 'formProduct', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', renderType: 'Form', pageX: 210, minPixelPerPoint: 2, anchor: 'grBoxForm' });
        formProduct.setFEG('Product',
            { FE_Prefix: 'GenericInteger', FGM_Key: "Pos", SEautoValue: `$._FIX` }, // use intenal (automatc) index
            { FE_Prefix: 'GenericString', FGM_Format: "dX5,W80,Vitem", FGM_Key: "Artikel", FE_Title: "Artikel" },
            { FE_Prefix: 'GenericInteger', FGM_Format: "dX5,W30,Vitem", FGM_Key: "Menge", FE_Title: "Menge" },
            { FE_Prefix: 'GenericFloat', FGM_Format: "dX5,W40,Vitem", FGM_Key: "Einzelpreis", FE_Title: "Einzelpreis" },
            { FE_Prefix: 'GenericFloat', FGM_Key: "Gesamtpreis", SEautoValue: `$.Menge*$.Einzelpreis` },
            { FE_Prefix: 'GenericButton', FGM_Key: 'btDelete', FE_Title: 'Löschen', FGM_Format: 'dX5,cText?r=200,W10,Vitem,N', icon: 'delete', SEonClick: '$deleteForm()', FGM_TPos: 4, ynfHidden: "$.FD_ixForm==0 || item.option.ynInSummaryMode=='Y'", ynReadOnly: 'Y', ynSkipSave: 'Y', iconSize_px: 25, ynClearCaption: 'Y', ynSkipOnSummary: 'Y', ynTableStyle: 'Y' },
        );
        formProduct.setSection("S1", { FEG_Prefix: "Product", FEG_Title: "Demo", ynVolatile: 'Y' });
    }
    formPDF() {

        let formPDF = XRW.mkForm({ FID: 'formPDF', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', renderType: 'PDF', delayRender: 3000, pageX: 210, minPixelPerPoint: 2, anchor: 'grBoxPDF' });
        formPDF.setFEG('Header',
            { FE_Prefix: 'Render', FGM_Format: FGM_Format_Header, ynSkipSave: 'Y', ynfOptional: 'Y' },
            { FE_Prefix: 'GenericString', FGM_Format: `dX4,dX20,p?T=1&R=2,f8,W80,cText?hex=#8c898a,Vitem,N,dY5`, defaultValue: `Bspw. Informationen zu Zahlungsbedingungen.` },
        );
        formPDF.setFEG('Product',
            { FE_Prefix: 'GenericInteger', FGM_Format: 'dY10,dX5,f8,W10,Vitem', FGM_Key: "Pos" },
            { FE_Prefix: 'GenericString', FGM_Format: 'dX5,f8,W60,Vitem', FGM_Key: 'Artikel' },
            { FE_Prefix: 'GenericInteger', FGM_Format: "dX5,f8,W15,Aright,Vitem", FGM_Key: "Menge" },
            { FE_Prefix: 'GenericString', FGM_Format: "dX5,f8,W25,Aright,Vitem", SEautoValue: `$toFixed($.Einzelpreis, 2)` },
            { FE_Prefix: 'GenericString', FGM_Format: "dX5,f8,W30,Aright,Sbold,Vitem,N", SEautoValue: `$toFixed($.Gesamtpreis, 2)` },
        );
        formPDF.setFEG('Footer',
            { FE_Prefix: 'GenericFloat', FGM_Key: 'TotalNetto', SEautoValue: `$addValues("Product", "Gesamtpreis")` },
            { FE_Prefix: 'GenericFloat', FGM_Key: 'MWST', SEautoValue: `$.TotalNetto * 0.19` },
            { FE_Prefix: 'GenericFloat', FGM_Key: 'TotalBrutto', SEautoValue: `$.TotalNetto + $.MWST` },
            { FE_Prefix: 'GenericString', FGM_Format: `dY10,dX110,f8,W20,Vitem`, defaultValue: `Netto:\nMwSt.:\nBrutto:` },
            { FE_Prefix: 'GenericString', FGM_Format: `dX5,f8,W30,Aright,Sbold,Vitem,N`, SEautoValue: `$toFixed($.TotalNetto, 2) + "\\n" + $toFixed($.MWST, 2) + "\\n" + $toFixed($.TotalBrutto, 2)` },
        );
        formPDF.setSection("S1",
            { FEG_Prefix: "Header", FEG_Title: "Demo", ynVolatile: 'Y', ynAllowEmptyArg: 'N' },
            { FEG_Prefix: "Product", FEG_Title: "Demo", ynVolatile: 'Y', ynAllowEmptyArg: 'Y' },
            { FEG_Prefix: "Footer", FEG_Title: "Demo", ynVolatile: 'Y', ynAllowEmptyArg: 'N' },
        );
    }
    async Start() {
        this.folder = XRW.mkFolder();
        this.folder.setArg('Product', { Artikel: "ACME Taschenrakete", Menge: 17, Einzelpreis: 145.30 });   // optionally create one product to start
        this.formFrame();
        this.formProduct();
        this.formPDF();
        await XRW.windowResizeCallBack();

    }
}
globalThis.oApp = new toApp();
