<!--
Copyright (C) 2023 Nicola Murino

This WebUI uses the KeenThemes Bootstrap Templates:

https://keenthemes.com/bootstrap-templates

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 {{if .IsAdd}}data-i18n="title.add_share"{{else}}data-i18n="title.update_share"{{end}} class="card-title section-title"></h3>
    </div>
    <div class="card-body">
        {{- template "errmsg" .Error}}
        <form id="share_form" action="{{.CurrentURL}}" method="POST" autocomplete="off">
            <div class="form-group row">
                <label for="name" data-i18n="general.name" class="col-md-3 col-form-label">Name</label>
                <div class="col-md-9">
                    <input id="name" type="text" placeholder="" name="name" value="{{.Share.Name}}" maxlength="255" autocomplete="off"
                        required {{if not .IsAdd}}class="form-control-plaintext readonly-input" readonly{{else}}class="form-control"{{end}} />
                </div>
            </div>

            <div class="form-group row mt-10">
                <label for="scope" data-i18n="share.scope" class="col-md-3 col-form-label">Scope</label>
                <div class="col-md-9">
                    <select id="scope" name="scope" class="form-select" data-control="i18n-select2" data-hide-search="true" aria-describedby="scopeHelp">
                        <option data-i18n="share.scope_read" value="1" {{if eq .Share.Scope 1 }}selected{{end}}>Read</option>
                        <option data-i18n="share.scope_write" value="2" {{if eq .Share.Scope 2 }}selected{{end}}>Write</option>
                        <option data-i18n="share.scope_read_write" value="3" {{if eq .Share.Scope 3 }}selected{{end}}>Read/Write</option>
                    </select>
                    <div id="scopeHelp" data-i18n="share.scope_help" class="form-text">
                        For scope "Write" and "Read/Write" you have to define one path and it must be a directory
                    </div>
                </div>
            </div>

            <div class="card mt-10">
                <div class="card-header bg-light">
                    <h3 data-i18n="general.paths" class="card-title section-title-inner">Paths</h3>
                </div>
                <div class="card-body">
                    <div id="paths">
                        <div class="form-group">
                            <div data-repeater-list="paths">
                                {{- range $idx, $val := .Share.Paths}}
                                <div data-repeater-item>
                                    <div class="form-group row">
                                        <div class="col-md-9 mt-3 mt-md-8">
                                            <input data-i18n="[placeholder]share.path_help" type="text" class="form-control"
                                                name="path" value="{{$val}}" />
                                        </div>
                                        <div class="col-md-3 mt-3 mt-md-8">
                                            <a href="#" data-repeater-delete
                                                class="btn btn-light-danger">
                                                <i class="ki-duotone ki-trash fs-5">
                                                    <span class="path1"></span>
                                                    <span class="path2"></span>
                                                    <span class="path3"></span>
                                                    <span class="path4"></span>
                                                    <span class="path5"></span>
                                                </i>
                                                <span data-i18n="general.delete">Delete</span>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                {{- else}}
                                <div data-repeater-item>
                                    <div class="form-group row">
                                        <div class="col-md-9 mt-3 mt-md-8">
                                            <input data-i18n="[placeholder]share.path_help" type="text" class="form-control"
                                                name="path" value="" />
                                        </div>
                                        <div class="col-md-3 mt-3 mt-md-8">
                                            <a href="#" data-repeater-delete
                                                class="btn btn-light-danger">
                                                <i class="ki-duotone ki-trash fs-5">
                                                    <span class="path1"></span>
                                                    <span class="path2"></span>
                                                    <span class="path3"></span>
                                                    <span class="path4"></span>
                                                    <span class="path5"></span>
                                                </i>
                                                <span data-i18n="general.delete">Delete</span>
                                            </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>

            <div class="form-group row mt-10">
                <label for="password" data-i18n="login.password" class="col-md-3 col-form-label">Password</label>
                <div class="col-md-9">
                    <input id="password" type="password" class="form-control" name="password" autocomplete="new-password"
                        placeholder="" spellcheck="false" value="{{.Share.Password}}" aria-describedby="passwordHelp" />
                    <div id="passwordHelp" data-i18n="share.password_help" class="form-text">
                        If set the share will be password-protected
                    </div>
                </div>
            </div>

            <div class="form-group row mt-10">
                <label for="id_expiration" data-i18n="general.expiration" class="col-md-3 col-form-label">Expiration</label>
                <div class="col-md-9 d-flex">
                    <input data-i18n="[placeholder]general.expiration_help" id="id_expiration" class="form-control" placeholder="Pick an expiration date" />
                    <button class="btn btn-icon btn-light-danger ms-2 d-none" id="id_expiration_clear">
                        <i class="ki-solid ki-cross fs-1"></i>
                    </button>
                </div>
            </div>

            <div class="form-group row mt-10">
                <label for="max_tokens" data-i18n="share.max_tokens" class="col-md-3 col-form-label">Max tokens</label>
                <div class="col-md-9">
                    <input id="max_tokens" type="number" min="0" class="form-control" name="max_tokens" value="{{.Share.MaxTokens}}" aria-describedby="max_tokens_help" />
                    <div id="max_tokens_help" data-i18n="share.max_tokens_help" class="form-text">
                        Maximum number of times this share can be accessed. 0 means no limit
                    </div>
                </div>
            </div>

            <div class="form-group row mt-10">
                <label for="allowed_ip" data-i18n="general.allowed_ip_mask" class="col-md-3 col-form-label">Allowed IP/Mask</label>
                <div class="col-md-9">
                    <textarea id="allowed_ip" class="form-control" name="allowed_ip" rows="3" aria-describedby="allowed_ip_help"
                        placeholder="">{{.Share.GetAllowedFromAsString}}</textarea>
                    <div id="allowed_ip_help" data-i18n="general.ip_mask_help" class="form-text">
                        Comma separated IP/Mask in CIDR format, for example "192.168.1.0/24,10.8.0.100/32"
                    </div>
                </div>
            </div>

            <div class="form-group row mt-10">
                <label for="description" data-i18n="general.description" class="col-md-3 col-form-label">Description</label>
                <div class="col-md-9">
                    <textarea id="description" class="form-control" name="description" rows="3"
                        placeholder="">{{.Share.Description}}</textarea>
                </div>
            </div>

            <div class="d-flex justify-content-end mt-12">
                <input type="hidden" name="expiration_date" id="hidden_start_datetime" value="">
                <input type="hidden" name="_form_token" value="{{.CSRFToken}}">
                <button type="submit" id="form_submit" class="btn btn-primary px-10">
                    <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 {{- if .CSPNonce}} nonce="{{.CSPNonce}}"{{- end}} src="{{.StaticURL}}/assets/plugins/custom/flatpickr/l10n/it.js"></script>
<script {{- if .CSPNonce}} nonce="{{.CSPNonce}}"{{- end}} src="{{.StaticURL}}/assets/plugins/custom/flatpickr/l10n/de.js"></script>
<script {{- if .CSPNonce}} nonce="{{.CSPNonce}}"{{- end}} src="{{.StaticURL}}/assets/plugins/custom/flatpickr/l10n/fr.js"></script>
<script type="text/javascript" {{- if .CSPNonce}} nonce="{{.CSPNonce}}"{{- end}}>
    $(document).on("i18nshow", function(){
            initRepeater('#paths');
            initRepeaterItems();

            const picker = $('#id_expiration').flatpickr({
                enableTime: false,
                time_24hr: true,
                formatDate: (date, format, locale) => {
                    return $.t('general.datetime', {
                                val: new Date(date),
                                formatParams: {
                                    val: { year: 'numeric', month: 'numeric', day: 'numeric' },
                                }
                            });
                },
                defaultHour: 23,
                defaultMinute: 59,
                locale: i18next.resolvedLanguage,
                onChange: function(selectedDates, dateStr, instance) {
                    if (selectedDates.length > 0){
                        $('#id_expiration_clear').removeClass("d-none");
                    } else {
                        $('#id_expiration_clear').addClass("d-none");
                    }
                }
            });
            //{{ if gt .Share.ExpiresAt 0 }}
            let input_dt = moment('{{.Share.ExpiresAt }}', 'x').format('YYYY-MM-DD');
            picker.setDate(input_dt, true);
            //{{ end }}

            $('#id_expiration_clear').on("click", function(e){
                e.preventDefault();
                picker.clear();
            });

            $("#share_form").submit(function (event) {
                $('#hidden_start_datetime').val("");
                let dt = picker.selectedDates;
                if (dt.length > 0) {
                    let d = dt[0];
                    if (d) {
                        let dateString = moment.utc(d).format('YYYY-MM-DD HH:mm:ss');
                        $('#hidden_start_datetime').val(dateString);
                    }
                }
                let submitButton = document.querySelector('#form_submit');
                submitButton.setAttribute('data-kt-indicator', 'on');
                submitButton.disabled = true;
            });
        });
</script>
{{- end}}