<!--
Copyright (C) 2024 Nicola Murino

This WebUI uses the KeenThemes Mega Bundle, a proprietary theme:

https://keenthemes.com/products/templates-mega-bundle

KeenThemes HTML/CSS/JS components are allowed for use only within the
SFTPGo product and restricted to be used in a resealable HTML template
that can compete with KeenThemes products anyhow.

This WebUI is allowed for use only within the SFTPGo product and
therefore cannot be used in derivative works/products without an
explicit grant from the SFTPGo Team (support@sftpgo.com).
-->
{{template "base" .}}

{{- define "page_body"}}
<div class="card shadow-sm">
    <div class="card-header bg-light">
        <h3 data-i18n="{{.Title}}" class="card-title section-title"></h3>
    </div>
    <div class="card-body">
        {{- template "infomsg" "group.help"}}
        {{- template "errmsg" .Error}}
        <form id="group_form" enctype="multipart/form-data" action="{{.CurrentURL}}" method="POST" autocomplete="off">

            <div class="form-group row">
                <label for="idGroupName" data-i18n="general.name" class="col-md-3 col-form-label">Name</label>
                <div class="col-md-9">
                    <input id="idGroupName" type="text" placeholder="" name="name" value="{{.Group.Name}}" maxlength="255" autocomplete="off"
                        spellcheck="false" required {{if eq .Mode 2}}class="form-control-plaintext readonly-input" readonly{{else}}class="form-control"{{end}} />
                </div>
            </div>

            <div class="form-group row mt-10">
                <label for="idDescription" data-i18n="general.description" class="col-md-3 col-form-label">Description</label>
                <div class="col-md-9">
                    <input id="idDescription" type="text" class="form-control" name="description" value="{{.Group.Description}}" maxlength="255">
                </div>
            </div>

            {{- template "fshtml" .FsWrapper}}
            {{- if .VirtualFolders}}
            <div class="card mt-10 {{if .LoggedUser.Filters.Preferences.HideVirtualFolders}}d-none{{end}}">
                <div class="card-header bg-light">
                    <h3 data-i18n="title.folders" class="card-title section-title-inner">Virtual folders</h3>
                </div>
                <div class="card-body">
                    <div id="virtual_folders">
                        {{- template "infomsg-no-mb" "user.virtual_folders_help"}}
                        <div class="form-group">
                            <div data-repeater-list="virtual_folders">
                                {{- range $idx, $val := .Group.VirtualFolders}}
                                <div data-repeater-item>
                                    <div data-repeater-item>
                                        <div class="form-group row">
                                            <div class="col-md-3 mt-3 mt-md-8">
                                                <input data-i18n="[placeholder]virtual_folders.mount_path" type="text" class="form-control" name="vfolder_path" value="{{$val.VirtualPath}}" />
                                            </div>
                                            <div class="col-md-3 mt-3 mt-md-8">
                                                <select name="vfolder_name" data-i18n="[data-placeholder]general.folder_placeholder" class="form-select select-repetear" data-placeholder="Select a folder" data-allow-clear="true">
                                                    <option value=""></option>
                                                    {{- range $.VirtualFolders}}
                                                    <option value="{{.Name}}" {{- if eq $val.Name .Name}} selected{{- end}}>{{.Name}}</option>
                                                    {{- end}}
                                                </select>
                                            </div>
                                            <div class="col-md-3 mt-3 mt-md-8">
                                                <input type="text" class="form-control" name="vfolder_quota_size" value="{{HumanizeBytes $val.QuotaSize}}" />
                                                <div class="form-text" data-i18n="virtual_folders.quota_size"></div>
                                            </div>
                                            <div class="col-md-2 mt-3 mt-md-8">
                                                <input type="number" min="-1" class="form-control" name="vfolder_quota_files" value="{{$val.QuotaFiles}}" />
                                                <div class="form-text" data-i18n="virtual_folders.quota_files"></div>
                                            </div>
                                            <div class="col-md-1 mt-3 mt-md-8">
                                                <a href="#" data-repeater-delete
                                                    class="btn btn-light-danger ps-5 pe-4">
                                                    <i class="ki-duotone ki-trash fs-2">
                                                        <span class="path1"></span>
                                                        <span class="path2"></span>
                                                        <span class="path3"></span>
                                                        <span class="path4"></span>
                                                        <span class="path5"></span>
                                                    </i>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                {{- else}}
                                <div data-repeater-item>
                                    <div class="form-group row">
                                        <div class="col-md-3 mt-3 mt-md-8">
                                            <input data-i18n="[placeholder]virtual_folders.mount_path" type="text" class="form-control" name="vfolder_path" value="" />
                                        </div>
                                        <div class="col-md-3 mt-3 mt-md-8">
                                            <select name="vfolder_name" data-i18n="[data-placeholder]general.folder_placeholder" class="form-select select-repetear" data-placeholder="Select a folder" data-allow-clear="true">
                                                <option value=""></option>
                                                {{- range .VirtualFolders}}
                                                <option value="{{.Name}}">{{.Name}}</option>
                                                {{- end}}
                                            </select>
                                        </div>
                                        <div class="col-md-3 mt-3 mt-md-8">
                                            <input type="text" class="form-control" name="vfolder_quota_size" value="" />
                                            <div class="form-text" data-i18n="virtual_folders.quota_size"></div>
                                        </div>
                                        <div class="col-md-2 mt-3 mt-md-8">
                                            <input type="number" min="-1" class="form-control" name="vfolder_quota_files" value="" />
                                            <div class="form-text" data-i18n="virtual_folders.quota_files"></div>
                                        </div>
                                        <div class="col-md-1 mt-3 mt-md-8">
                                            <a href="#" data-repeater-delete
                                                class="btn btn-light-danger ps-5 pe-4">
                                                <i class="ki-duotone ki-trash fs-2">
                                                    <span class="path1"></span>
                                                    <span class="path2"></span>
                                                    <span class="path3"></span>
                                                    <span class="path4"></span>
                                                    <span class="path5"></span>
                                                </i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                {{- end}}
                            </div>
                        </div>

                        <div class="form-group mt-5">
                            <a href="#" data-repeater-create class="btn btn-light-primary">
                                <i class="ki-duotone ki-plus fs-3"></i>
                                <span data-i18n="general.add">Add</span>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            {{- end}}

            <div class="accordion shadow-sm mt-10" id="accordionUser">

                <div class="accordion-item">
                    <h2 class="accordion-header" id="headingPermissions">
                        <button class="accordion-button section-title-inner text-primary collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapsePermissions" aria-expanded="true" aria-controls="collapsePermissions">
                            <span data-i18n="general.acls">ACLs</span>
                        </button>
                    </h2>
                    <div id="collapsePermissions" class="accordion-collapse collapse" aria-labelledby="headingPermissions" data-bs-parent="#accordionUser">
                        <div class="accordion-body">

                            <div class="card mt-10">
                                <div class="card-header bg-light">
                                    <h3 data-i18n="filters.directory_permissions" class="card-title section-title-inner">Per-directory permissions</h3>
                                </div>
                                <div class="card-body">
                                    <div id="directory_permissions">
                                        {{- template "infomsg-no-mb" "filters.directory_permissions_help"}}
                                        <div class="form-group">
                                            <div data-repeater-list="directory_permissions">
                                                {{- range $idx, $dirPerms := .Group.GetPermissions -}}
                                                <div data-repeater-item>
                                                    <div class="form-group row">
                                                        <div class="col-md-6 mt-3 mt-md-8">
                                                            <input data-i18n="[placeholder]filters.directory_path_help" type="text" class="form-control" name="sub_perm_path" value="{{$dirPerms.Path}}" />
                                                        </div>
                                                        <div class="col-md-5 mt-3 mt-md-8">
                                                            <select name="sub_perm_permissions" data-i18n="[data-placeholder]general.permissions" class="form-select select-repetear" data-hide-search="true" data-close-on-select="false" multiple>
                                                                {{- range $validPerm := $.ValidPerms}}
                                                                <option value="{{$validPerm}}" {{- range $perm := $dirPerms.Permissions }}{{- if eq $perm $validPerm}} selected{{- end}}{{- end}}>{{$validPerm}}</option>
                                                                {{- end}}
                                                            </select>
                                                        </div>
                                                        <div class="col-md-1 mt-3 mt-md-8">
                                                            <a href="#" data-repeater-delete
                                                                class="btn btn-light-danger ps-5 pe-4">
                                                                <i class="ki-duotone ki-trash fs-2">
                                                                    <span class="path1"></span>
                                                                    <span class="path2"></span>
                                                                    <span class="path3"></span>
                                                                    <span class="path4"></span>
                                                                    <span class="path5"></span>
                                                                </i>
                                                            </a>
                                                        </div>
                                                    </div>
                                                </div>
                                                {{- else}}
                                                <div data-repeater-item>
                                                    <div class="form-group row">
                                                        <div class="col-md-6 mt-3 mt-md-8">
                                                            <input data-i18n="[placeholder]filters.directory_path_help" type="text" class="form-control" name="sub_perm_path" value="" />
                                                        </div>
                                                        <div class="col-md-5 mt-3 mt-md-8">
                                                            <select name="sub_perm_permissions" data-i18n="[data-placeholder]general.permissions" class="form-select select-repetear" data-hide-search="true" data-close-on-select="false" multiple>
                                                                {{- range $validPerm := .ValidPerms}}
                                                                <option value="{{$validPerm}}">{{$validPerm}}</option>
                                                                {{- end}}
                                                            </select>
                                                        </div>
                                                        <div class="col-md-1 mt-3 mt-md-8">
                                                            <a href="#" data-repeater-delete
                                                                class="btn btn-light-danger ps-5 pe-4">
                                                                <i class="ki-duotone ki-trash fs-2">
                                                                    <span class="path1"></span>
                                                                    <span class="path2"></span>
                                                                    <span class="path3"></span>
                                                                    <span class="path4"></span>
                                                                    <span class="path5"></span>
                                                                </i>
                                                            </a>
                                                        </div>
                                                    </div>
                                                </div>
                                                {{- end}}
                                            </div>
                                        </div>

                                        <div class="form-group mt-5">
                                            <a href="#" data-repeater-create class="btn btn-light-primary">
                                                <i class="ki-duotone ki-plus fs-3"></i>
                                                <span data-i18n="general.add">Add</span>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            {{- template "user_group_perms" .Group.UserSettings.Filters}}

                            {{- template "user_group_access_time" .Group.UserSettings.Filters}}

                            <div class="form-group row mt-10">
                                <label for="idMaxSessions" data-i18n="filters.max_sessions" class="col-md-3 col-form-label">Max sessions</label>
                                <div class="col-md-9">
                                    <input id="idMaxSessions" type="number" min="0" class="form-control" name="max_sessions" value="{{.Group.UserSettings.MaxSessions}}" aria-describedby="idMaxSessionsHelp" />
                                    <div id="idMaxSessionsHelp" class="form-text" data-i18n="filters.max_sessions_help"></div>
                                </div>
                            </div>

                            <div class="form-group row mt-10">
                                <label for="idProtocols" data-i18n="filters.denied_protocols" class="col-md-3 col-form-label">
                                    Denied protocols
                                </label>
                                <div class="col-md-9">
                                    <select id="idProtocols" name="denied_protocols" class="form-select" data-control="i18n-select2" data-close-on-select="false" multiple>
                                        {{- range $protocol := .ValidProtocols}}
                                        <option value="{{$protocol}}" {{- range $p :=$.Group.UserSettings.Filters.DeniedProtocols }}{{- if eq $p $protocol}} selected{{- end}}{{- end}}>{{$protocol}}</option>
                                        {{- end}}
                                    </select>
                                </div>
                            </div>

                            <div class="form-group row mt-10">
                                <label for="idLoginMethods" data-i18n="filters.denied_login_methods" class="col-md-3 col-form-label">
                                    Denied login methods
                                </label>
                                <div class="col-md-9">
                                    <select id="idLoginMethods" name="denied_login_methods" class="form-select" data-control="i18n-select2" data-close-on-select="false" multiple aria-describedby="idLoginMethodsHelp">
                                        {{- range $method := .ValidLoginMethods}}
                                        <option value="{{$method}}" {{- range $m :=$.Group.UserSettings.Filters.DeniedLoginMethods }}{{- if eq $m $method}} selected{{- end}}{{- end}}>{{$method}}</option>
                                        {{- end}}
                                    </select>
                                    <div id="idLoginMethodsHelp" data-i18n="filters.denied_login_methods_help" class="form-text">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group row mt-10">
                                <label for="idTwoFactorProtocols" data-i18n="2fa.require_for" class="col-md-3 col-form-label">
                                    Require 2FA for
                                </label>
                                <div class="col-md-9">
                                    <select id="idTwoFactorProtocols" name="required_two_factor_protocols" class="form-select" data-control="i18n-select2" data-close-on-select="false" multiple>
                                        {{- range $protocol := .TwoFactorProtocols}}
                                        <option value="{{$protocol}}" {{- range $p :=$.Group.UserSettings.Filters.TwoFactorAuthProtocols }}{{- if eq $p $protocol}} selected{{- end}}{{- end}}>{{$protocol}}</option>
                                        {{end}}
                                    </select>
                                </div>
                            </div>

                            <div class="form-group row mt-10">
                                <label for="idWebClient" data-i18n="filters.web_client_options" class="col-md-3 col-form-label">
                                    Web client/REST API
                                </label>
                                <div class="col-md-9">
                                    <select id="idWebClient" name="web_client_options" class="form-select" data-control="i18n-select2" data-close-on-select="false" multiple>
                                        {{- range $option := .WebClientOptions}}
                                        <option value="{{$option}}" {{- range $p :=$.Group.UserSettings.Filters.WebClient }}{{- if eq $p $option}}selected{{- end}}{{- end}}>{{$option}}</option>
                                        {{- end}}
                                    </select>
                                </div>
                            </div>

                            <div class="form-group row mt-10">
                                <label for="idDeniedIP" data-i18n="general.denied_ip_mask" class="col-md-3 col-form-label">Denied IP/Mask</label>
                                <div class="col-md-9">
                                    <textarea class="form-control" id="idDeniedIP" name="denied_ip" aria-describedby="idDeniedIPHelp"
                                        rows="3">{{.Group.GetDeniedIPAsString}}</textarea>
                                    <div id="idDeniedIPHelp" class="form-text" data-i18n="general.ip_mask_help"></div>
                                </div>
                            </div>

                            <div class="form-group row mt-10">
                                <label for="idAllowedIP" data-i18n="general.allowed_ip_mask" class="col-md-3 col-form-label">Allowed IP/Mask</label>
                                <div class="col-md-9">
                                    <textarea class="form-control" id="idAllowedIP" name="allowed_ip" aria-describedby="idAllowedIPHelp"
                                        rows="3">{{.Group.GetAllowedIPAsString}}</textarea>
                                    <div id="idAllowedIPHelp" class="form-text" data-i18n="general.ip_mask_help"></div>
                                </div>
                            </div>

                        </div>
                    </div>

                </div>

                <div class="accordion-item">
                    <h2 class="accordion-header" id="headingQuota">
                        <button class="accordion-button section-title-inner text-primary collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseQuota" aria-expanded="true" aria-controls="collapseQuota">
                            <span data-i18n="general.quota_limits">Disk quota and bandwidth limits</span>
                        </button>
                    </h2>
                    <div id="collapseQuota" class="accordion-collapse collapse" aria-labelledby="headingQuota" data-bs-parent="#accordionUser">
                        <div class="accordion-body">
                            {{- template "user_group_quota" .Group.UserSettings}}
                        </div>
                    </div>
                </div>

                <div class="accordion-item">
                    <h2 class="accordion-header" id="headingAdvanced">
                        <button class="accordion-button section-title-inner text-primary collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseAdvanced" aria-expanded="true" aria-controls="collapseAdvanced">
                            <span data-i18n="general.advanced_settings">Advanced settings</span>
                        </button>
                    </h2>
                    <div id="collapseAdvanced" class="accordion-collapse collapse" aria-labelledby="headingAdvanced" data-bs-parent="#accordionUser">
                        <div class="accordion-body">

                            <div class="form-group row mt-10">
                                <label for="idExpiresIn" data-i18n="user.expires_in" class="col-md-3 col-form-label">Expires in</label>
                                <div class="col-md-9">
                                    <input id="idExpiresIn" type="number" min="0" class="form-control" name="expires_in" value="{{.Group.UserSettings.ExpiresIn}}" aria-describedby="idExpiresIn" />
                                    <div id="idExpiresInHelp" class="form-text" data-i18n="user.expires_in_help"></div>
                                </div>
                            </div>

                            {{- template "user_group_profile" .Group.UserSettings.Filters}}

                            {{- template "user_group_advanced" .Group.UserSettings.Filters}}

                            <div class="form-group row mt-10 {{if not .Group.HasExternalAuth}}d-none{{end}}">
                                <label for="idExtAuthCacheTime" data-i18n="filters.external_auth_cache_time" class="col-md-3 col-form-label">External auth cache time</label>
                                <div class="col-md-9">
                                    <input id="idExtAuthCacheTime" type="number" min="0" class="form-control" name="external_auth_cache_time" value="{{.Group.UserSettings.Filters.ExternalAuthCacheTime}}" aria-describedby="idExtAuthCacheTimeHelp" />
                                    <div id="idExtAuthCacheTimeHelp" class="form-text" data-i18n="filters.external_auth_cache_time_help"></div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>

            </div>

            <div class="d-flex justify-content-end mt-12">
                <input type="hidden" name="_form_token" value="{{.CSRFToken}}">
                <button type="submit" id="form_submit" class="btn btn-primary px-10" name="form_action" value="submit">
                    <span data-i18n="general.submit" class="indicator-label">
                        Submit
                    </span>
                    <span data-i18n="general.wait" class="indicator-progress">
                        Please wait...
                        <span class="spinner-border spinner-border-sm align-middle ms-2"></span>
                    </span>
                </button>
            </div>
        </form>
    </div>
</div>
{{- end}}
{{- define "extra_js"}}
<script {{- if .CSPNonce}} nonce="{{.CSPNonce}}"{{- end}} src="{{.StaticURL}}/assets/plugins/custom/formrepeater/formrepeater.bundle.js"></script>
<script type="text/javascript" {{- if .CSPNonce}} nonce="{{.CSPNonce}}"{{- end}}>

    $(document).on("i18nload", function(){
        initRepeater('#virtual_folders');
        initRepeater('#directory_permissions');
        initRepeater('#directory_patterns');
        initRepeater('#src_bandwidth_limits');
        initRepeater('#access_time_restrictions');
        initRepeaterItems();
        //{{- if .Error}}
        $('#accordionUser .collapse').removeAttr("data-bs-parent").collapse('show');
        //{{- end}}
        onFilesystemChanged('{{.Group.UserSettings.FsConfig.Provider}}');

        $('#idFilesystem').on("change", function(){
            onFilesystemChanged(this.value);
        });
    });

    $(document).on("i18nshow", function(){
        $('#group_form').submit(function (event) {
			let submitButton = document.querySelector('#form_submit');
			submitButton.setAttribute('data-kt-indicator', 'on');
			submitButton.disabled = true;
        });
    });
</script>
{{- end}}