<!--
Copyright (C) 2019-2023 Nicola Murino

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, version 3.

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
{{define "shared_user_group"}}
<script type="text/javascript">
    $("body").on("click", ".add_new_dirperms_field_btn", function () {
        let index = $(".form_field_dirperms_outer").find(".form_field_dirperms_outer_row").length;
        while (document.getElementById("idSubDirPermsPath"+index) != null){
            index++;
        }
        $(".form_field_dirperms_outer").append(`
                <div class="row form_field_dirperms_outer_row">
                    <div class="form-group col-md-8">
                        <input type="text" class="form-control" id="idSubDirPermsPath${index}" name="sub_perm_path${index}" placeholder="directory path, i.e. /dir" value="" maxlength="255">
                    </div>
                    <div class="form-group col-md-3">
                        <select class="form-control" id="idSubDirPermissions${index}" name="sub_perm_permissions${index}" multiple>
                        </select>
                    </div>
                    <div class="form-group col-md-1">
                        <button class="btn btn-circle btn-danger remove_dirperms_btn_frm_field">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </div>
            `);

            {{- range .ValidPerms}}
            $("#idSubDirPermissions"+index).append($('<option>').val('{{.}}').text('{{.}}'));
            {{- end}}
            $("#idSubDirPermissions"+index).selectpicker();
        });

    $("body").on("click", ".remove_dirperms_btn_frm_field", function () {
        $(this).closest(".form_field_dirperms_outer_row").remove();
    });

    $("body").on("click", ".add_new_vfolder_field_btn", function () {
        let index = $(".form_field_vfolders_outer").find(".form_field_vfolder_outer_row").length;
        while (document.getElementById("idVolderPath" + index) != null) {
            index++;
        }
        $(".form_field_vfolders_outer").append(`
                <div class="row form_field_vfolder_outer_row">
                    <div class="form-group col-md-3">
                        <input type="text" class="form-control" id="idVolderPath${index}" name="vfolder_path" placeholder="mount path, i.e. /vfolder" value="" maxlength="255">
                    </div>
                    <div class="form-group col-md-3">
                        <select class="form-control" id="idVfolderName${index}" name="vfolder_name">
                            <option value=""></option>
                        </select>
                    </div>
                    <div class="form-group col-md-3">
                        <input type="text" class="form-control" id="idVfolderQuotaSize${index}" name="vfolder_quota_size"
                            value="" aria-describedby="vqsHelpBlock${index}">
                        <small id="vqsHelpBlock${index}" class="form-text text-muted">
                            Quota size
                        </small>
                    </div>
                    <div class="form-group col-md-2">
                        <input type="number" class="form-control" id="idVfolderQuotaFiles${index}" name="vfolder_quota_files"
                            value="" min="-1" aria-describedby="vqfHelpBlock${index}">
                        <small id="vqfHelpBlock${index}" class="form-text text-muted">
                            Quota files
                        </small>
                    </div>
                    <div class="form-group col-md-1">
                        <button class="btn btn-circle btn-danger remove_vfolder_btn_frm_field">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </div>
            `);

        {{- range .VirtualFolders}}
        $("#idVfolderName"+index).append($('<option>').val('{{.Name}}').text('{{.Name}}'));
        {{- end}}
        $("#idVfolderName"+index).selectpicker({'liveSearch': true});
    });

    $("body").on("click", ".remove_vfolder_btn_frm_field", function () {
        $(this).closest(".form_field_vfolder_outer_row").remove();
    });

    $("body").on("click", ".add_new_bwlimit_field_btn", function () {
        let index = $(".form_field_bwlimits_outer").find(".form_field_bwlimits_outer_row").length;
        while (document.getElementById("idBandwidthLimitSources"+index) != null){
            index++;
        }
        $(".form_field_bwlimits_outer").append(`
                <div class="row form_field_bwlimits_outer_row">
                     <div class="form-group col-md-8">
                        <textarea class="form-control" id="idBandwidthLimitSources0" name="bandwidth_limit_sources${index}" rows="4" placeholder=""
                            aria-describedby="bwLimitSourcesHelpBlock${index}"></textarea>
                        <small id="bwLimitSourcesHelpBlock${index}" class="form-text text-muted">
                            Comma separated IP/Mask in CIDR format, example: "192.168.1.0/24,10.8.0.100/32"
                        </small>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <input type="number" class="form-control" id="idUploadBandwidthSource${index}" name="upload_bandwidth_source${index}"
                                placeholder="" value="" min="0" aria-describedby="ulHelpBlock${index}">
                            <small id="ulHelpBlock${index}" class="form-text text-muted">
                                UL (KB/s). 0 means no limit
                            </small>
                        </div>
                        <div class="form-group">
                            <input type="number" class="form-control" id="idDownloadBandwidthSource${index}" name="download_bandwidth_source${index}"
                                placeholder="" value="" min="0" aria-describedby="dlHelpBlock${index}">
                            <small id="dlHelpBlock${index}" class="form-text text-muted">
                                DL (KB/s). 0 means no limit
                            </small>
                        </div>
                    </div>
                    <div class="form-group col-md-1">
                        <button class="btn btn-circle btn-danger remove_bwlimit_btn_frm_field">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </div>
            `);
    });

     $("body").on("click", ".remove_bwlimit_btn_frm_field", function () {
        $(this).closest(".form_field_bwlimits_outer_row").remove();
    });

    $("body").on("click", ".add_new_pattern_field_btn", function () {
        let index = $(".form_field_patterns_outer").find(".form_field_patterns_outer_row").length;
        while (document.getElementById("idPatternPath"+index) != null){
            index++;
        }
        $(".form_field_patterns_outer").append(`
                <div class="row form_field_patterns_outer_row">
                    <div class="form-group col-md-3">
                        <input type="text" class="form-control" id="idPatternPath${index}" name="pattern_path${index}" placeholder="directory path, i.e. /dir" value="" maxlength="255">
                    </div>
                    <div class="form-group col-md-4">
                        <input type="text" class="form-control" id="idPatterns${index}" name="patterns${index}" placeholder="*.zip,?.txt" value="" maxlength="255">
                    </div>
                    <div class="form-group col-md-2">
                        <select class="form-control" id="idPatternType${index}" name="pattern_type${index}">
                            <option value="denied">Denied</option>
                            <option value="allowed">Allowed</option>
                        </select>
                    </div>
                    <div class="form-group col-md-2">
                        <select class="form-control" id="idPatternPolicy${index}" name="pattern_policy${index}">
                            <option value="0">Visible</option>
                            <option value="1">Hidden</option>
                        </select>
                    </div>
                    <div class="form-group col-md-1">
                        <button class="btn btn-circle btn-danger remove_pattern_btn_frm_field">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </div>
            `);
        $("#idPatternType"+index).selectpicker();
        $("#idPatternPolicy"+index).selectpicker();
    });

    $("body").on("click", ".remove_pattern_btn_frm_field", function () {
        $(this).closest(".form_field_patterns_outer_row").remove();
    });
</script>
{{end}}