@page
@model Admin.Pages.Forum.Posts.List.IndexModel
@using Microsoft.AspNetCore.Mvc.Rendering
@{
    ViewData["Title"] = "게시물 관리";
}

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

    <partial name="_StatusMessage" />

    <div class="row g-2 align-items-end mb-3">
        <div class="col col-sm-auto">
            <select id="boardID" class="form-select" form="fAdminSearch">
                <option value="">- 전체 -</option>
                @foreach (var g in (Model?.BoardList ?? Enumerable.Empty<SelectListItem>()))
                {
                    <option value="@g.Value" selected="@((Model?.Query?.BoardID?.ToString() ?? "") == (g.Value ?? ""))">@g.Text</option>
                }
            </select>
        </div>
        <div class="col col-sm-auto">
            <select id="search" class="form-select" form="fAdminSearch">
                <option value="">- 전체 -</option>
                <option value="0" selected="@(Model?.Query.Search == 0)">제목</option>
                <option value="1" selected="@(Model?.Query.Search == 1)">내용</option>
                <option value="2" selected="@(Model?.Query.Search == 2)">작성자</option>
            </select>
        </div>
        <div class="col-12 col-sm col-md">
            <input type="text" id="keyword" class="form-control" value="@(Model?.Query.Keyword ?? "")" placeholder="검색어" form="fAdminSearch" />
        </div>
        <div class="col-12 col-sm-12 col-lg-auto">
            <div class="input-group">
                <input type="date" id="startAt" class="form-control" value="@(Model?.Query.StartAt ?? "")" form="fAdminSearch" />
                <span class="input-group-text">~</span>
                <input type="date" id="endAt" class="form-control" value="@(Model?.Query.EndAt ?? "")" form="fAdminSearch" />
            </div>
        </div>
    </div>

    <div class="row g-2 align-items-end">
        <div class="col-auto">
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" id="isNotice" checked="@(Model?.Query.IsNotice == true)" />
                <label class="form-check-label" for="isNotice">공지</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" id="isSecret" checked="@(Model?.Query.IsSecret == true)" />
                <label class="form-check-label" for="isSecret">비밀</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" id="isReply" checked="@(Model?.Query.IsReply == true)" />
                <label class="form-check-label" for="isReply">답글</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" id="isDeleted" checked="@(Model?.Query.IsDeleted == true)" />
                <label class="form-check-label" for="isDeleted">삭제</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" id="isQnA" checked="@(Model?.Query.IsQnA == true)" />
                <label class="form-check-label" for="isQnA">1:1 문의</label>
            </div>
        </div>
    </div>

    <div class="row justify-content-center mt-3">
        <div class="col col-sm-auto">
            <button type="submit" id="btnSearch" class="btn btn-primary w-100" form="fAdminSearch">검색</button>
        </div>
    </div>

    <hr/>

    <div class="row g-2 align-items-end">
        <div class="col">
            Total : @Model?.Total.ToString("N0")
        </div>
        <div class="col-auto">
            <select id="sort" class="form-select w-auto d-inline-block" form="fAdminSearch">
                <option value="0" selected="@(Model?.Query.Sort == 0)">최신순</option>
                <option value="1" selected="@(Model?.Query.Sort == 1)">조회순</option>
                <option value="2" selected="@(Model?.Query.Sort == 2)">댓글순</option>
                <option value="3" selected="@(Model?.Query.Sort == 3)">공감순</option>
            </select>
        </div>
        <div class="col-auto">
            <select name="perPage" id="perPage" class="form-select w-auto d-inline-block" form="fAdminSearch">
                <option value="10" selected="@(Model.Query.PerPage == 10)">10</option>
                <option value="20" selected="@(Model.Query.PerPage == 20)">20</option>
                <option value="50" selected="@(Model.Query.PerPage == 50)">50</option>
                <option value="100" selected="@(Model.Query.PerPage == 100)">100</option>
            </select>
        </div>
        <div class="col-auto">
            <button type="button" id="btnListDelete" class="btn btn-danger" form="fAdminList" disabled>삭제</button>
            <a class="btn btn-success" asp-page="/Forum/Posts/List/Write">추가</a>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <colgroup>
                <col style="width: 5%;" />
                <col />
                <col style="width: 10%;" />
                <col style="width: 6%;" />
                <col style="width: 5%;" />
                <col style="width: 5%;" />
                <col style="width: 5%;" />
                <col style="width: 5%;" />
                <col style="width: 5%;" />
                <col style="width: 5%;" />
                <col style="width: 12%;" />
            </colgroup>
            <thead>
                <tr>
                    <th>
                        <div class="form-check form-check-inline">
                            <input type="checkbox" id="checkedAll" class="form-check-input" value="1" form="fAdminList" />
                            <label for="checkedAll">ID</label>
                        </div>
                    </th>
                    <th>제목</th>
                    <th>작성자</th>
                    <th>조회</th>
                    <th>공감</th>
                    <th>비공감</th>
                    <th>이미지</th>
                    <th>미디어</th>
                    <th>파일</th>
                    <th>태그</th>
                    <th>작성일</th>
                </tr>
            </thead>
            <tbody>
                @if (Model?.Data == null || Model.Data.Count == 0)
                {
                    <tr>
                        <td colspan="9">No Data.</td>
                    </tr>
                }
                else
                {
                    foreach (var item in Model.Data)
                    {
                        <tr class="@(item.IsNotice ? "table-warning" : "")">
                            <td>
                                <div class="form-check form-check-inline">
                                    <input type="checkbox" name="ids[]" id="ids_@item.ID" class="form-check-input list-check-box" value="@item.ID" form="fAdminList" />
                                    <label for="ids_@item.ID">@item.ID</label>
                                </div>
                            </td>
                            <td class="text-start">
                                @if (item.BoardPrefixID != null)
                                {
                                    <a href="?BoardPrefixID=@item.BoardPrefixID" class="text-success text-decoration-none fw-bold">[@item.BoardPrefixName]</a>
                                }
                                <a class="text-decoration-none" href="@item.ViewURL">
                                    @if (item.IsDeleted)
                                    {
                                        <del>@item.Subject</del>
                                    }
                                    else
                                    {
                                        @item.Subject
                                    }
                                </a>
                                <span class="text-danger">[@item.Comments]</span>
                                @if (item.IsSpeaker)
                                {
                                    <i class="bi bi-megaphone-fill"></i>
                                }
                                @if (item.IsNotice)
                                {
                                    <i class="bi bi-bell-fill"></i>
                                }
                                @if (item.IsSecret)
                                {
                                    <i class="bi bi-lock-fill"></i>
                                }
                                @if (item.IsQnA)
                                {
                                    <i class="bi bi-question-circle-fill"></i>

                                    @if (item.IsReply)
                                    {
                                        <i class="bi bi-person-raised-hand"></i>
                                    }
                                }
                                @if (item.IsAnonymous)
                                {
                                    <i class="bi bi-person-fill"></i>
                                }
                                @if (item.IsDeleted)
                                {
                                    <i class="bi bi-trash3-fill"></i>
                                }
                            </td>
                            <td>@(item.Name ?? item.SID ?? "-")</td>
                            <td class="text-end">@item.Views</td>
                            <td class="text-end">@item.Likes</td>
                            <td class="text-end">@item.Dislikes</td>
                            <td class="text-end">@item.Images</td>
                            <td class="text-end">@item.Medias</td>
                            <td class="text-end">@item.Files</td>
                            <td class="text-end">@item.Tags</td>
                            <td>@item.CreatedAt</td>
                        </tr>
                    }
                }
            </tbody>
        </table>

        <partial name="_Pagination" model="@Model!.Pagination" />
    </div>
</div>

<!-- 검색을 위한 -->
<form id="fAdminSearch" method="get" accept-charset="utf-8">
    <input type="hidden" name="pageNum" value="@Model.Query.PageNum" />
</form>

<!-- 삭제를 위한 -->
<form id="fAdminList" method="post" accept-charset="utf-8" asp-page-handler="Delete">
    @Html.AntiForgeryToken()
    <input type="hidden" name="boardID" value="@Model.Query.BoardID" />
    <input type="hidden" name="boardPrefixID" value="@Model.Query.BoardPrefixID" />
    <input type="hidden" name="search" value="@Model.Query.Search" />
    <input type="hidden" name="keyword" value="@Model.Query.Keyword" />
    <input type="hidden" name="startAt" value="@Model.Query.StartAt" />
    <input type="hidden" name="endAt" value="@Model.Query.EndAt" />
    <input type="hidden" name="sort" value="@Model.Query.Sort" />
    <input type="hidden" name="isNotice" value="@Model.Query.IsNotice" />
    <input type="hidden" name="isSecret" value="@Model.Query.IsSecret" />
    <input type="hidden" name="isReply" value="@Model.Query.IsReply" />
    <input type="hidden" name="isDeleted" value="@Model.Query.IsDeleted" />
    <input type="hidden" name="pageNum" value="@Model.Query.PageNum" />
    <input type="hidden" name="perPage" value="@Model.Query.PerPage" />
</form>

@section Scripts {
    <script>
        let searchForm = document.getElementById("fAdminSearch");

        $(document).on("change", "#sort, #perPage", function () {
            document.getElementById("btnSearch").click();
        });

        document.getElementById("btnSearch")?.addEventListener("click", function (e) {
            e.preventDefault();
            const qp = new URLSearchParams();

            const boardID = document.getElementById("boardID").value;
            if (boardID) {
                qp.set("BoardID", boardID);
            }

            const searchVal = document.getElementById("search").value;
            if (searchVal !== "") {
                qp.set("Search", searchVal);
            }

            const keyword = document.getElementById("keyword").value;
            if (keyword) {
                qp.set("Keyword", keyword);
            }

            const startAt = document.getElementById("startAt").value;
            if (startAt) {
                qp.set("StartAt", startAt);
            }

            const endAt = document.getElementById("endAt").value;
            if (endAt) {
                qp.set("EndAt", endAt);
            }

            const sortVal = document.getElementById("sort").value;
            if (sortVal !== "") {
                qp.set("Sort", sortVal);
            }

            if (document.getElementById("isNotice").checked) qp.set("IsNotice", "true");
            if (document.getElementById("isSecret").checked) qp.set("IsSecret", "true");
            if (document.getElementById("isReply").checked) qp.set("IsReply", "true");
            if (document.getElementById("isDeleted").checked) qp.set("IsDeleted", "true");
            if (document.getElementById("isQnA").checked) qp.set("isQnA", "true");

            qp.set("PerPage", document.getElementById("perPage").value || "20");
            qp.set("PageNum", "1");

            window.location.href = window.location.pathname + "?" + qp.toString();
        });
    </script>
}