{{!
    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 core/progress_bar

    Progress bar.

    Example context (json):
    {
        "id": "progressbar_test",
        "width": "500"
    }
}}
<div id="{{id}}" class="progressbar_container mb-3">
    <div class="progress">
        <div id="{{id}}_bar" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-value="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%"></div>
    </div>
    <div class="d-flex">
        <div style="flex: 1 1 0; min-width: 0;">
            <div id="{{id}}_status" class="text-truncate">&nbsp;</div>
        </div>
        <div class="text-right pl-3" style="flex: 0 0 content">
            <span id="{{id}}_estimate" class="">&nbsp;</span>
            <span id="{{id}}_percentage" class="d-inline-block" style="width: 3em">0%</span>
        </div>
    </div>
</div>

{{! We must not use the JS helper otherwise this gets executed too late. }}
<script>
(function() {
    var el = document.getElementById('{{id}}'),
        progressBar = document.getElementById('{{id}}_bar'),
        statusIndicator = document.getElementById('{{id}}_status'),
        estimateIndicator = document.getElementById('{{id}}_estimate');
        percentageIndicator = document.getElementById('{{id}}_percentage');

    el.addEventListener('update', function(e) {
        var msg = e.detail.message,
            percent = e.detail.percent,
            estimate = e.detail.estimate;

        statusIndicator.textContent = msg;
        progressBar.style.width = percent.toFixed(1) + '%';
        progressBar.setAttribute('value', percent.toFixed(1));
        if (percent === 100) {
            progressBar.classList.add('bg-success');
            progressBar.classList.remove('progress-bar-striped');
            progressBar.classList.remove('progress-bar-animated');
            percentageIndicator.textContent = '100%';
            estimateIndicator.textContent = '';
        } else {
            estimateIndicator.textContent = estimate;
            percentageIndicator.textContent =  percent.toFixed(1) + '%';
            progressBar.classList.remove('bg-success');
        }
    });
})();
</script>
