{
  "data": {
    "user": {
      "id": "",
      "name": "",
      "token": ""
    }
  },
  "on": {
    "resize": [],
    "hashchange": [
      {
        "log": "hashchange"
      },
      {
        "browser:pageFromHash": {
          "pagesClass": "page",
          "pageChanger": "rl:openPage"
        }
      }
    ],
    "init": [
      {
        "if": {
          "is": "{local:preferences}",
          "then": [
            {
              "set": {
                "data preferences": "{local:preferences}"
              }
            }
          ],
          "else": [
            {
              "local set": {
                "preferences": "{data preferences}"
              }
            }
          ]
        }
      },
      {
        "set": {
          "var saveMethod": "POST"
        }
      },
      {
        "loadTemplate": {
          "selector": "body",
          "url": "./assets/templates/menu.html",
          "to": "template menu",
          "arguments": {},
          "callback": []
        }
      },
      {
        "rl:menuIcons": {
          "selector": "body",
          "items": [
            {
              "title": "Buildings",
              "icon": "rl:menu",
              "link": "#building"
            },
            {
              "title": "Floors",
              "icon": "rl:floors",
              "link": "#floor"
            },
            {
              "title": "Zones",
              "icon": "rl:zones",
              "link": "#zone"
            },
            {
              "title": "Operators",
              "icon": "rl:operators",
              "link": "#operator"
            },
            {
              "title": "Subjects",
              "icon": "rl:subjects",
              "link": "#subject"
            },
            {
              "title": "Alarms",
              "icon": "rl:alarms",
              "link": "#alarm"
            },
            {
              "title": "Gateways",
              "icon": "rl:gateways",
              "link": "#gateway"
            },
            {
              "title": "Trackers",
              "icon": "rl:trackers",
              "link": "#tracker"
            },
            {
              "title": "Tracks",
              "icon": "rl:tracks",
              "link": "#track"
            },
            {
              "title": "Settings",
              "icon": "rl:settings",
              "link": "#setting"
            },
            {
              "title": "Tracker Zone",
              "icon": "rl:trackerZone",
              "link": "#trackerZone"
            }
          ]
        }
      },
      {
        "fetchJson": {
          "url": "./assets/api/reslevis.api-1.0.5.json",
          "to": "data api",
          "options": {
            "method": "GET",
            "headers": {
              "Content-Type": "application/json",
              "Authorization": ""
            }
          },
          "success": [
            {
              "log": "API {data api info version} loaded"
            },
            {
              "rl:checkAuth": {}
            }
          ],
          "error": [
            {
              "log": "Error loading reslevis.api-1.0.5.json"
            },
            {
              "rl:checkAuth": {}
            }
          ]
        }
      }
    ]
  },
  "plugins": [
    {
      "name": "app",
      "version": "",
      "ondemand": false,
      "files": [
        {
          "type": "link",
          "url": "app.css"
        },
        {
          "type": "script",
          "url": "app.js"
        },
        {
          "type": "script",
          "url": "api.js"
        }
      ]
    },
    {
      "name": "tablestack",
      "description": "Headless UI for building powerful tables & datagrids",
      "version": "8.21.3",
      "ondemand": false,
      "files": [
        {
          "type": "script",
          "url": "assets/plugins/tablestack.min.js",
          "cdn": "https://unpkg.com/@tanstack/table-core@8.11.6/build/umd/index.production.js"
        }
      ]
    },
    {
      "name": "alpinejs",
      "description": "The rugged, minimal JavaScript framework",
      "version": "3.x.x",
      "ondemand": false,
      "files": [
        {
          "type": "script",
          "url": "assets/plugins/alpinejs.min.js"
        }
      ]
    },
    {
      "name": "simplebar",
      "description": "Scrollbars, simpler",
      "version": "6.2.7",
      "ondemand": false,
      "files": [
        {
          "type": "link",
          "url": "assets/plugins/simplebar.css"
        },
        {
          "type": "script",
          "url": "assets/plugins/simplebar.min.js"
        }
      ]
    },
    {
      "name": "flatpickr",
      "description": " A lightweight, powerful javascript datetime picker",
      "version": "4.6.13",
      "ondemand": false,
      "files": [
        {
          "type": "link",
          "url": "assets/plugins/flatpickr.min.css"
        },
        {
          "type": "script",
          "url": "assets/plugins/flatpickr.min.js"
        }
      ]
    },
    {
      "name": "sweetalert2",
      "description": "A beautiful, responsive, customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes",
      "version": "11.17.2",
      "ondemand": false,
      "files": [
        {
          "type": "script",
          "url": "assets/plugins/sweetalert2@11.js"
        }
      ]
    },
    {
      "name": "snapsvg",
      "description": "JavaScript Vector Library",
      "type": "script",
      "version": "0.5.1",
      "ondemand": false,
      "url": "assets/plugins/snap.svg-min.js"
    },
    {
      "name": "qrcode-svg",
      "description": "A simple QR Code generator in pure JavaScript",
      "type": "script",
      "version": "1.1.0",
      "ondemand": false,
      "url": "assets/plugins/qrcode.min.js",
      "ref": "https://papnkukn.github.io/qrcode-svg/"
    }
  ],
  "plugins-disabled": [],
  "setup": {
    "languages": [
      "en"
    ],
    "language": "en",
    "log": false,
    "modules": [
      {
        "name": "reslevisData",
        "url": "assets/modules/reslevis.config.json"
      },
      {
        "name": "reslevisIcons",
        "url": "assets/modules/reslevis.icons.json"
      },
      {
        "name": "reslevisData",
        "url": "assets/modules/reslevis.data.json"
      },
      {
        "name": "reslevisTexts",
        "url": "assets/modules/reslevis.texts.json"
      }
    ]
  },
  "parts": {
    "rl": {
      "checkAuth": [
        {
          "if": {
            "is": "{data settings authRequired}",
            "then": [
              {
                "getToken": {
                  "url": "{data settings serverTokenUrl}",
                  "options": {
                    "method": "POST",
                    "headers": {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    "body": "grant_type=client_credentials&client_id=Fastapi&client_secret={data settings serverClientSecret}"
                  },
                  "success": [
                    {
                      "rl:createDashboard": {
                        "page": "home",
                        "db": "alarm"
                      }
                    },
                    {
                      "rl:createPages": {}
                    }
                  ],
                  "error": [
                    {
                      "rl:createDashboard": {
                        "page": "home",
                        "db": "alarm"
                      }
                    },
                    {
                      "rl:createPages": {}
                    }
                  ]
                }
              }
            ],
            "else": [
              {
                "rl:createDashboard": {
                  "page": "home",
                  "db": "alarm"
                }
              },
              {
                "rl:createPages": {}
              }
            ]
          }
        }
      ],
      "getToken": [
        {
          "log": "getToken from {arguments:serverTokenUrl}"
        },
        {
          "fetchJson": {
            "to": "data user token",
            "url": "{arguments:serverTokenUrl}",
            "options": {
              "method": "POST",
              "headers": {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              "body": "grant_type=client_credentials&client_id=Fastapi&client_secret=wojuoB7Z5xhlPFrF2lIxJSSdVHCApEgC"
            },
            "success": [
              {
                "log": "token ok"
              }
            ],
            "error": [
              {
                "log": "Error fetching auth token"
              }
            ]
          }
        }
      ],
      "createPages": {
        "do": [
          {
            "log": "Create pages"
          },
          {
            "for": {
              "var": "db",
              "of": [
                "building",
                "floor",
                "zone",
                "operator",
                "subject",
                "gateway",
                "tracker",
                "trackerZone",
                "track",
                "fingerprint",
                "setting"
              ],
              "do": [
                {
                  "html": [
                    {
                      "selector": ".main",
                      "tag": "div",
                      "attr": {
                        "id": "page{var db}",
                        "data-value": "{var db}",
                        "class": "page hidden"
                      },
                      "text": ""
                    }
                  ]
                },
                {
                  "if": {
                    "is": "'{data settings localDb}' == '1'",
                    "then": [
                      {
                        "rl:getDb": {
                          "template": "{var db}",
                          "db": "{var db}",
                          "serverMethod": "GET",
                          "localDb": "{data settings localDb}",
                          "localPath": "{data settings localPath}",
                          "token": "{data user token}"
                        }
                      }
                    ],
                    "else": [
                      {
                        "apiGet": {
                          "db": "{var db}",
                          "template": "{var db}",
                          "method": "GET",
                          "success": [
                            {
                              "log": "apiCall get success"
                            }
                          ],
                          "error": [
                            {
                              "log": "apiCall get error"
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          },
          {
            "browser:pageFromHash": {
              "pagesClass": "page",
              "pageChanger": "rl:openPage"
            }
          }
        ]
      },
      "createDashboard": {
        "do": [
          {
            "log": "Create page {arguments:page}"
          },
          {
            "html": [
              {
                "selector": ".main",
                "tag": "div",
                "attr": {
                  "id": "page{arguments:page}",
                  "data-value": "{arguments:page}",
                  "class": "page hidden"
                },
                "text": ""
              }
            ]
          },
          {
            "rl:getDb": {
              "template": "{arguments:page}",
              "db": "{arguments:db}",
              "serverMethod": "GET",
              "localDb": "{data settings localDb}",
              "localPath": "{data settings localPath}",
              "token": "{data user token}"
            }
          },
          {
            "browser:pageFromHash": {
              "pagesClass": "page",
              "pageChanger": "rl:openPage"
            }
          }
        ]
      },
      "apiPostCallback": {
        "do": [
          {
            "log": "apiPostCallback"
          },
          {
            "log": "{arguments:data}"
          },
          {
            "if": {
              "is": "{arguments:data detail 0}",
              "then": [
                {
                  "log": "apiPostCallback error"
                },
                {
                  "alert": "arguments:data detail 0 msg"
                }
              ],
              "else": [
                {
                  "log": "apiPostCallback success"
                }
              ]
            }
          },
          {
            "apiGet": {
              "db": "{arguments:db}",
              "template": "{arguments:db}",
              "method": "GET",
              "success": [
                {
                  "log": "apiCall get success"
                }
              ],
              "error": [
                {
                  "log": "apiCall get error"
                }
              ]
            }
          }
        ]
      },
      "apiUpdateCallback": {
        "do": [
          {
            "log": "apiUpdateCallback"
          },
          {
            "log": "{arguments:data}"
          }
        ]
      },
      "apiDeleteCallback": {
        "do": [
          {
            "log": "apiDeleteCallback"
          },
          {
            "log": "{arguments:data}"
          }
        ]
      },
      "apiGetCallback": {
        "do": [
          {
            "log": "apiGetCallback"
          },
          {
            "log": "{arguments:data}"
          },
          {
            "set": {
              "data db {arguments:db}": "{arguments:data}"
            }
          },
          {
            "loadTemplate": {
              "selector": "#page{arguments:template}",
              "url": "./assets/templates/{arguments:template}.html",
              "to": "template {arguments:template}",
              "empty": true,
              "arguments": {
                "db": "{arguments:db}",
                "info": "{texts info{arguments:db}s}",
                "serverUrl": "{arguments:serverUrl}",
                "new": "rl:newPost"
              },
              "callback": []
            }
          }
        ]
      },
      "updateDb": {
        "do": [
          {
            "set": {
              "var dbUrl": [
                "{data api servers {arguments:localDb} url}/get{arguments:db}s",
                "{arguments:localPath}/{arguments:db}.json"
              ],
              "var token": "{data user token}"
            }
          },
          {
            "log": "DB update:  {var dbUrl {arguments:localDb}}"
          },
          {
            "fetchJson": {
              "to": "data db {arguments:db}",
              "url": "{var dbUrl {arguments:localDb}}",
              "options": {
                "method": "GET",
                "headers": {
                  "Content-Type": "application/json",
                  "Authorization": "Bearer {var token}"
                }
              },
              "success": [
                {
                  "js": "{document.querySelector('.update{arguments:db}').click()}"
                }
              ]
            }
          }
        ]
      },
      "getDb": {
        "do": [
          {
            "set": {
              "var infoText": "{texts info{arguments:db}s}"
            }
          },
          {
            "log": "getDb:  {var dbUrl {arguments:localDb}}"
          },
          {
            "fetchJson": {
              "to": "var getResult",
              "url": "{arguments:localPath}/{arguments:db}.json",
              "options": {
                "method": "GET",
                "headers": {
                  "Content-Type": "application/json",
                  "Authorization": "Bearer {var token}"
                }
              },
              "success": [
                {
                  "set": {
                    "data db {arguments:db}": "{var getResult}"
                  }
                },
                {
                  "loadTemplate": {
                    "selector": "#page{arguments:template}",
                    "url": "./assets/templates/{arguments:template}.html",
                    "to": "template {arguments:template}",
                    "empty": true,
                    "arguments": {
                      "db": "{arguments:db}",
                      "info": "{texts info{arguments:db}s}",
                      "serverUrl": "{data settings serverUrl}",
                      "new": "rl:newPost"
                    },
                    "callback": []
                  }
                }
              ],
              "errorDisabled": [
                {
                  "log": "Error loading {var dbUrl {arguments:localDb}}"
                },
                {
                  "log": "{var getResult}"
                }
              ]
            }
          }
        ]
      },
      "openPage": {
        "arguments": "(page, path)",
        "do": [
          {
            "if2": {
              "is": "'{arguments:page}' === 'home'",
              "then": [
                {
                  "attr": {
                    "selector": ".menuleft",
                    "addClass": "hidden"
                  }
                }
              ],
              "else": [
                {
                  "attr": {
                    "selector": ".menuleft",
                    "removeClass": "hidden"
                  }
                }
              ]
            }
          },
          {
            "if": {
              "is": "'{arguments:page}' === ''",
              "then": [
                {
                  "link": "#home"
                }
              ]
            }
          },
          {
            "set": {
              "var infoText": "{texts info{arguments:page}}"
            }
          },
          {
            "html": [
              {
                "selector": ".infoText",
                "text": "{var infoText}",
                "empty": true
              }
            ]
          },
          {
            "log": "openPage: {var infoText}"
          },
          {
            "if": {
              "is": "{arguments:page} !== {var actualPage}",
              "then": [
                {
                  "set": {
                    "var actualPage": "{arguments:page}"
                  }
                }
              ],
              "else": [
                {
                  "rl:closePost": {
                    "db": "{arguments:page}"
                  }
                },
                {
                  "clearInterval": {
                    "name": "update"
                  }
                },
                {
                  "setInterval2": {
                    "name": "update",
                    "duration": 5000,
                    "do": [
                      {
                        "apiGet": {
                          "db": "{arguments:page}",
                          "template": "{arguments:page}",
                          "method": "GET",
                          "success": [
                            {
                              "log": "apiCall get success"
                            }
                          ],
                          "error": [
                            {
                              "log": "apiCall get error"
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        ]
      },
      "updateTable": {
        "do": [
          {
            "set": {
              "var dbUrl": [
                "{data api servers {arguments:localDb} url}/get{arguments:db}s",
                "{arguments:localPath}/{arguments:db}.json"
              ],
              "var token": "{data user token}"
            }
          },
          {
            "log": "DB source:  {var dbUrl {arguments:localDb}}"
          },
          {
            "fetchJson": {
              "to": "data db {arguments:db}",
              "url": "{var dbUrl {arguments:localDb}}",
              "options": {
                "method": "POST",
                "headers": {
                  "Content-Type": "application/json",
                  "Authorization": "Bearer {var token}"
                },
                "body": ""
              },
              "success": [
                {
                  "loadTemplate": {
                    "selector": "#page{arguments:db}",
                    "url": "./assets/templates/{arguments:db}Table.html",
                    "to": "template {arguments:db}",
                    "empty": true,
                    "arguments": {
                      "db": "{arguments:db}",
                      "info": "{var infoText}",
                      "serverUrl": "{data settings serverUrl}",
                      "new": "rl:newPost"
                    },
                    "callback": []
                  }
                }
              ]
            }
          }
        ]
      },
      "openPost": {
        "do": [
          {
            "set": {
              "var saveMethod": "{arguments:method}"
            }
          },
          {
            "set": {
              "var schemaItem": "{data api components schemas {arguments:db}Item}"
            }
          },
          {
            "set": {
              "var schemaItem": "{data api components schemas {arguments:db}Item}"
            }
          },
          {
            "loadTemplate": {
              "selector": ".page[data-value={arguments:db}] .post",
              "url": "./assets/templates/post.html",
              "to": "template post",
              "empty": true,
              "arguments": {
                "db": "{arguments:db}",
                "id": "{arguments:fields id}",
                "fields": "{arguments:fields}",
                "method": "{arguments:method}",
                "serverUrl": "{data settings serverUrl}",
                "close": "rl:closePost",
                "remove": "rl:removePost",
                "save": "rl:savePost"
              },
              "callback": [
                {
                  "html": [
                    {
                      "selector": ".cancelButton",
                      "on": {
                        "mousedown": [
                          {
                            "rl:closePost": {
                              "db": "{arguments:db}"
                            }
                          }
                        ]
                      }
                    }
                  ]
                },
                {
                  "createForm": {
                    "selector": ".page[data-value={arguments:db}] .fields",
                    "serverUrl": "{data settings serverUrl}",
                    "db": "{arguments:db}",
                    "name": "{arguments:db}",
                    "schema": "{var schemaItem}",
                    "fields": "{arguments:fields}",
                    "error": [
                      {
                        "log": "error"
                      }
                    ],
                    "success": [
                      {
                        "log": "success"
                      }
                    ],
                    "callback": []
                  }
                }
              ]
            }
          },
          {
            "attr": {
              "selector": ".page[data-value={arguments:db}] .post",
              "removeClass": "hidden"
            }
          }
        ]
      },
      "newPost": {
        "note": "https://developer.mozilla.org/en-US/docs/Glossary/UUID",
        "do": [
          {
            "set": {
              "var newId": "{js:window.crypto.randomUUID();}"
            }
          },
          {
            "rl:openPost": {
              "db": "{arguments:db}",
              "method": "POST",
              "id": "{var newId}",
              "fields": {
                "id": "{var newId}"
              }
            }
          }
        ]
      },
      "closePost": {
        "do": [
          {
            "attr": {
              "selector": ".page[data-value={arguments:db}] .post",
              "addClass": "hidden"
            }
          }
        ]
      },
      "savePost": {
        "do": [
          {
            "log": "savePost:{arguments:name}"
          },
          {
            "set": {
              "var formData": "{formDataToJson:.form{arguments:name}}"
            }
          },
          {
            "apiPost": {
              "db": "{arguments:name}",
              "method": "{var saveMethod}",
              "item": "{var formData}",
              "success": [
                {
                  "log": "apiPost {var saveMethod} success"
                }
              ],
              "error": [
                {
                  "log": "apiPost {var saveMethod} error"
                }
              ]
            }
          }
        ]
      },
      "removePost": {
        "do": [
          {
            "log": "removePost:{arguments:name}"
          },
          {
            "apiPost": {
              "db": "{arguments:name}",
              "id": "{arguments:id}",
              "method": "DELETE",
              "success": [
                {
                  "log": "apiPost DELETE success"
                }
              ],
              "error": [
                {
                  "log": "apiPost DELETE error"
                }
              ]
            }
          }
        ]
      },
      "menuIcons": {
        "do": [
          {
            "html": [
              {
                "selector": "body",
                "tag": "div",
                "attr": {
                  "class": "menuleft float-left w-[200px] hidden lg:block"
                }
              },
              {
                "selector": "body",
                "tag": "div",
                "attr": {
                  "class": "main h-screen h-full"
                },
                "comment": [
                  {
                    "tag": "div",
                    "attr": {
                      "class": "header flex align-items-center justify-content-center text-center float-wrap"
                    },
                    "html": [
                      {
                        "tag": "div",
                        "attr": {
                          "class": "logo m-[10px]"
                        },
                        "html": [
                          {
                            "tag": "div",
                            "attr": {
                              "class": "w-[120px] float-left"
                            },
                            "html": [
                              {
                                "tag": "a",
                                "attr": {
                                  "href": "#home"
                                },
                                "html": [
                                  {
                                    "tag": "img",
                                    "attr": {
                                      "class": "",
                                      "src": "./assets/images/logo-reslevis.svg"
                                    }
                                  }
                                ]
                              }
                            ]
                          }
                        ]
                      },
                      {
                        "for": {
                          "var": "item",
                          "of": "{arguments:items}",
                          "html": [
                            {
                              "tag": "div",
                              "attr": {
                                "class": "p-[20px] text-center"
                              },
                              "html": [
                                {
                                  "tag": "a",
                                  "attr": {
                                    "href": "{var item link}"
                                  },
                                  "html": [
                                    {
                                      "tag": "span",
                                      "attr": {
                                        "class": "iconify text-[60px] bg-[transparent] text-[#008EED]",
                                        "data-icon": "{var item icon}"
                                      }
                                    }
                                  ]
                                },
                                {
                                  "html": [
                                    {
                                      "tag": "a",
                                      "attr": {
                                        "href": "{var item link}"
                                      }
                                    },
                                    {
                                      "tag": "span",
                                      "text": "{var item title}",
                                      "attr": {
                                        "class": "text-[#008EED] text-[16px] px-[0px] py-[10px]"
                                      }
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "loadTemplate": {
              "selector": ".menuleft",
              "url": "./assets/templates/menuleft.html",
              "to": "template menuleft",
              "arguments": {},
              "callback": []
            }
          }
        ]
      }
    }
  },
  "functions": {
    "apiGet": {
      "js": "let args = arguments[0]; console.log('UI.apiGet'); UI.apiGet(args);"
    },
    "apiPost": {
      "js": "let args = arguments[0]; console.log('UI.apiPost'); UI.apiPost(args);"
    },
    "apiCall": {
      "js": "let args = arguments[0]; console.log('UI.apiCall'); UI.apiCall(args);"
    },
    "getToken": {
      "js": "let args = arguments[0]; UI.getToken(args);"
    },
    "createTable": {
      "js": "let args = arguments[0]; console.log('createTable'); console.log(args); UI.createTable(args); js.callback({do:args.callback});"
    },
    "createForm": {
      "js": "let args = arguments[0]; /*console.log('createForm'); console.log(args);*/ UI.createForm(args); js.callback({do:args.callback});"
    },
    "loadTemplate": {
      "js": "let args = arguments[0]; /*console.log('loadTemplate'); console.log(args);*/ UI.loadTemplate(args); js.callback({do:args.callback});"
    }
  }
}