@page
@model Admin.Pages.Forum.Comments.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="1" selected="@(Model?.Query.Search == 1)">게시글 ID</option>
                <option value="2" selected="@(Model?.Query.Search == 2)">게시글 제목</option>
                <option value="3" selected="@(Model?.Query.Search == 3)">작성자</option>
                <option value="0" selected="@(Model?.Query.Search == 0)">내용</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="isDeleted" checked="@(Model?.Query.IsDeleted == true)" />
                <label class="form-check-label" for="isDeleted">삭제 여부</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: 15%;" />
                <col style="width: 10%;" />
                <col style="width: 8%;" />
                <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>
                </tr>
            </thead>
            <tbody>
                @if (Model.List == null || Model.Total <= 0)
                {
                    <tr>
                        <td colspan="9">No Data.</td>
                    </tr>
                }
                else
                {
                    @foreach (var row in Model.List)
                    {
                        <tr>
                            <td>
                                <div class="form-check form-check-inline">
                                    <input type="checkbox" name="ids[]" id="ids_@row.ID" class="form-check-input list-check-box" value="@row.ID" form="fAdminList" />
                                    <label for="ids_@row.ID">@row.ID</label>
                                </div>
                            </td>
                            <td class="text-start">
                                @if (row.IsDeleted) {
                                    <span class="badge text-bg-danger me-1">삭제</span>
                                }

                                @if (row.IsSecret) {
                                    <span class="badge text-bg-secondary me-1">비밀</span>
                                }

                                @if (row.IsReply) {
                                    <span class="badge text-bg-info me-1">답글</span>
                                }

                                <a href="/Forum/Comments/List/Edit/@row.ID@(Request.QueryString)">
                                    @(row.Content.Length > 80 ? row.Content[..80] + "..." : row.Content)
                                </a>
                            </td>
                            <td class="text-start">
                                <a href="/Forum/Posts/List/Edit/@row.PostID">
                                    @(row.PostSubject.Length > 30 ? row.PostSubject[..30] + "..." : row.PostSubject)
                                </a>
                            </td>
                            <td>@row.BoardName</td>
                            <td>@(row.Name ?? row.SID ?? "-")</td>
                            <td>@row.Likes</td>
                            <td>@row.Dislikes</td>
                            <td>@row.Reports</td>
                            <td>@row.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">
    @Html.AntiForgeryToken()
    <input type="hidden" name="pageNum" value="@Model.Query.PageNum" />
    <input type="hidden" name="perPage" value="@Model.Query.PerPage" />
    <input type="hidden" name="boardID" value="@Model.Query.BoardID" />
    <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="isDeleted" value="@Model.Query.IsDeleted" />
</form>

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

        $(document).on("change", "#perPage", function () {
            searchForm.elements["pageNum"].value = "1";
            searchForm.submit();
        });
    </script>
}