@page "{id:int}"
@model Admin.Pages.Forum.Board.ManagerModel
@{
    ViewData["Title"] = "게시판 관리 - 관리자";
    ViewData["Sector"] = "Manager";
    ViewData["BoardID"] = Model.BoardID;
    ViewData["BoardList"] = Model.BoardList;
    ViewData["QueryString"] = Model.QueryString;
}

<div class="container">
    <partial name="_Header" />
    <partial name="_StatusMessage" />
    <partial name="/Pages/Forum/Board/_NavTabs.cshtml" />

    <form id="fAdminWrite" method="post" asp-page-handler="Create" accept-charset="utf-8" autocomplete="off">
        <div class="row g-2">
            <label class="col-lg-1 col-form-label">관리자</label>
            <div class="col-12 col-sm-auto">
                <input type="number" name="Input.MemberID" class="form-control" required placeholder="회원 ID" />
            </div>
            <div class="col-auto col-sm-auto col-form-label">
                <div class="form-check form-check-inline">
                    <input type="checkbox" name="Input.CanEdit" id="CanEdit" class="form-check-input" checked value="true" />
                    <label for="CanEdit" class="form-check-label">수정</label>
                </div>
            </div>
            <div class="col-auto col-sm-auto col-form-label">
                <div class="form-check form-check-inline">
                    <input type="checkbox" name="Input.CanDelete" id="CanDelete" class="form-check-input" checked value="true" />
                    <label for="CanDelete" class="form-check-label">삭제</label>
                </div>
            </div>
            <div class="col-12 col-sm-auto text-center">
                <button type="submit" class="btn btn-primary w-100">등록</button>
            </div>
        </div>
    </form>

    <hr/>

    <div class="row g-2 align-items-end">
        <div class="col">
            Total : @Model.Total.ToString("N0")
        </div>
        <div class="col text-end">
            <button type="button" id="btnFListDelete" class="btn btn-sm btn-danger" form="fAdminList" disabled>삭제</button>
            <button type="submit" id="btnFListSave" class="btn btn-sm btn-success" form="fAdminList" @(Model.Total <= 0 ? "disabled" : "")> 저장</button>
        </div>
    </div>

    <div class="table-responsive">
        <form id="fAdminList" method="post" asp-page-handler="Save" accept-charset="utf-8" autocomplete="off">
            <table class="table table-striped table-bordered table-hover mt-3">
                <caption>
                    게시판을 운영할 수 있는 관리자를 등록합니다.
                </caption>
                <colgroup>
                    <col width="10%"/>
                    <col width="*"/>
                    <col width="12%"/>
                    <col width="12%"/>
                    <col width="15%"/>
                    <col width="15%"/>
                </colgroup>
                <thead>
                    <tr>
                        <th>
                            <div class="form-check form-check-inline">
                                <input type="checkbox" id="checkedAll" class="form-check-input" value="1" />
                                <label for="checkedAll" class="form-check-label">ID</label>
                            </div>
                        </th>
                        <th>관리자</th>
                        <th>수정 권한</th>
                        <th>삭제 권한</th>
                        <th>등록일시</th>
                        <th>수정일시</th>
                    </tr>
                </thead>
                <tbody>
                    @if (Model.Data == null || !Model.Data.Any())
                    {
                        <tr>
                            <td colspan="6">No Data.</td>
                        </tr>
                    }
                    else
                    {
                        @foreach (var row in Model.Data)
                        {
                            var index = Model.Data.IndexOf(row);

                            <tr>
                                <td>
                                    <div class="form-check form-check-inline">
                                        <input type="checkbox" name="CheckList[]" id="CheckList_@index" class="form-check-input list-check-box" value="@row.ID" />
                                        <label for="CheckList_@index" class="form-check-label">@row.ID</label>
                                    </div>

                                    <input type="hidden" name="UpdateItems[@index].ID" value="@row.ID" />
                                </td>
                                <td>@row.MemberEmail</td>
                                <td>
                                    <div class="form-check form-check-inline">
                                        <input type="checkbox" name="UpdateItems[@index].CanEdit" id="UpdateItems_@(index)_CanEdit" class="form-check-input" checked="@row.CanEdit" value="true" />
                                        <label for="UpdateItems_@(index)_CanEdit" class="form-check-label">수정</label>
                                    </div>
                                </td>
                                <td>
                                    <div class="form-check form-check-inline">
                                        <input type="checkbox" name="UpdateItems[@index].CanDelete" id="UpdateItems_@(index)_CanDelete" class="form-check-input" checked="@row.CanDelete" value="true" />
                                        <label for="UpdateItems_@(index)_CanDelete" class="form-check-label">삭제</label>
                                    </div>
                                </td>
                                <td>@row.CreatedAt</td>
                                <td>@(row.UpdatedAt ?? "-")</td>
                            </tr>
                        }
                    }
                </tbody>
            </table>
        </form>
    </div>
</div>

@section Scripts {
<script>
    // 삭제
    $(document).on("click", "#btnFListDelete", function() {
        if (confirm("삭제 하시겠습니까?")) {
            let checked = document.querySelectorAll(".list-check-box:checked");
            if (checked.length === 0) return false;

            checked.forEach(function(el) {
                let form = document.createElement("form");
                form.method = "post";
                form.action = "?handler=Delete";

                let input = document.createElement("input");
                input.type = "hidden";
                input.name = "DeleteID";
                input.value = el.value;

                let token = document.querySelector('input[name="__RequestVerificationToken"]');
                if (token) {
                    let tokenInput = document.createElement("input");
                    tokenInput.type = "hidden";
                    tokenInput.name = "__RequestVerificationToken";
                    tokenInput.value = token.value;
                    form.appendChild(tokenInput);
                }

                form.appendChild(input);
                document.body.appendChild(form);
                form.submit();
            });
        }
        return false;
    });

    // 저장
    $(document).on("click", "#btnFListSave", function() {
        if (confirm("저장 하시겠습니까?")) {
            let form = document.getElementById("fAdminList");
            if (form.checkValidity()) { // HTML5 폼 검증 수행
                form.submit();
            } else {
                form.reportValidity();
            }
        }
        return false;
    });
</script>
}