{{!
    This file is part of Moodle - http://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
}}
{{!
    @template block_opencast/workflow_settings_opencast

    This template renders a the configuration panels of Opencast workflows.

    Example context (json):
    {
        "has_config_panel": true,
        "config_panel": "<div>...</div>",
        "parent_url": "http://mymoodle.de/blocks/opencast/workflowsettings.php",
        "parent_origin": "http://mymoodle.de",
        "language": "en"
    }
}}
{{#has_config_panel}}
    <!DOCTYPE html>
    <html lang="{{language}}">
    <head>
        <meta charset="UTF-8">
        <title>{{#str}}workflow_settings_opencast, block_opencast{{/str}}</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
    <body>
    <!-- Security risk: Do not directly open this page in a browser!
    If you do, the external JavaScript code from the Opencast server can access moodle sites and cookies. -->
    <div id="configForm">
        {{{config_panel}}}
    </div>
    <script>
        window.onload = function () {
            var body = document.body, html = document.documentElement;
            var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
            parent.postMessage(parseInt(height), "{{parent_url}}");
        };

        window.addEventListener('message', function (event) {
            let parent_origin = new URL("{{parent_origin}}");
            if (event.origin !== parent_origin.origin) {
                return;
            }
            if (event.data === "getdata") {
                var values = {};
                $('#configForm input').each(function () {
                    if (this.type === 'checkbox' || this.type === 'radio') {
                        values[this.id] = this.checked ? 'true' : 'false';
                    } else {
                        values[this.id] = $(this).val();
                    }
                });
                parent.postMessage(JSON.stringify(values), "{{parent_url}}");
            }
        });
    </script>
    </body>
    </html>
{{/has_config_panel}}
