@model Account
@using Newtonsoft.Json
@using System.Text
@{
    ViewData["Title"] = "Account details";
}

<a href="/">home</a>/@Html.Raw(ViewData["breadcrumbs"])
<table class="table">
    <tbody>
        <tr>
            <th>Id</th>
            <td>@Model.Id</td>
        </tr>
        <tr>
            <th scope="row">belongs to user</th>
            <td>@Model.IsUser.DisplayName</td>
            <td><button onclick="unlinkAccountUser(() => { window.location.reload(); })">separate</button></2td>
        </tr>
        <tr>
            <th scope="row">Seen in channel</th>
            <td class="account @Model.SeenInChannel.Protocol"><div class="protocol-icon">&nbsp;</div>@Model.SeenInChannel.LineageSummary<a href="/Channels/Details/@Model.SeenInChannel.Id">@Model.SeenInChannel.DisplayName</a></td>
        </tr>
        <tr>
            <th scope="row">UACs tied to account</th>
            <td>
                <div id="uacsTree"></div>
            </td>
        </tr>
    </tbody>
</table>

@section Scripts{

    <script type="text/javascript">
        @{
            var accountAsString = JsonConvert.SerializeObject(Model, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
        }
          const userOnLoad = @Html.Raw(accountAsString);
        function jsonifyUser() {
            var userNow = structuredClone(userOnLoad);
            userNow.Accounts = null;
            userNow.DisplayName = document.querySelector("#displayName").value;
            console.log(userNow);
            return userNow;
        }

        function getUacsTree() {
        @{
            var sb = new StringBuilder();
            sb.Append("[{text: \"UACs\", \"expanded\":true, nodes: [");
            var first = true;
            for (int i = 0; i < 1; i++)
            {
                if (!first)
                    sb.Append(',');
                sb.Append($"{{text: \"<input type=\\\"checkbox\\\" > is goated (w/ sauce)</input>\"}}");
                first = false;
            }
            sb.Append("]}]");
        }
            console.log(@Html.Raw(sb.ToString()));
            var tree = @Html.Raw(sb.ToString());
            return tree;
        }
        $('#uacsTree').bstreeview({ data: getUacsTree() });
        //document.querySelectorAll("input[type=checkbox]").forEach(node => { node.onchange = () => { patchModel(jsonifyUser(), '/api/Users/') } });
    </script>
}
