@page
@model Admin.Pages.Channel.List.IndexModel
@{
    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="search" id="search" class="form-select" form="fAdminSearch">
                <option value="1" selected="@(Model.Query.Search == 1)">채널 이름</option>
                <option value="2" selected="@(Model.Query.Search == 2)">채널 SID</option>
                <option value="3" selected="@(Model.Query.Search == 3)">핸들</option>
                <option value="4" selected="@(Model.Query.Search == 4)">YouTube URL</option>
                <option value="5" selected="@(Model.Query.Search == 5)">회원 ID</option>
                <option value="6" selected="@(Model.Query.Search == 6)">회원 이메일</option>
                <option value="7" selected="@(Model.Query.Search == 7)">회원 이름</option>
            </select>
        </div>
        <div class="col-6 col-sm-auto">
            <select name="isVerified" id="isVerified" class="form-select" form="fAdminSearch">
                <option value="">인증 전체</option>
                <option value="false" selected="@(Model.Query.IsVerified.HasValue && Model.Query.IsVerified == false)">미인증</option>
                <option value="true" selected="@(Model.Query.IsVerified.HasValue && Model.Query.IsVerified == true)">인증</option>
            </select>
        </div>
        <div class="col-12 col-sm col-md col-lg-auto">
            <input type="text" name="keyword" id="keyword" class="form-control" maxlength="100" 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-12 col-lg-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-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="/Channel/List/Write">추가</a>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <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>SID</th>
                    <th>이름</th>
                    <th>핸들</th>
                    <th>소유자</th>
                    <th>인증</th>
                    <th>사용</th>
                    <th>등록일시</th>
                </tr>
            </thead>
            <tbody>
                @if (Model.List.Count == 0)
                {
                    <tr>
                        <td colspan="9">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.SID</td>
                            <td><a href="/Channel/List/Edit/@row.ID">@row.Name</a></td>
                            <td>@row.Handle</td>
                            <td>[@row.MemberID] @row.MemberName</td>
                            <td>@row.IsVerified</td>
                            <td>@row.IsActive</td>
                            <td>@row.CreatedAt</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="1" />
    <input type="hidden" name="perPage" value="@Model.Query.PerPage" />
</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="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="isVerified" value="@Model.Query.IsVerified" />
</form>

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

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