@page
@model Admin.Pages.Popup.IndexModel
@{
    ViewData["Title"] = "팝업 목록";
}

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

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

    <div class="row g-2 align-items-end mt-2">
        <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="/Popup/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 style="width: 10%;" />
                <col style="width: 20%;" />
                <col />
                <col style="width: 10%;" />
                <col style="width: 10%;" />
                <col style="width: 5%;" />
                <col style="width: 5%;" />
                <col style="width: 10%;" />
                <col style="width: 10%;" />
            </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>
                    <th>관리</th>
                </tr>
            </thead>
            <tbody>
                @if (Model.List == null || Model.Total <= 0)
                {
                    <tr>
                        <td colspan="10">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.PositionCode] @row.PositionSubject</td>
                            <td class="text-start">@row.Subject</td>
                            <td>
                                @if (!string.IsNullOrWhiteSpace(row.Link) && row.Link != "-")
                                {
                                    <a href="@row.Link" target="_blank" rel="external">
                                        <i class="bi bi-box-arrow-up-right"></i> @row.Link
                                    </a>
                                }
                                else
                                {
                                    <text>-</text>
                                }
                            </td>
                            <td>@row.StartAt</td>
                            <td>@row.EndAt</td>
                            <td>@row.Order</td>
                            <td>@(row.IsActive ? "Y" : "N")</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>
                                    <button type="button" class="btn btn-sm btn-outline-danger btn-row-delete" data-id="@row.ID" data-subject="@row.Subject">삭제</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>
}