@page
@model Admin.Pages.Crypto.List.IndexModel
@{
    ViewData["Title"] = "코인 목록";
}

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

    <partial name="_StatusMessage" />

    <div class="row g-2 mb-2 mb-sm-0 mt-2">
        <div class="col-6 col-sm-4 col-lg-auto">
            <select name="categoryID" id="categoryID" class="form-select" form="fAdminSearch">
                <option value="">카테고리 전체</option>
                @foreach (var item in Model.Categories)
                {
                    <option value="@item.Value" selected="@(item.Value == Model.Query.CategoryID?.ToString())">@item.Text</option>
                }
            </select>
        </div>
        <div class="col-6 col-sm-4 col-lg-auto">
            <select name="isActive" id="isActive" class="form-select" form="fAdminSearch">
                <option value="">활성 전체</option>
                <option value="true" selected="@(Model.Query.IsActive == true)">활성</option>
                <option value="false" selected="@(Model.Query.IsActive == false)">비활성</option>
            </select>
        </div>
        <div class="col-6 col-sm-4 col-lg-auto">
            <select name="isWarning" id="isWarning" class="form-select" form="fAdminSearch">
                <option value="">경고 전체</option>
                <option value="true" selected="@(Model.Query.IsWarning == true)">경고</option>
                <option value="false" selected="@(Model.Query.IsWarning == false)">정상</option>
            </select>
        </div>
        <div class="col-6 col-sm-auto">
            <select name="isDelisted" id="isDelisted" class="form-select" form="fAdminSearch">
                <option value="">상폐 전체</option>
                <option value="true" selected="@(Model.Query.IsDelisted == true)">상장폐지</option>
                <option value="false" selected="@(Model.Query.IsDelisted == false)">상장중</option>
            </select>
        </div>
        <div class="col col-sm col-lg-auto">
            <input type="search" name="keyword" class="form-control" value="@Model.Query.Keyword" placeholder="심볼, 이름" form="fAdminSearch" />
        </div>
        <div class="col-12 col-md-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="submit" class="btn btn-warning" form="fSyncForm" onclick="return confirm('Upbit 마켓 데이터를 동기화하시겠습니까?')">Upbit 동기화</button>
            <button type="button" id="btnListDelete" class="btn btn-danger" form="fAdminList" disabled>삭제</button>
            <a class="btn btn-success" asp-page="/Crypto/List/Write">추가</a>
        </div>
    </div>

    @{
        var tabQuery = new Dictionary<string, string?>(
            Request.Query
                .Where(q => !q.Key.Equals("tab", StringComparison.OrdinalIgnoreCase) && !q.Key.Equals("pageNum", StringComparison.OrdinalIgnoreCase))
                .Select(q => new KeyValuePair<string, string?>(q.Key, q.Value.ToString()))
        );

        string TabHref(ushort tab)
        {
            tabQuery["tab"] = tab.ToString();
            return $"/Crypto/List?{string.Join("&", tabQuery.Select(kv => $"{kv.Key}={Uri.EscapeDataString(kv.Value ?? "")}"))}";
        }
    }

    <ul class="nav nav-tabs mt-2">
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Tab == 0 ? "active" : null)" href="@TabHref(0)">전체(@Model.AllCount.ToString("N0"))</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Tab == 1 ? "active" : null)" href="@TabHref(1)">KRW(@Model.KrwCount.ToString("N0"))</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Tab == 2 ? "active" : null)" href="@TabHref(2)">BTC(@Model.BtcCount.ToString("N0"))</a>
        </li>
        <li class="nav-item">
            <a class="nav-link @(Model.Query.Tab == 3 ? "active" : null)" href="@TabHref(3)">USDT(@Model.UsdtCount.ToString("N0"))</a>
        </li>
    </ul>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <colgroup>
                <col />
                <col style="width: 6%;" />
                <col />
                <col style="width: 12%;" />
                <col style="width: 12%;" />
                <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>거래쌍</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.List.Count <= 0)
                {
                    <tr>
                        <td colspan="12">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 class="text-center">
                                @if (!string.IsNullOrEmpty(row.LogoImage))
                                {
                                    <img src="@row.LogoImage" alt="@row.Symbol" style="width:100%;height:auto;object-fit:contain;" />
                                }
                                else
                                {
                                    <span class="text-muted">-</span>
                                }
                            </td>
                            <td class="fw-bold">@row.Market</td>
                            <td>@row.KorName</td>
                            <td>@row.EngName</td>
                            <td>
                                @if (row.CategoryNames != null && row.CategoryNames.Count > 0)
                                {
                                    @string.Join(", ", row.CategoryNames)
                                }
                                else
                                {
                                    <span class="text-muted">-</span>
                                }
                            </td>
                            <td class="text-center @(row.IsActive == 'Y' ? "text-success" : "text-muted")">@row.IsActive</td>
                            <td class="text-center @(row.IsWarning == 'Y' ? "text-danger" : "text-muted")">@row.IsWarning</td>
                            <td class="text-center @(row.IsNew == 'Y' ? "text-primary" : "text-muted")">@row.IsNew</td>
                            <td class="text-center @(row.IsDelisted == 'Y' ? "text-warning" : "text-muted")">@row.IsDelisted</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">삭제</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" />
    <input type="hidden" name="tab" value="@Model.Query.Tab" />
</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" />
    <input type="hidden" name="categoryID" value="@Model.Query.CategoryID" />
    <input type="hidden" name="keyword" value="@Model.Query.Keyword" />
</form>

<!-- 동기화 폼 -->
<form id="fSyncForm" method="post" accept-charset="utf-8" asp-page-handler="Sync">
    @Html.AntiForgeryToken()
</form>

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

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