<?xml version="1.0" encoding="utf-8" ?>
<!-- -->
<!-- (c) Copyright Ascensio System SIA 2024 -->
<!-- -->
<odoo>
  <template id="eurooffice_layout" name="Euro-Office Editor">
    <html>
      <head>
        <meta charset="UTF-8" />
        <title> Euro-Office - <t t-out="docTitle" /> </title>
        <link rel="icon" t-att-href="docIcon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <t t-call-assets="web.assets_frontend" t-js="false" />
        <style>
          html, body {
            height: 100%;
          }
        </style>
        <script t-nocache="Session information should always be up to date." type="text/javascript">
          var odoo = {
            csrf_token: "<t t-nocache="The csrf token must always be up to date." t-esc="request.csrf_token(None)" />",
            debug: "<t t-esc="debug" />",
          };
          odoo.__session_info__ = <t t-out="json.dumps(request.env['ir.http'].get_frontend_session_info())" />;
          if (!/(^|;\s)tz=/.test(document.cookie)) {
            const userTZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
            document.cookie = `tz=${userTZ}; path=/`;
          }
        </script>
        <t t-call-assets="web.assets_frontend_minimal" t-css="false" defer_load="True" />
        <t t-call="web.conditional_assets_tests" />
        <t t-call-assets="web.assets_frontend_lazy" t-css="false" lazy_load="True" />
      </head>
      <body>
        <t t-out="0" />
      </body>
    </html>
  </template>

  <template id="eurooffice_editor" name="Euro-Office Editor">
    <t t-call="eurooffice_odoo.eurooffice_layout">
      <div id="doceditor" />
      <div id="error" class="w-25 m-auto my-5 py-5 d-flex flex-column d-none">
        <img src="/eurooffice_odoo/static/svg/logo.svg" alt="Euro-Office logo" />
        <div class="my-3 alert alert-danger">
          <p> Euro-Office cannot be reached. Please contact admin. </p>
        </div>
      </div>
      <script type="text/javascript" t-att-src="docApiJS" />
      <script type="text/javascript">
        function getCookie(name) {
          const value = `; ${document.cookie}`;
          const parts = value.split(`; ${name}=`);
          if (parts.length === 2) {
            return parts.pop().split(";").shift();
          }
          return null;
        }
        // Skip version mismatch check between web-apps (x2t) and Euro-Office docservice
        // web-apps About dialog shows x2t version (e.g. 9.2.1) but docservice reports
        // Euro-Office version (0-unstable-*). Setting compareVersions bypasses the check.
        window.compareVersions = true;
        const color_scheme = getCookie('color_scheme');
        var config = JSON.parse('<t t-out="editorConfig" />');
        config.editorConfig.customization.uiTheme = color_scheme ? `default-${color_scheme}` : "default-light";
        if (window?.opener?.location?.href) {
          if (window.opener.location.origin === window.origin) {
            config.editorConfig.customization.goback = {
              blank: false,
              url: window.opener.location.href
            }
          }
        }
        if (window.DocsAPI) {
          config.events = config.events || {};
          config.events.onError = function(event) {
            console.error("Euro-Office Error:", JSON.stringify(event));
            var errorDiv = document.getElementById("error");
            errorDiv.classList.remove("d-none");
            errorDiv.querySelector(".alert-danger p").textContent =
              "Euro-Office Error: " + (event.data ? JSON.stringify(event.data) : JSON.stringify(event));
          };
          var docEditor = new DocsAPI.DocEditor("doceditor", config);
        } else {
          document.getElementById("error").classList.remove("d-none");
        }
      </script>
    </t>
  </template>
</odoo>
