{{!
    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_course/defaultactivitycompletion

    Activity completion selector.

    Example context (json):
    {
        "courseid": "2",
        "sesskey": "AAAAAA",
        "modules": [{
            "id": "10",
            "formattedname": "Assignment",
            "canmanage": true,
            "icon": "https://raw.githubusercontent.com/moodle/moodle/master/mod/assign/pix/icon.png",
            "completionstatus": {
                "string": "Manual",
                "icon": "https://raw.githubusercontent.com/moodle/moodle/master/pix/i/completion-manual-enabled.png"
            }
        }]
    }
}}
<div class="container-fluid">
    <div class="row m-b-2">
        <div class="col">{{#str}}bulkactivitydetail, core_completion{{/str}}</div>
    </div>
<form method="post" action="editdefaultcompletion.php" class="mform" id="theform">
    <div class="row m-b-2">
        <div class="col">
            <input type="submit" value="{{#str}}edit{{/str}}" class="btn btn-primary" name="submitbutton" aria-label="{{#str}}updateactivities, completion{{/str}}" disabled/>
        </div>
    </div>
    <div class="row m-b-1">
        <div class="col-xs-6 span6">
            <input type="checkbox" class="mastercheck m-r-1" aria-label="{{#str}}checkall, completion{{/str}}">
            <label class="font-weight-bold">{{#str}}activitieslabel, core_completion{{/str}}</label>
        </div>
        <div class="col-xs-6 span6">
            <label class="font-weight-bold">{{#str}}completion, core_completion{{/str}}</label>
            <span>{{{helpicon}}}</span>
        </div>
    </div>
    <hr class="row">
    <div class="modules">
        {{#modules}}
            {{#canmanage}}
             <div class="m-b-1">
                <div class="row m-b-1 row-fluid">
                    <div class="col-xs-6 span6">
                        <label class="accesshide" for="modtype_{{id}}">{{#str}}select, core_completion{{/str}} {{formattedname}}</label>
                        <input id="modtype_{{id}}" type="checkbox" class="m-r-1" name="modids[]" value="{{id}}" aria-label="{{#str}}checkactivity, completion, {{{formattedname}}}{{/str}}">
                        <img class="iconlarge activityicon" src="{{icon}}" alt=" " role="presentation" />
                        <span>{{{formattedname}}}</span>
                    </div>
                    <div class="activity-completionstatus col-xs-6 span6">
                        <div class="col-sm-1 span1 p-l-0">
                            {{#completionstatus.icon}}
                                {{{completionstatus.icon}}}
                            {{/completionstatus.icon}}
                            {{^completionstatus.icon}}
                                <span class="m-r-3"></span>
                            {{/completionstatus.icon}}
                        </div>
                        <div class="col-sm-11 span11 p-l-0">
                            <span class="text-muted muted">{{{completionstatus.string}}}</span>
                        </div>
                    </div>
                </div>
            </div>
            <hr class="row">
            {{/canmanage}}
        {{/modules}}
    </div>
    <input type="hidden" name="id" value="{{courseid}}" />
    <input type="hidden" name="sesskey" value="{{sesskey}}" />
    <div class="row">
        <div class="col">
            <input type="submit" value="{{#str}}edit{{/str}}" class="btn btn-primary" name="submitbutton" disabled/>
        </div>
    </div>
</form>
</div>

{{#js}}
require([
    'jquery',
], function($) {
    $('.mastercheck').click(function() {
        var checked = $('.mastercheck').is(':checked');
        $('input[type=checkbox]').each(function() {
            $(this).prop('checked', checked);
            $(this).trigger('change');
        });
    });

    $('input[type=checkbox][id^=modtype_]').change(function() {
        if ($(this).is(':checked')) {
            $('[name=submitbutton]').removeAttr('disabled');
        } else {
            // Is this the last activity checkbox to be un-checked? If so, disable the edit button.
            var somechecked = false;
            $('input[type=checkbox][id^=modtype_]').each(function() {
                if ($(this).is(':checked')) {
                    somechecked = true;
                    return false;
                }
                return true;
            });
            if (!somechecked) {
                $('[name=submitbutton]').attr('disabled', 'disabled');
            }
        }
    });
});
{{/js}}