@page "{id:int}"
@model Admin.Pages.Forum.Board.Meta.NotifyModel
@using Domain.Entities.Forum.ValueObject
@{
    ViewData["Title"] = "게시판 관리 - 알림";
    ViewData["Sector"] = "Notify";
    ViewData["BoardID"] = Model.BoardID;
    ViewData["BoardList"] = Model.BoardList;
    ViewData["QueryString"] = Model.QueryString;

    var notifyList = Model.NotifyList;
}

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

    <form name="f_admin_write" id="fAdminWrite" method="post" accept-charset="utf-8" autocomplete="off" enctype="multipart/form-data">
        <input type="hidden" asp-for="Input.ID" />
        <input type="hidden" asp-for="Input.BoardID" />

        <h5>이메일 알림</h5>
        <p class="text-muted form-text">이메일 알림이 양식에 맞추어 아래 대상자에게 발송됩니다.</p>

        <div class="row mb-3">
            <label class="col-md-2 col-form-label">게시글 작성 시</label>
            <div class="col-md-10 align-self-center">
                @if (notifyList is not null) {
                    @foreach (var item in notifyList)
                    {
                        var notifyValue = (BoardNotify)Enum.Parse(typeof(BoardNotify), item.Value);
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="PostWriteNotify[]" value="@item.Value" id="postWriteNotify_@item.Value" class="form-check-input" checked="@(((BoardNotify)(Model.Input.PostWriteNotify ?? 0)).HasFlag(notifyValue))" />
                            <label for="postWriteNotify_@item.Value" class="form-check-label">@item.Text</label>
                        </div>
                    }
                }
            </div>
        </div>
         <div class="row mb-3">
            <label class="col-md-2 col-form-label">댓글 작성 시</label>
            <div class="col-md-10 align-self-center">
                @if (notifyList is not null) {
                    @foreach (var item in notifyList)
                    {
                        var notifyValue = (BoardNotify)Enum.Parse(typeof(BoardNotify), item.Value);
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="CommentWriteNotify[]" value="@item.Value" id="commentWriteNotify_@item.Value" class="form-check-input" checked="@(((BoardNotify)(Model.Input.CommentWriteNotify ?? 0)).HasFlag(notifyValue))" />
                            <label for="commentWriteNotify_@item.Value" class="form-check-label">@item.Text</label>
                        </div>
                    }
                }
            </div>
        </div>
        <div class="row mb-3">
            <label for="Notify_ReplyWriteNotify" class="col-md-2 col-form-label">답글 작성 시</label>
            <div class="col-md-10 align-self-center">
                @if (notifyList is not null) {
                    @foreach (var item in notifyList)
                    {
                        var notifyValue = (BoardNotify)Enum.Parse(typeof(BoardNotify), item.Value);
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="ReplyWriteNotify[]" value="@item.Value" id="replyWriteNotify_@item.Value" class="form-check-input" checked="@(((BoardNotify)(Model.Input.ReplyWriteNotify ?? 0)).HasFlag(notifyValue))" />
                            <label for="replyWriteNotify_@item.Value" class="form-check-label">@item.Text</label>
                        </div>
                    }
                }
            </div>
        </div>

        <input type="hidden" name="Input.PostWriteNotify" id="PostWriteNotifyValue" />
        <input type="hidden" name="Input.CommentWriteNotify" id="CommentWriteNotifyValue" />
        <input type="hidden" name="Input.ReplyWriteNotify" id="ReplyWriteNotifyValue" />

        <hr/>
        <div class="d-grid gap-2 text-center d-md-block">
            <button type="submit" class="btn btn-success">저장</button>
            <a href="/Forum/Board/List\@Model.QueryString" class="btn btn-secondary">취소</a>
        </div>
    </form>
</div>

@section Scripts {
    <script>
        document.addEventListener("DOMContentLoaded", function () {
            function calculateFlagValue(groupName) {
                let checkboxes = document.querySelectorAll(`input[name="${groupName}[]"]:checked`);
                let flagValue = 0;

                checkboxes.forEach(checkbox => {
                    flagValue |= parseInt(checkbox.value, 10);
                });

                document.getElementById(`${groupName}Value`).value = flagValue;
            }

            function updateAllFlags() {
                calculateFlagValue("PostWriteNotify");
                calculateFlagValue("CommentWriteNotify");
                calculateFlagValue("ReplyWriteNotify");
            }

            document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
                checkbox.addEventListener("change", updateAllFlags);
            });

            document.getElementById("fAdminWrite").addEventListener("submit", (e) => {
                updateAllFlags();
            });
        });
    </script>
}