@page "{id:int}"
@model Admin.Pages.Forum.Posts.List.EditModel
@{
    ViewData["Title"] = "게시글 수정";
}

<partial name="_Editor" />

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

    <partial name="_StatusMessage" />

    <form method="post" accept-charset="utf-8" autocomplete="off" enctype="multipart/form-data">
        <input type="hidden" asp-for="Input.ID" />
        <input type="hidden" asp-for="Input.BoardID" />
        <input type="hidden" asp-for="Input.ReturnUrl" />

        <!-- 말머리 -->
        <div class="row mb-2" id="prefixRow">
            <label class="col-sm-2 col-form-label">말머리</label>
            <div class="col-sm-10">
                <select asp-for="Input.BoardPrefixID" class="form-select w-auto" id="boardPrefixSelect">
                    <option value="">- 선택 -</option>
                </select>
            </div>
        </div>

        <!-- 제목 -->
        <div class="row mb-2">
            <label class="col-sm-2 col-form-label"><span class="text-danger">*</span> 제목</label>
            <div class="col-sm-10">
                <input type="text" asp-for="Input.Subject" class="form-control" maxlength="255" required />
            </div>
        </div>

        <!-- 내용 (CKEditor) -->
        <div class="row mb-2">
            <label class="col-sm-2 col-form-label">내용</label>
            <div class="col-sm-10">
                <textarea asp-for="Input.Content" class="ck-editor" id="Input_Content" rows="12"></textarea>
            </div>
        </div>

        <!-- 대표 이미지 -->
        <div class="row mb-2">
            <label class="col-sm-2 col-form-label">대표 이미지</label>
            <div class="col-sm-10">
                <div id="thumbPrev" @(string.IsNullOrWhiteSpace(Model.CurrentThumbnail) ? "hidden" : "")>
                    <img class="img-fluid img-thumbnail" alt="대표 이미지 미리보기" src="@(Model.CurrentThumbnail ?? "")" style="max-width: 300px;" />
                </div>
                <input type="file" id="Input_ThumbnailFile" asp-for="Input.ThumbnailFile" class="form-control" accept=".jpg,.jpeg,.png,.gif,.webp,.bmp" />
                <span class="form-text text-muted">
                    지원 확장자: <code>.jpg</code>, <code>.jpeg</code>, <code>.png</code>, <code>.gif</code>, <code>.webp</code>, <code>.bmp</code>
                </span>
            </div>
        </div>

        <!-- 기존 첨부파일 -->
        <div class="row mb-2">
            <label class="col-sm-2 col-form-label">기존 첨부파일</label>
            <div class="col-sm-10">
                @if (Model.ExistingFiles.Count > 0)
                {
                    <ul class="list-unstyled mb-1">
                        @foreach (var file in Model.ExistingFiles)
                        {
                            <li>
                                <a href="@file.Url" target="_blank">@file.FileName</a>
                                @if (file.Size.HasValue)
                                {
                                    <small class="text-muted">(@(file.Size.Value > 1048576 ? $"{file.Size.Value / 1048576.0:F1}MB" : $"{file.Size.Value / 1024.0:F1}KB"))</small>
                                }
                                <small class="text-muted">다운로드: @file.Downloads</small>
                            </li>
                        }
                    </ul>
                }
                else
                {
                    <span class="text-muted">없음</span>
                }
            </div>
        </div>

        <!-- 새 첨부파일 -->
        <div class="row mb-2">
            <label class="col-sm-2 col-form-label">첨부파일 추가</label>
            <div class="col-sm-10">
                <input type="file" asp-for="Input.Files" class="form-control" multiple
                       accept=".jpg,.jpeg,.png,.gif,.webp,.bmp,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.zip,.rar,.7z,.hwp,.hwpx,.csv" />
                <span class="form-text text-muted">여러 파일을 선택할 수 있습니다.</span>
            </div>
        </div>

        <!-- 태그 -->
        <div class="row mb-2">
            <label class="col-sm-2 col-form-label">태그</label>
            <div class="col-sm-10">
                <input type="text" asp-for="Input.TagsInput" class="form-control"
                       placeholder="태그를 쉼표(,)로 구분하여 입력하세요" />
                <span class="form-text text-muted">예: 비트코인, 이더리움, 블록체인</span>
            </div>
        </div>

        <!-- 상태 -->
        <div class="row mb-2">
            <label class="col-sm-2 col-form-label">상태</label>
            <div class="col-sm-10 align-content-center">
                <div class="form-check form-check-inline">
                    <input type="checkbox" class="form-check-input" asp-for="Input.IsNotice" />
                    <label class="form-check-label" for="Input_IsNotice">공지</label>
                </div>

                <div class="form-check form-check-inline">
                    <input type="checkbox" class="form-check-input" asp-for="Input.IsSecret" />
                    <label class="form-check-label" for="Input_IsSecret">비밀</label>
                </div>

                <div class="form-check form-check-inline">
                    <input type="checkbox" class="form-check-input" asp-for="Input.IsAnonymous" />
                    <label class="form-check-label" for="Input_IsAnonymous">익명</label>
                </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="@(Model.ReturnUrl ?? "/Forum/Posts/List")" class="btn btn-secondary">취소</a>
            <button type="submit"
                    class="btn btn-danger"
                    formaction="?handler=Delete"
                    formnovalidate
                    onclick="return confirm('삭제 하시겠습니까? 삭제된 게시물은 복구가 불가능합니다.');">
                삭제
            </button>
        </div>

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

@section Scripts {
    <script>
        setupImagePreview("Input_ThumbnailFile", "thumbPrev");

        // 페이지 로드 시 현재 게시판의 말머리 로드
        (async function () {
            const boardIDInput = document.querySelector("input[name='Input.BoardID']");
            const boardID = boardIDInput ? boardIDInput.value : "";
            const currentPrefixID = "@(Model.Input.BoardPrefixID?.ToString() ?? "")";
            const postID = "@Model.Input.ID";

            if (boardID) {
                try {
                    const res = await fetch(`/Forum/Posts/List/Edit/${postID}?handler=Prefixes&boardID=${boardID}`);
                    const items = await res.json();
                    const prefixSelect = document.getElementById("boardPrefixSelect");

                    if (items.length > 0) {
                        items.forEach(item => {
                            const opt = document.createElement("option");
                            opt.value = item.id;
                            opt.textContent = item.name;
                            if (item.id.toString() === currentPrefixID) {
                                opt.selected = true;
                            }
                            prefixSelect.appendChild(opt);
                        });
                        document.getElementById("prefixRow").hidden = false;
                    } else {
                        document.getElementById("prefixRow").hidden = true;
                    }
                } catch {
                    document.getElementById("prefixRow").hidden = true;
                }
            } else {
                document.getElementById("prefixRow").hidden = true;
            }
        })();
    </script>
}
