@page
@model Admin.Pages.Forum.Reports.Comment.IndexModel
@using Microsoft.AspNetCore.Mvc.Rendering
@using Domain.Entities.Forum.ValueObject
@{
    ViewData["Title"] = "댓글 신고 관리";
}

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

    <partial name="_StatusMessage" />

    <div class="row g-2 align-items-end">
        <div class="col-6 col-sm-auto">
            <select name="boardID" 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-6 col-sm-auto">
            <select name="search" 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>
            </select>
        </div>
        <div class="col-12 col-sm col-md col-lg-auto">
            <input type="text" name="keyword" id="keyword" class="form-control" value="@(Model?.Query.Keyword ?? "")" placeholder="검색어" form="fAdminSearch" />
        </div>
        <div class="col-12 col-md-12 col-lg-auto">
            <div class="input-group">
                <input type="date" name="startAt" id="startAt" class="form-control" value="@(Model?.Query.StartAt ?? "")" form="fAdminSearch" />
                <span class="input-group-text">~</span>
                <input type="date" name="endAt" id="endAt" class="form-control" value="@(Model?.Query.EndAt ?? "")" form="fAdminSearch" />
            </div>
        </div>
        <div class="col-6 col-sm col-lg-auto">
            <select name="status" id="status" class="form-select" form="fAdminSearch">
                <option value="">- 상태 -</option>
                <option value="0" selected="@(Model?.Query.Status == 0)">접수</option>
                <option value="1" selected="@(Model?.Query.Status == 1)">처리중</option>
                <option value="2" selected="@(Model?.Query.Status == 2)">완료</option>
            </select>
        </div>
        <div class="col-6 col-sm col-lg-auto">
            <select name="type" id="type" class="form-select" form="fAdminSearch">
                <option value="">- 유형 -</option>
                <option value="1" selected="@(Model?.Query.Type == 1)">욕설</option>
                <option value="2" selected="@(Model?.Query.Type == 2)">음란</option>
                <option value="3" selected="@(Model?.Query.Type == 3)">불법</option>
                <option value="4" selected="@(Model?.Query.Type == 4)">사칭</option>
                <option value="5" selected="@(Model?.Query.Type == 5)">현금거래유도</option>
                <option value="6" selected="@(Model?.Query.Type == 6)">스팸/광고</option>
                <option value="7" selected="@(Model?.Query.Type == 7)">도배</option>
                <option value="8" selected="@(Model?.Query.Type == 8)">개인정보노출</option>
                <option value="9" selected="@(Model?.Query.Type == 9)">기타</option>
            </select>
        </div>
        <div class="col-12 col-md-auto text-center">
            <button type="submit" id="btnSearch" class="btn btn-primary" form="fAdminSearch">검색</button>
        </div>
    </div>

    <hr />

    <div class="row g-2 align-items-center mt-2">
        <div class="col">
            Total : @Model?.Total.ToString("N0")
        </div>
        <div class="col-auto">
            <select name="perPage" id="perPage" class="form-select" 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" disabled>삭제</button>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-bordered mt-3">
            <colgroup>
                <col style="width: 5%;" />   <!-- ID -->
                <col style="width: 8%;" />   <!-- 게시판 -->
                <col style="width: 8%;" />   <!-- 유형 -->
                <col style="width: 7%;" />   <!-- 상태 -->
                <col />                      <!-- 댓글 내용 -->
                <col style="width: 7%;" />   <!-- 댓글ID -->
                <col style="width: 10%;" />  <!-- 신고자 -->
                <col style="width: 10%;" />  <!-- 사유 -->
                <col style="width: 10%;" />  <!-- 등록일 -->
            </colgroup>
            <thead>
                <tr>
                    <th rowspan="2">
                        <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 rowspan="2">게시판</th>
                    <th colspan="3">댓글</th>
                    <th rowspan="2">게시글 ID</th>
                    <th rowspan="2">신고자</th>
                    <th rowspan="2">사유</th>
                    <th rowspan="2">등록일</th>
                </tr>
                <tr>
                    <th>유형</th>
                    <th>상태</th>
                    <th>메모</th>
                </tr>
            </thead>
            <tbody>
                @if (Model.List == null || Model.Total <= 0)
                {
                    <tbody>
                        <tr>
                            <td colspan="9">No Data.</td>
                        </tr>
                    </tbody>
                }
                else
                {
                    var typeLabels = new Dictionary<ReportType, string> {
                        { ReportType.None, "-" },
                        { ReportType.Abuse, "욕설" },
                        { ReportType.Obscene, "음란" },
                        { ReportType.Illegal, "불법" },
                        { ReportType.Impersonation, "사칭" },
                        { ReportType.CashTrade, "현금거래" },
                        { ReportType.SpamAd, "스팸/광고" },
                        { ReportType.Flood, "도배" },
                        { ReportType.PersonalLeak, "개인정보" },
                        { ReportType.Other, "기타" }
                    };

                    @foreach (var row in Model.List)
                    {
                        <tbody class="striped">
                            <tr>
                                <td rowspan="2">
                                    <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 rowspan="2">@row.BoardName</td>
                                <td colspan="3" class="text-start">
                                    [@row.CommentID] @row.Comment
                                </td>
                                <td rowspan="2">
                                    <a href="/Forum/Posts/List/Edit/@row.PostID">
                                        @row.PostID
                                    </a>
                                </td>
                                <td rowspan="2">@(row.MemberName ?? $"ID:{row.MemberID}")</td>
                                <td rowspan="2">@row.Reason</td>
                                <td rowspan="2">@row.CreatedAt</td>
                            </tr>
                            <tr>
                                <td><span class="badge text-bg-warning">@(typeLabels.GetValueOrDefault(row.Type, "-"))</span></td>
                                <td>
                                    @if (row.Status == ReportStatus.Received)
                                    {
                                        <span class="badge text-bg-danger">접수</span>
                                    }
                                    else if (row.Status == ReportStatus.Processing)
                                    {
                                        <span class="badge text-bg-warning">처리중</span>
                                    }
                                    else if (row.Status == ReportStatus.Completed)
                                    {
                                        <span class="badge text-bg-success">완료</span>
                                    }
                                </td>
                                <td>@row.Memo</td>
                            </tr>
                        </tbody>
                    }
                }
            </tbody>
        </table>

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

    <div class="row g-2 align-items-center">
        <div class="col-auto">
            <select name="status" class="form-select w-auto d-inline-block" form="fAdminList">
                <option value="0">접수</option>
                <option value="1">처리중</option>
                <option value="2">완료</option>
            </select>
        </div>
        <div class="col-auto">
            <input type="text" name="memo" class="form-control" style="width:250px" placeholder="메모 (선택)" form="fAdminList" />
        </div>
        <div class="col-auto">
            <button type="button" id="btnUpdateStatus" class="btn btn-primary">상태 변경</button>
        </div>
    </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="status" value="@Model.Query.Status" />
    <input type="hidden" name="type" value="@Model.Query.Type" />
</form>

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

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

        document.getElementById("btnUpdateStatus")?.addEventListener("click", function () {
            let form = document.getElementById("fAdminList");
            form.action = "?handler=UpdateStatus";
            form.submit();
        });
    </script>
}