@page
@model Admin.Pages.Director.User.RolesModel
@{
    ViewData["Title"] = "권한 관리";
}

<div class="container">
    <h3>@ViewData["Title"]</h3>
    <hr />

    <partial name="_StatusMessage" />
    <small>사용자에게 권한이 지정된 역할을 부여합니다.</small>

    <form name="f_admin_write" id="fAdminWrite" class="mt-3" method="post" accept-charset="utf-8" autocomplete="off">
        <input type="hidden" asp-for="Input.User.ID" />

        <h5>@Model.Input!.User.FullName</h5>
        <hr />

        @if (Model.Input.Roles == null || Model.Input.Roles.Count <= 0)
        {
            <div class="text-center">
                부여된 권한이 없습니다.
            </div>
        }

        <div class="form-check">
            <input class="form-check-input" type="checkbox" value="1" id="allChecked" />
            <label class="form-check-label" for="allChecked">
                모두 선택
            </label>
        </div>

        @if (Model.Input.Roles != null) {
            @for (int i = 0; i < Model.Input.Roles.Count; i++)
            {
                <div class="form-check m-1">
                    <input type="hidden" asp-for="@Model.Input.Roles[i].DisplayValue" />
                    <input asp-for="@Model.Input.Roles[i].IsSelected" class="form-check-input" />
                    <label asp-for="@Model.Input.Roles[i].IsSelected" class="form-check-label">
                        @Model.Input.Roles[i].DisplayValue
                    </label>
                </div>
            }
        }

        <div asp-validation-summary="All" class="text-danger"></div>
        <hr />

        <div class="text-center">
            @if (Model.Input.Roles != null && Model.Input.Roles.Count >= 0)
            {
                <button type="submit" class="btn btn-success">저장</button>
            }
            <a asp-page="/Director/User/Index" class="btn btn-secondary">취소</a>
        </div>
    </form>
</div>

@section Scripts {
    <script>
        // checkbox 모두 선택/해제
        document.getElementById("allChecked").addEventListener("change", function () {
            var checkboxes = document.querySelectorAll("input[type='checkbox']:not(#allChecked)");
            for (var i = 0; i < checkboxes.length; i++) {
                checkboxes[i].checked = this.checked;
            }
        });
    </script>
}