{{!
    This file is part of Moodle - https://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 local_kopere_bi/dashboard-start

    Example context (json):
    {
      "categorys": [
        {
          "category_title": "Categoria 1",
          "editing": true,
          "category_id": "1",
          "pages": [
            {
              "page_id": "101",
              "page_title": "Relatório 1",
              "description": "Uma breve descrição do relatório 1.",
              "user_fullname": "Usuário 1",
              "is_user_fullname": true
            }
          ]
        }
      ]
    }
}}

<div class="dashboard-homes">
    {{#categorys}}
        <div class="card dashboard-home" data-category_id="{{{category_id}}}">
            <div class="category-name">
                <h3 class="header-title-text">{{{category_title}}}</h3>
                {{#editing}}
                    <div class="buttons-move">
                        <span class="btn-move-up" style="display:none">
                            {{#pix}} icons/btn-move-up, local_kopere_bi {{/pix}}
                        </span>
                        <span class="btn-move-down" style="display:none">
                            {{#pix}} icons/btn-move-down, local_kopere_bi {{/pix}}
                        </span>

                        <a href="?classname=bi-dashboard&method=edit&cat_id={{category_id}}"
                           class="btn btn-primary">{{#str}}page_new_cat,local_kopere_bi{{/str}}</a>
                        <a href="?classname=bi-dashboard&method=edit_cat&cat_id={{category_id}}"
                           class="btn btn-success">{{#str}}cat_edit,local_kopere_bi{{/str}}</a>
                        <a href="?classname=bi-dashboard&method=delete_cat&cat_id={{category_id}}"
                        class="btn btn-danger ml-2">{{#str}}delete{{/str}}</a>
                    </div>
                {{/editing}}
            </div>

            <div class="list-reports mt-3">
                {{#pages}}
                    <div class="list-reports-item">
                        <div class="line-icon">
                            {{#pix}} icons/report, local_kopere_bi {{/pix}}
                        </div>
                        <div class="line-description">
                            <a class="line-title"
                               href="?classname=bi-dashboard&method=preview&page_id={{page_id}}">
                                {{page_title}}</a>
                            <div class="line-description-text">
                                <span>{{description}}</span>
                            </div>
                            {{#is_user_fullname}}
                                <div class="line-description-user">
                                    {{#pix}} icons/user, local_kopere_bi {{/pix}}
                                    <span>{{user_fullname}}</span>
                                </div>
                            {{/is_user_fullname}}
                        </div>
                    </div>
                {{/pages}}
            </div>
        </div>
    {{/categorys}}
</div>

{{#editing}}
    <div style="margin-top: 20px;display: flex;justify-content: flex-end;">
        <a href="?classname=bi-dashboard&method=edit_cat" class="btn btn-success">{{#str}}
            cat_new,local_kopere_bi{{/str}}</a>
    </div>
{{/editing}}

{{#js}}
    require(["jquery", "core/ajax",], function($, ajax) {
        $(".btn-move-up,.btn-move-down").show();
        $(".dashboard-home").each(clickIcons);

        function clickIcons(id, element) {
            var $element = $(element);
            $element.find(".btn-move-down").click(function() {
                $element.next().insertBefore($element);
                saveOrderCategories();
            });
            $element.find(".btn-move-up").click(function() {
                $element.prev().insertAfter($element);
                saveOrderCategories();
            });
        }

        function saveOrderCategories() {
            var itens = "";
            $(".dashboard-home").each(function() {
                itens += "," + $(this).attr("data-category_id");
            });

            ajax.call([{
                methodname: "local_kopere_bi_cat_sortorder",
                args: {
                    itens: itens
                }
            }])[0];
        }
    });
{{/js}}