@page "{id:int}"
@model Admin.Pages.Crypto.List.EditModel
@{
    ViewData["Title"] = "코인 수정";
}

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

    <partial name="_StatusMessage" />

    <ul class="nav nav-tabs mb-3">
        <li class="nav-item">
            <a class="nav-link active" href="/Crypto/List/Edit/@Model.CoinID">기본 정보</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="/Crypto/Board/Index?coinID=@Model.CoinID">게시판 연결</a>
        </li>
    </ul>

    <form id="fAdminWrite" method="post" enctype="multipart/form-data" accept-charset="utf-8" autocomplete="off" class="mt-3">
        @Html.AntiForgeryToken()

        <div class="row mb-2">
            <label asp-for="Input.Symbol" class="col-sm-2 col-form-label">
                <span class="text-danger">*</span> 심볼
            </label>
            <div class="col-sm-10">
                <input asp-for="Input.Symbol" class="form-control text-uppercase" maxlength="30" required placeholder="BTC" />
                <div class="form-text">대문자로 자동 변환됩니다 (예: BTC, ETH)</div>
                <span asp-validation-for="Input.Symbol" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label class="col-sm-2 col-form-label">거래쌍</label>
            <div class="col-sm-10 align-self-center">
                @if (Model.Markets.Count > 0)
                {
                    <span>@string.Join(", ", Model.Markets)</span>
                }
                else
                {
                    <span class="text-muted">Upbit 동기화 후 자동 등록됩니다</span>
                }
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.KorName" class="col-sm-2 col-form-label">
                <span class="text-danger">*</span> 한글명
            </label>
            <div class="col">
                <input asp-for="Input.KorName" class="form-control" maxlength="200" required placeholder="비트코인" />
                <span asp-validation-for="Input.KorName" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.EngName" class="col-sm-2 col-form-label">
                <span class="text-danger">*</span> 영문명
            </label>
            <div class="col">
                <input asp-for="Input.EngName" class="form-control" maxlength="200" required placeholder="Bitcoin" />
                <span asp-validation-for="Input.EngName" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.LogoImageFile" class="col-sm-2 col-form-label">로고 이미지</label>
            <div class="col-sm-10">
                @if (!string.IsNullOrEmpty(Model.CurrentLogoImage))
                {
                    <div class="mb-2">
                        <img src="@Model.CurrentLogoImage" alt="현재 로고" style="max-width:128px;max-height:128px;object-fit:contain;border:1px solid #ddd;border-radius:4px;" />
                        <div class="form-check mt-1">
                            <input asp-for="Input.DeleteLogoImage" class="form-check-input" />
                            <label asp-for="Input.DeleteLogoImage" class="form-check-label text-danger">현재 이미지 삭제</label>
                        </div>
                    </div>
                }
                <div id="LogoImagePrev" hidden>
                    <img class="img-fluid img-thumbnail" alt="로고 이미지 미리보기" style="max-width:128px;max-height:128px;" /><br />
                    <button type="button" class="btn btn-sm btn-danger mt-2 mb-2 btn-remove-preview">삭제</button>
                </div>
                <input asp-for="Input.LogoImageFile" class="form-control" accept="image/*,.svg" />
                <div class="form-text">jpg, jpeg, png, gif, webp, svg</div>
                <span asp-validation-for="Input.LogoImageFile" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.CategoryIDs" class="col-sm-2 col-form-label">카테고리</label>
            <div class="col-sm-10 align-self-center">
                @if (Model.Categories.Count > 0)
                {
                    <div class="border rounded p-2" style="max-height: 150px; overflow-y: auto;">
                        @foreach (var cat in Model.Categories)
                        {
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="Input.CategoryIDs" id="cat_@(cat.Value)" value="@cat.Value" @(Model.Input.CategoryIDs.Contains(int.Parse(cat.Value)) ? "checked" : "") />
                                <label class="form-check-label" for="cat_@(cat.Value)">@cat.Text</label>
                            </div>
                        }
                    </div>
                }
                else
                {
                    <span>-</span>
                }
                <span asp-validation-for="Input.CategoryIDs" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.Description" class="col-sm-2 col-form-label">설명</label>
            <div class="col-sm-10">
                <textarea asp-for="Input.Description" class="form-control" rows="4" maxlength="5000" placeholder="코인에 대한 설명을 입력하세요."></textarea>
                <span asp-validation-for="Input.Description" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.ContractAddress" class="col-sm-2 col-form-label">컨트랙트 주소</label>
            <div class="col-md-10">
                <input asp-for="Input.ContractAddress" class="form-control" maxlength="100" placeholder="0x..." />
                <span asp-validation-for="Input.ContractAddress" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.WebsiteUrl" class="col-sm-2 col-form-label">Site URL</label>
            <div class="col-sm-10">
                <input asp-for="Input.WebsiteUrl" class="form-control" maxlength="500" placeholder="https://..." />
                <span asp-validation-for="Input.WebsiteUrl" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.WhitepaperUrl" class="col-sm-2 col-form-label">Whitepaper URL</label>
            <div class="col-sm-10">
                <input asp-for="Input.WhitepaperUrl" class="form-control" maxlength="500" placeholder="https://..." />
                <span asp-validation-for="Input.WhitepaperUrl" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-2">
            <label asp-for="Input.TwitterUrl" class="col-sm-2 col-form-label">Twitter URL</label>
            <div class="col-sm-10">
                <input asp-for="Input.TwitterUrl" class="form-control" maxlength="500" placeholder="https://twitter.com/..." />
                <span asp-validation-for="Input.TwitterUrl" class="text-danger"></span>
            </div>
        </div>

        <div class="row mb-3">
            <label asp-for="Input.TelegramUrl" class="col-sm-2 col-form-label">Telegram URL</label>
            <div class="col-sm-10">
                <input asp-for="Input.TelegramUrl" class="form-control" maxlength="500" placeholder="https://t.me/..." />
                <span asp-validation-for="Input.TelegramUrl" class="text-danger"></span>
            </div>
        </div>

        <div class="row">
            <label class="col-sm-2">상태</label>
            <div class="col-sm-10">
                <div class="d-flex gap-3 flex-wrap">
                    <div class="form-check">
                        <input asp-for="Input.IsActive" class="form-check-input" />
                        <label asp-for="Input.IsActive" class="form-check-label">활성화</label>
                    </div>
                    <div class="form-check">
                        <input asp-for="Input.IsNew" class="form-check-input" />
                        <label asp-for="Input.IsNew" class="form-check-label text-primary">신규 상장</label>
                    </div>
                    <div class="form-check">
                        <input asp-for="Input.IsWarning" class="form-check-input" />
                        <label asp-for="Input.IsWarning" class="form-check-label text-danger">위험 경고</label>
                    </div>
                    <div class="form-check">
                        <input asp-for="Input.IsDelisted" class="form-check-input" />
                        <label asp-for="Input.IsDelisted" class="form-check-label text-warning">상장 폐지</label>
                    </div>
                </div>
            </div>
        </div>

        <hr />

        <div class="d-grid gap-2 text-center d-md-block">
            <button type="submit" class="btn btn-success">저장</button>
            <a href="/Crypto/List/Index" class="btn btn-secondary">취소</a>
        </div>

        <br/>
    </form>
</div>

@section Scripts {
    <script>
        setupImagePreview("Input_LogoImageFile", "LogoImagePrev");
    </script>
}