@page
@model Admin.Pages.Crypto.News.IndexModel
@{
    ViewData["Title"] = "뉴스 관리";
}

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

    <partial name="_StatusMessage" />

    <ul class="nav nav-tabs mt-2">
        <li class="nav-item">
            <a class="nav-link active" href="/Crypto/News">RSS 소스</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/Crypto/News/Articles">수집된 기사</a>
        </li>
    </ul>

    <div class="row g-2 align-items-end mt-3">
        <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="/Crypto/News/Write">추가</a>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <colgroup>
                <col />
                <col />
                <col />
                <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>URL</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.Num</label>
                                </div>
                            </td>
                            <td>@row.Name</td>
                            <td><a href="@row.Url" target="_blank" rel="noopener">@row.Url</a></td>
                            <td class="text-center">@row.IntervalMinutes</td>
                            <td class="text-center @(row.IsActive == 'Y' ? "text-success" : "text-muted")">@row.IsActive</td>
                            <td>@row.LastFetchedAt</td>
                            <td>@row.CreatedAt</td>
                            <td>
                                <div class="d-xl-flex gap-2 justify-content-center d-grid">
                                    <a class="btn btn-sm btn-outline-info" href="@row.EditURL">편집</a>
                                    <form method="post" asp-page-handler="ForceFetch" class="d-inline">
                                        @Html.AntiForgeryToken()
                                        <input type="hidden" name="id" value="@row.ID" />
                                        <button type="submit" class="btn btn-sm btn-outline-warning" onclick="return confirm('이 소스를 강제 수집하시겠습니까?')">수집</button>
                                    </form>
                                    <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" />
</form>

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

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