@page
@model Admin.Pages.Member.Wallet.List.IndexModel
@{
    ViewData["Title"] = "지갑 관리";
}

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

    <partial name="_StatusMessage" />

    <div class="row g-2 mb-2">
        <div class="col-12 col-lg-auto">
            <div class="row g-2">
                <div class="col-auto col-md-auto">
                    <select id="search" class="form-select">
                        <option value="1" selected="@(Model.Parameter.Search == 1)">회원 ID</option>
                        <option value="2" selected="@(Model.Parameter.Search == 2)">회원 별명</option>
                        <option value="3" selected="@(Model.Parameter.Search == 3)">회원 이메일</option>
                        <option value="4" selected="@(Model.Parameter.Search == 4)">지갑 ID</option>
                    </select>
                </div>
                <div class="col col-md-auto">
                    <input type="search" id="keyword" class="form-control" maxlength="100" value="@Model.Parameter.Keyword" />
                </div>
            </div>
        </div>
        <div class="col-12 col-sm">
            <div class="row g-2">
                <div class="col-12 col-md-auto">
                    <div class="row row-cols-2 g-2">
                        <div class="col">
                            <input type="datetime-local" name="startAt" id="startAt" class="form-control" value="@Model.Parameter.StartAt" />
                        </div>
                        <div class="col d-none">
                            ~
                        </div>
                        <div class="col">
                            <input type="datetime-local" name="endAt" id="endAt" class="form-control" value="@Model.Parameter.EndAt" />
                        </div>
                    </div>
                </div>
                <div class="col col-md-auto text-center">
                    <button type="submit" id="btnSearch" class="btn btn-primary w-100">검색</button>
                </div>
            </div>
        </div>
    </div>

    <hr />

    <div class="row g-2 align-items-end">
        <div class="col">
            Total : @Model.Total.ToString("N0")
        </div>
        <div class="col text-end">
            <select name="per_page" id="perPage" class="form-select w-auto d-inline-block" form="fAdminList">
                <option value="10" selected="@(Model.Parameter.PerPage == 10)">10</option>
                <option value="20" selected="@(Model.Parameter.PerPage == 20)">20</option>
                <option value="50" selected="@(Model.Parameter.PerPage == 50)">50</option>
                <option value="100" selected="@(Model.Parameter.PerPage == 100)">100</option>
            </select>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <colgroup>
                <col style="width: 5%;" />
                <col />
                <col />
                <col />
                <col />
                <col />
                <col />
                <col />
            </colgroup>
            <thead>
                <tr>
                    <th>No</th>
                    <th>지갑 ID</th>
                    <th>회원</th>
                    <th>보유 P</th>
                    <th>후원 받은 P</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>@row.Num</td>
                        <td>@row.ID</td>
                        <td>[@row.MemberID] @row.MemberEmail, @row.MemberName</td>
                        <td>@row.Balance</td>
                        <td>@row.DonationBalance</td>
                        <td>@row.UpdatedAt</td>
                        <td>@row.CreatedAt</td>
                        <td>
                            <a class="btn btn-sm btn-outline-success" href="@row.ChargeURL">충전</a>
                        </td>
                    </tr>
                }
            }
            </tbody>
        </table>

        <partial name="_Pagination" model="Model.Pagination" />
    </div>

    <div>
        <ul class="form-text text-muted">
            <li>회원 ID는 회원 번호(PK)로 조회가 가능합니다.</li>
            <li>지갑 ID는 지갑 번호(PK)로 조회가 가능합니다.</li>
        </ul>
    </div>
</div>

@section Scripts {
    <script>
        function updateQueryString() {
            let queryParams = new URLSearchParams();

            queryParams.set("search", document.getElementById("search").value);
            queryParams.set("keyword", document.getElementById("keyword").value);
            queryParams.set("startAt", document.getElementById("startAt").value);
            queryParams.set("endAt", document.getElementById("endAt").value);
            queryParams.set("perPage", document.getElementById("perPage").value);

            window.location.href = window.location.pathname + "?" + queryParams.toString();
        }

        $(document).on("change", "#perPage", function () {
            updateQueryString();
        });

        $(document).on("click", "#btnSearch", function(e) {
            e.preventDefault();
            updateQueryString();
        });

        $(document).on("keyup", "#keyword, #startAt, #endAt", function(e) {
            if (e.which === 13 || e.key === "Enter") {
                e.preventDefault();
                updateQueryString();
            }
        });
    </script>
}