(% extends "user_settings_partial_base.html" %)

(% block selected_setting_group %)
Profile
(% endblock %)

(% macro string_attr(dispname, name, value, editable, attribute) %)
(% if scim_effective_access.search.check(&attribute) %)
<div class="row g-0 mt-3">
    <label for="person(( name ))" class="col-12 col-md-3 col-lg-2 col-form-label fw-bold py-0">(( dispname ))</label>
    <div class="col-12 col-md-8 col-lg-6">
        (% if scim_effective_access.modify_present.check(&attribute) %)
        <input class="form-control py-0" id="person(( name ))" name="(( name ))" value="(( value ))">
        (% else %)
        <input readonly class="form-control-plaintext py-0" id="person(( name ))" name="(( name ))" value="(( value ))">
        (% endif %)
    </div>
</div>
(% endif %)
(% endmacro %)

(% block settings_window %)

<form id="user_settings_container" class="needs-validation"
      hx-post="/ui/api/user_settings/edit_profile"
      hx-target="#main"
      hx-swap="outerHTML"
      hx-validate="true"
      hx-ext="bs-validation"
      novalidate>
    (% call string_attr("Name", "name", person.name, true, Attribute::Name) %)(% endcall %)

    (% call string_attr("Displayname", "displayname", person.displayname, true, Attribute::DisplayName) %)(% endcall %)

    (% let can_edit_email = rw_active && scim_effective_access.modify_present.check(&Attribute::Mail) %)
    <div class="mb-2">
        <div class="mt-3 mb-2 col-12 col-md-11 col-lg-8">
            <label for="profileEmail" class="fw-bold">Email addresses (% if can_edit_email %)(select primary)(% endif %)</label>
            (% if can_edit_email %)
            <a class="cursor-pointer float-end"
               hx-get="/ui/api/user_settings/add_email"
               hx-target="#emailAddresses"
               hx-include="#emailAddresses :last-child .email-index-state"
               hx-swap="beforeend">
                <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="bi bi-plus-square"
                     viewBox="0 0 16 16" width="20" height="20">
                    <path d="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2z"></path>
                    <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4"></path>
                </svg>
            </a>
            (% endif %)
        </div>
        <div>
            <div class="row g-0">
                <div class="col-12 col-md-11 col-lg-8" id="emailAddresses">
                    (% for (index, email) in person.mails.iter().enumerate() %)
                    (% let value = email.value.clone() %)
                    (% let primary = email.primary %)
                    (% let can_edit = can_edit_email %)

                    (% include "user_settings/form_email_entry_partial.html" %)

                    (% endfor %)
                </div>
            </div>
        </div>
    </div>

    <!-- Edit button -->
    <div class="pt-4">
        (% if rw_active %)
        <button class="btn btn-primary" type="submit">Save</button>
        (% else %)
        <a href="/ui/unlock"
              (% if !rw_possible %)disabled(% endif %)
          >
            <button
              class="btn btn-primary"
              type="button"
              (% if !rw_possible %)
              disabled
              alt="Your session is read-only and may not make changes."
              (% endif %)
              >Unlock Edit 🔒</button>
        </a>
        (% endif %)
    </div>
</form>

(% endblock %)

