info:
  name: elixPro - Template Delibera ONLYTABLE
  type: http
  seq: 29

http:
  method: POST
  url: "{{elixFormsApiUrl}}/eftl/process/v1"
  headers:
    - name: x-requested-with
      value: XMLHttpRequest
    - name: x-api-key
      value: "{{elixFormsWsAuthenticationToken}}"
    - name: x-ef-request-id
      value: "31630"
      disabled: true
    - name: x-ef-request-id
      value: "31499"
      disabled: true
    - name: x-ef-request-id
      value: "31801"
  body:
    type: json
    data: |-
      {
        "userDocument": "{{elixBase64EftlDocument}}", // see the pre-request script
        "userContext": {
          "lang": "IT",
          "crlf": "\r\n"
        }
      }
  auth: inherit

runtime:
  scripts:
    - type: before-request
      code: |-
        const { prepareEftlDocument } = require("./elixFormsJs.js")
        // Copy-pasta your EFTL document below inside the template literals (`)
        prepareEftlDocument(String.raw`
        [EFTL]

        [VAR name="codiciFiscaliIterable" type="iterable"][SPLIT regex="##"][TAG]SCHEMAID,862,COL0001,IUQOID, ,##[/TAG][/SPLIT][/VAR]
        [VAR name="importiIterable" type="iterable"][SPLIT regex="##"][TAG]SCHEMAID,862,COL0002,IUQOID, ,##[/TAG][/SPLIT][/VAR]
        [VAR name="anniIterable" type="iterable"][SPLIT regex="##"][TAG]SCHEMAID,862,COL0005,IUQOID, ,##[/TAG][/SPLIT][/VAR]
        [VAR name="modalitaIterable" type="iterable"][SPLIT regex="##"][TAG]SCHEMAID,862,COL0003,IUQOID, ,##[/TAG][/SPLIT][/VAR]
        [VAR name="insertCount" type="number"][SIZE_OF varname="codiciFiscaliIterable" /][/VAR]
        [VAR name="datiRipartizioneIterable" type="iterable"][SPLIT regex="##" emptyIfBlank="true"][TAG]GETVALUEBYTAG,RIPARTIZIONE_DATI_COMPLETI,REQUEST,IUQOID,CONCAT,##[/TAG][/SPLIT][/VAR]

        [VAR name="item1" type="number"][% item1 = 0; %][/VAR]
        [VAR name="item2" type="number"][% item2 = 1; %][/VAR]
        [VAR name="item3" type="number"][% item3 = 2; %][/VAR]
        [VAR name="item4" type="number"][% item4 = 3; %][/VAR]
        [VAR name="item5" type="number"][% item5 = 4; %][/VAR]

        <table style='width: 95%; margin: 0 auto;'>
          <tr>
            <th>Nominativo</th>
            <th>Codice fiscale</th>
            <th>Qualifica</th>
            <th>Struttura</th>
            <th></th>
            <th>Importo (&euro;)</th>
          </tr>
        [FOR varName="ripartizione" iterable="datiRipartizioneIterable"]
          [VAR name="datiIterable" type="iterable"][SPLIT regex=";;" emptyIfBlank="true"][VALUE_OF varname="ripartizione" /][/SPLIT][/VAR]
          [VAR name="codiceFiscaleCorrente"][VALUE_OF varname="datiIterable" index="item2" /][/VAR]
          <tr>
            <td style='text-align: left;'>[VALUE_OF varname="datiIterable" index="item1" /]</td>
            <td>[VALUE_OF varname="datiIterable" index="item2" /]</td>
            <td>[VALUE_OF varname="datiIterable" index="item3" /]</td>
            <td>[VALUE_OF varname="datiIterable" index="item4" /]</td>
            <td></td>
            <td style='text-align: right;'>[FORMAT type="number" pattern="#,##0.00"][VALUE_OF varname="datiIterable" index="item5" /][/FORMAT]</td>
          </tr>
          [% insertIdx = 0; firstCfMatch = true; %]
          [WHILE threshold="99"]
            [CONDITION][% insertIdx < insertCount %][/CONDITION]
            [DO]
              [VAR name="codiceFiscale"][VALUE_OF varname="codiciFiscaliIterable" index="insertIdx" /][/VAR]
              [VAR name="anno"][VALUE_OF varname="anniIterable" index="insertIdx" /][/VAR]
              [VAR name="modalita"][VALUE_OF varname="modalitaIterable" index="insertIdx" /][/VAR]
              [VAR name="importo"][VALUE_OF varname="importiIterable" index="insertIdx" /][/VAR]
              [IF]
                [CONDITION][% codiceFiscale == codiceFiscaleCorrente && importo != "" && importo != 0 %][/CONDITION]
                [THEN]
                  [% modalitaOut = ""; annoOut = ""; suddivisoOut = ""; %]
                  [IF][CONDITION][% modalita != "" %][/CONDITION][THEN][% modalitaOut = modalita + " "; %][/THEN][/IF]
                  [IF][CONDITION][% anno != "" %][/CONDITION][THEN][% annoOut = "nel " + anno; %][/THEN][/IF]
                  [IF][CONDITION][% firstCfMatch == true %][/CONDITION][THEN][% suddivisoOut = "suddiviso come "; %][/THEN][/IF]
          <tr>
            <td colspan="4" style='text-align: right;'>[%= suddivisoOut %]</td>
            <td style='text-align: right;'>[%= modalitaOut %][%= annoOut %]</td>
            <td style='text-align: right;'>[FORMAT type="number" pattern="#,##0.00"][VALUE_OF varname="importo" /][/FORMAT]</td>
          </tr>
                  [IF][CONDITION][% firstCfMatch == true %][/CONDITION][THEN][% firstCfMatch = false; %][/THEN][/IF]
                [/THEN]
              [/IF]
              [% insertIdx = insertIdx + 1; %]
            [/DO]
          [/WHILE]

        [/FOR]
        </table>

        [/EFTL]
        `);
    - type: after-response
      code: |-
        var status = res.status;

        if (status !== 200) {
          console.log("ERRORE HTTP! Response status code: " + status);
          return;
        }

        var decodedDocument = "";
        var response = res.getBody();

        if (response.value.code === "ERROR") {
          decodedDocument = "<h1>ERRORE EFTL!</h1>";
          decodedDocument += "<b>" + response.value.description.split("\n").slice(0, 3).join("\n") + "</b>";
        }
        else if (response.value.processedDocument === "") {
          decodedDocument = "<h1>EMPTY OUTPUT!</h1>";
          decodedDocument += "<b>" + response.value.description.split("\n").slice(0, 3).join("\n") + "</b>";
        }
        else {
          decodedDocument = atob(response.value.processedDocument);
        }

        bru.visualize('html', {
          name: 'HTML',
          content: decodedDocument
        });

settings:
  encodeUrl: true
  timeout: 0
  followRedirects: true
  maxRedirects: 5
  forwardAuthorizationHeader: true
