(% extends "admin/admin_partial_base.html" %)

(% block groups_item_extra_classes %)active(% endblock %)

(% macro string_attr(dispname, name, value, can_modify_any_attr, attribute) %)
(% if scim_effective_access.search.check(&attribute) -%)
<div class="row mt-3">
    (% if can_rw && can_modify_any_attr -%)
    <label for="group(( name ))" class="col-12 col-md-3 col-lg-2 col-form-label fw-bold py-0 py-md-2">(( dispname ))</label>
    (% else -%)
    <label for="group(( name ))" class="col-12 col-md-3 col-lg-2 col-form-label fw-bold py-0">(( dispname ))</label>
    (% endif -%)
    <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="group(( name ))" name="(( name ))" value="(( value ))"/>
        (% else -%)
        (( value ))
        (% endif -%)
    </div>
</div>
(% endif -%)
(% endmacro %)


(% block admin_page %)
<!-- TODO: code like this shouldn't be in the template-->
(%
let can_modify_any_attr = scim_effective_access.modify_present.check_any(
&std::collections::BTreeSet::from(GROUP_ATTRIBUTES))
%)

<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/ui/admin/groups" hx-target="#main">Group List</a></li>
        <li class="breadcrumb-item active" aria-current="page">(% if can_rw %)Viewing(% else %)Managing(% endif %) Entry</li>
    </ol>
</nav>

<form id="user_settings_container" class="needs-validation"
      hx-post="/ui/api/admin/group/(( group.uuid ))"
      hx-swap="beforeend"
      hx-target=".toast-container"
      hx-validate="true"
      hx-ext="bs-validation"
      novalidate>

    (% if can_modify_any_attr -%)
        (% if can_rw -%)
            <button type="submit" class="btn btn-primary">Save</button>
        (% else -%)
            <a href="/ui/unlock">
                <button type="button" class="btn btn-secondary">Unlock Edit 🔒</button>
            </a>
        (% endif -%)
    (% endif -%)

    (% call string_attr("UUID", "uuid", group.uuid, can_modify_any_attr, Attribute::Uuid) -%)(% endcall %)
    (% call string_attr("Name", "name", group.name.clone(), can_modify_any_attr, Attribute::Name) -%)(% endcall %)
    (% let description = group.description.clone().unwrap_or(String::new()) %)
    (% call string_attr("Description", "description", description, can_modify_any_attr, Attribute::Description) -%)(% endcall %)
</form>
    (% if scim_effective_access.search.check(&Attribute::Member) %)
        (% let can_edit_member = can_rw && scim_effective_access.modify_present.check(&Attribute::Member) %)
        <label class="mt-3 fw-bold">Members</label>
        (% if can_edit_member %)
        <form hx-boost="true"
              hx-post="/ui/api/admin/group/(( group.uuid ))/add_member"
              hx-target="#members"
              hx-swap="beforeend">
            <div class="input-group col-12 col-md-8 col-lg-6">
                <input type="text" class="form-control" id="add-member-input" name="member"
                       aria-label="Name or uuid of member to add" placeholder="name or uuid"
                       aria-describedby="add-member-button">
                <button class="btn btn-primary float-end" id="add-member-button">Add member</button>
            </div>
        </form>
        (% endif %)
        <ol class="list-group col-12 col-md-8 col-lg-6" id="members">
            (% for member in group.members %)
                (% let member_name = member.value.clone() %)
                (% let group_uuid = group.uuid.clone() %)
                (% include "admin_group_member_entry_partial.html" %)
            (% endfor %)
        </ol>
    (% endif %)


<div id="toast-container" class="toast-container position-fixed bottom-0 end-0 p-3">
</div>
(% endblock %)
