@using bitforum.Helpers;
@{
    ViewData["Title"] = "FAQ 관리";

    var faqCategories = ViewBag.FaqCategories as List<bitforum.Models.Page.Faq.FaqCategory>;
    var faqItems = ViewBag.FaqItems as List<bitforum.Models.Page.Faq.FaqItem>;
    var total = (ViewBag.Total ?? 0).ToString("N0");
    var pagination = ViewBag.Pagination as bitforum.Models.Pagination;
}

<partial name="~/Views/Page/Faq/_Navbar.cshtml" />

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

    <partial name="_StatusMessage" />

    <div class="row g-2 mb-2">
        <div class="col">
            <select name="category_id" id="categoryID" class="form-select w-auto">
                <option value="">선택하세요.</option>
                @foreach (var row in faqCategories)
                {
                    <option value="@row.ID" selected="@(row.ID == ViewBag.CategoryID ? "selected" : null)">@row.Subject (@row.FaqItem.Count)</option>
                }
            </select>
        </div>
    </div>

    <div class="row g-2 align-items-end">
        <div class="col">
            Total : @total
        </div>
        <div class="col text-end">
            <button type="button" id="btnListDelete" class="btn btn-sm btn-danger" form="fAdminList" data-action="/Page/Faq/Item/Delete">삭제</button>
            <a href="/Page/Faq/Item/Write" class="btn btn-sm btn-success">추가</a>
        </div>
    </div>

    <form name="f_admin_list" id="fAdminList" method="post" accept-charset="utf-8" autocomplete="off"></form>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <colgroup>
                <col width="2%" />
                <col width="5%" />
                <col width="30%" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
            </colgroup>
            <thead>
                <tr>
                    <th><input type="checkbox" id="checkedAll" class="form-check-input" value="1" form="fAdminList" /></th>
                    <th>ID</th>
                    <th>질문</th>
                    <th>순서</th>
                    <th>사용</th>
                    <th>조회 수</th>
                    <th>등록일시</th>
                    <th>수정일시</th>
                    <th>비고</th>
                </tr>
            </thead>
            <tbody>
                @if (faqItems == null || !faqItems.Any())
                {
                    <tr>
                        <td colspan="9">No Data.</td>
                    </tr>
                }
                else
                {
                    @foreach (var row in faqItems)
                    {
                        <tr>
                            <td>
                                <input type="checkbox" name="ids[]" class="form-check-input list-check-box" value="@row.ID" form="fAdminList" />
                            </td>
                            <td>@row.ID</td>
                            <td>
                                <span class="badge text-bg-secondary">[@row.FaqCategory.Subject]</span> @row.Question
                            </td>
                            <td>@row.Order</td>
                            <td>@(row.IsActive ? "Y" : "N")</td>
                            <td>@row.Views</td>
                            <td>@row.CreatedAt.GetDateAt()</td>
                            <td>@row.UpdatedAt.GetDateAt()</td>
                            <td>
                                <div class="d-grid gap-2 d-md-block">
                                    <a href="/Page/Faq/Item/@row.ID/Edit" class=" btn btn-sm btn-outline-info">수정</a>
                                    <a href="/Page/Faq/Item/@row.ID/Delete" class="btn btn-sm btn-outline-danger btn-row-delete">삭제</a>
                                </div>
                            </td>
                        </tr>
                    }
                }
            </tbody>
        </table>

        <partial name="_Pagination" model="pagination" />
    </div>
</div>

<script type="module">
    $(document).on("change", "#categoryID", function () {
        location.href = ("/Page/Faq/Item/" + $(this).val());
    });
</script>