@page
@model Admin.Pages.Member.Wallet.Transactions.IndexModel
@using Domain.Entities.Wallets.ValueObject
@{
    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.Query.Search == 1)">회원 이메일</option>
                        <option value="2" selected="@(Model.Query.Search == 2)">거래 ID</option>
                        <option value="3" selected="@(Model.Query.Search == 3)">지갑 ID</option>
                        <option value="4" selected="@(Model.Query.Search == 4)">회원 ID</option>
                        <option value="5" selected="@(Model.Query.Search == 5)">회원 별명</option>
                    </select>
                </div>
                <div class="col col-md-auto">
                    <input type="search" id="keyword" class="form-control" maxlength="100" value="@Model.Query.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.Query.StartAt" />
                        </div>
                        <div class="col d-none">
                            ~
                        </div>
                        <div class="col">
                            <input type="datetime-local" name="endAt" id="endAt" class="form-control" value="@Model.Query.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 mb-3">
        <div class="col">
            Total : @Model.Total
        </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">
            <environment include="Local,Development">
                <button type="button" id="btnListDelete" class="btn btn-danger" disabled>삭제</button>
            </environment>
        </div>
    </div>

    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == null ? "active" : null)" href="/Member/Wallet/Transactions">전체(@Model.Total)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.Charge ? "active" : null)" href="/Member/Wallet/Transactions?Type=1">충전(@Model.TotalCharge)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.DonationIn ? "active" : null)" href="/Member/Wallet/Transactions?Type=2">후원 받음(@Model.TotalDonationIn)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.DonationOut ? "active" : null)" href="/Member/Wallet/Transactions?Type=3">후원 보냄(@Model.TotalDonationOut)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.RewardEarned ? "active" : null)" href="/Member/Wallet/Transactions?Type=4">보상 적립(@Model.TotalRewardEarned)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.Spend ? "active" : null)" href="/Member/Wallet/Transactions?Type=5">사용(@Model.TotalSpend)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.Refund ? "active" : null)" href="/Member/Wallet/Transactions?Type=6">환불(@Model.TotalRefund)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.Lock ? "active" : null)" href="/Member/Wallet/Transactions?Type=7">잠금(@Model.TotalLock)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.Unlock ? "active" : null)" href="/Member/Wallet/Transactions?Type=8">잠금 해제(@Model.TotalUnlock)</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Type == WalletTransactionType.Adjusted ? "active" : null)" href="/Member/Wallet/Transactions?Type=9">조정(@Model.TotalAdjusted)</a>
        </li>
    </ul>

    <form name="f_admin_list" id="fAdminList" method="post" accept-charset="utf-8" autocomplete="off">
        <input type="hidden" name="type" id="type" value="@((int?)Model.Query.Type)" />
    </form>

    <div class="table-responsive">
        <table class="table table-bordered table-hover mt-3">
            <colgroup>
                <col style="width: 5%;" />
                <col />
                <col />
                <col />
                <col />
                <col />
                <col />
                <col />
                <col />
            </colgroup>
            <thead>
                <tr>
                    <th>No</th>
                    <th>
                        <div class="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>
                </tr>
            </thead>
            @if (Model.List == null || Model.List.Count <= 0)
            {
                <tbody>
                    <tr>
                        <td colspan="9">No Data.</td>
                    </tr>
                </tbody>
            }
            else
            {
                @foreach (var row in Model.List)
                {
                    <tbody>
                        <tr>
                            <td>@row.Num</td>
                            <td>
                                <div>
                                    <input type="checkbox" name="checkList[]" id="chk_@row.ID" class="form-check-input list-check-box" value="@row.ID" form="fAdminList" />
                                    <label for="chk_@row.ID" class="form-check-inline">@row.ID</label>
                                </div>
                            </td>
                            <td>[@row.MemberID] @row.MemberEmail, @row.MemberName</td>
                            <td>@row.TxType</td>
                            <td>@row.BalanceType</td>
                            <td>@row.Amount</td>
                            <td>@row.BalanceAfter</td>
                            <td>@row.CreatedAt</td>
                            <td>
                                <environment include="Local,Development">
                                <div class="d-xl-flex gap-2 justify-content-center d-grid">
                                    <a class="btn btn-sm btn-outline-info" href="@row.ViewURL">상세</a>
                                    <button type="button" class="btn btn-sm btn-outline-danger btn-row-delete" data-id="@row.ID">삭제</button>
                                </div>
                                </environment>
                            </td>
                        </tr>
                    </tbody>
                }
            }
        </table>

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

    <div>
        <ul class="form-text text-muted">
            <li>거래 장부 삭제 시 거래 유형에 맞는 증감 또는 차감 처리가 이루어 집니다.</li>
            <li>거래 후 잔액은 거래 시점의 해당 잔액 구분 잔액입니다.</li>
            <li><u>삭제 전 지갑 정보를 자세히 확인 후 삭제하실 것을 권장합니다.</u></li>
        </ul>
    </div>
</div>

@section Scripts {
    <script>
        function updateQueryString() {
            let queryParams = new URLSearchParams();
            let type = document.getElementById("type").value;

            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);
            if (type) queryParams.set("type", type);

            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>
}