@page
@model Admin.Pages.Faq.List.IndexModel
@{
    ViewData["Title"] = "FAQ 관리";
}

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

    <partial name="_StatusMessage" />
    <partial name="_NavTabs" />

    <div class="row g-2 mb-2 mb-sm-0 mt-2">
        <div class="col-auto">
            <select name="categoryID" id="categoryID" class="form-select w-auto" form="fAdminSearch">
                <option value="">선택하세요.</option>
                @foreach ((int ID, string Code, string Subject, int FaqItemRows) row in Model.Categories)
                {
                    <option value="@row.ID" selected="@(row.ID == Model.Query.CategoryID)">[@row.Code] @row.Subject (@row.FaqItemRows)</option>
                }
            </select>
        </div>
        <div class="col col-sm-auto">
            <input type="search" name="keyword" class="form-control" value="@Model.Query.Keyword" placeholder="질문" form="fAdminSearch"/>
        </div>
        <div class="col-auto col-sm">
            <button type="submit" class="btn btn-success" form="fAdminSearch">검색</button>
        </div>
    </div>

    <div class="row g-2 align-items-end">
        <div class="col">
            Total : @Model.Total.ToString("N0")
        </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="/Faq/List/Write">추가</a>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <colgroup>
                <col />
                <col style="width: 10%" />
                <col style="width: 30%" />
                <col />
                <col />
                <col />
                <col />
                <col />
            </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>
                </tr>
            </thead>
            <tbody>
                @if (Model.List == null || Model.List.Count <= 0)
                {
                    <tr>
                        <td colspan="8">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>[@row.CategoryCode] @row.CategorySubject</td>
                            <td>@row.Question</td>
                            <td>@row.Order</td>
                            <td>@row.IsActive</td>
                            <td>@row.CreatedAt</td>
                            <td>@row.UpdatedAt</td>
                            <td>
                                <div class="d-grid gap-2 d-block d-xxl-inline">
                                    <a class="btn btn-sm btn-outline-info" href="@row.EditURL">수정</a>
                                    <button type="button" class="btn btn-sm btn-outline-danger btn-row-delete" data-id="@row.ID">삭제</button>
                                </div>
                            </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="pageNum" value="@Model.Query.PageNum" />
    <input type="hidden" name="perPage" value="@Model.Query.PerPage" />
    <input type="hidden" name="categoryID" value="@Model.Query.CategoryID" />
    <input type="hidden" name="keyword" value="@Model.Query.Keyword" />
</form>

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

        $(document).on("change", "#categoryID, #perPage", function () {
           searchForm.submit();
        });
    </script>
}