@using bitforum.Helpers;
@{
    ViewData["Title"] = "배너 관리";

    var bannerPositions = ViewBag.BannerPositions as List<bitforum.Models.Page.Banner.BannerPosition>;
    var bannerItems = ViewBag.BannerItems as List<bitforum.Models.Page.Banner.BannerItem>;
    var total = (ViewBag.Total ?? 0).ToString("N0");
    var pagination = ViewBag.Pagination as bitforum.Models.Pagination;
}

<partial name="~/Views/Page/Banner/_Navbar.cshtml" />

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

    <partial name="_StatusMessage" />

    <div class="row g-2 mb-2">
        <div class="col">
            <select name="position_id" id="positionID" class="form-select w-auto">
                <option value="">선택하세요.</option>
                @foreach (var row in bannerPositions)
                {
                    <option value="@row.ID" selected="@(row.ID == ViewBag.PositionID ? "selected" : null)">@row.Subject (@row.BannerItem.Count)</option>
                }
            </select>
        </div>
    </div>

    <div class="row g-2 align-items-end">
        <div class="col">
            Total : @total
        </div>
        <div class="col text-end">
            <button type="button" id="btnListDelete" class="btn btn-sm btn-danger" form="fAdminList" data-action="/Page/Banner/Item/Delete">삭제</button>
            <a href="/Page/Banner/Item/Write" class="btn btn-sm btn-success">추가</a>
        </div>
    </div>

    <form name="f_admin_list" id="fAdminList" method="post" accept-charset="utf-8" autocomplete="off"></form>

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-3">
            <colgroup>
                <col width="2%" />
                <col width="5%" />
                <col width="*" />
                <col width="30%" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
                <col width="*" />
            </colgroup>
            <thead>
                <tr>
                    <th><input type="checkbox" id="checkedAll" class="form-check-input" value="1" form="fAdminList" /></th>
                    <th>ID</th>
                    <th>이미지</th>
                    <th>제목</th>
                    <th>순서</th>
                    <th>사용</th>
                    <th>조회 수</th>
                    <th>등록일시</th>
                    <th>수정일시</th>
                    <th>비고</th>
                </tr>
            </thead>
            <tbody>
                @if (bannerItems == null || !bannerItems.Any())
                {
                    <tr>
                        <td colspan="10">No Data.</td>
                    </tr>
                }
                else
                {
                    @foreach (var row in bannerItems)
                    {
                        <tr>
                            <td>
                                <input type="checkbox" name="ids[]" class="form-check-input list-check-box" value="@row.ID" form="fAdminList" />
                            </td>
                            <td>@row.ID</td>
                            <td>
                                @if (row.Image is not null && row.Image != string.Empty)
                                {
                                    <img src="@Url.Content(row.Image)" class="img-fluid img-thumbnail" alt="@row.Subject" />
                                }
                                else
                                {
                                    <text>-</text>
                                }
                            </td>
                            <td>
                                <span class="badge text-bg-secondary">[@row.BannerPosition.Subject]</span>
                                <strong>@row.Subject</strong><br/>
                                <small>
                                    사용 기한：
                                    @if (row.StartAt != null && row.EndAt != null)
                                    {
                                        <text>@row.StartAt.GetDateAt() ~ @row.EndAt.GetDateAt()</text>
                                    } @if (row.StartAt != null && row.EndAt is null)
                                    {
                                        <text>@row.StartAt.GetDateAt() ~</text>
                                    } @if (row.StartAt is null && row.EndAt != null)
                                    {
                                        <text>~ @row.EndAt.GetDateAt()</text>
                                    }
                                    else
                                    {
                                        <text>[무기한]</text>
                                    }
                                </small><br/>
                                <small>크기：@row.Width x @row.Height</small>
                            </td>
                            <td>@row.Order</td>
                            <td>@(row.IsActive ? "Y" : "N")</td>
                            <td>@row.Views</td>
                            <td>@row.CreatedAt.GetDateAt()</td>
                            <td>@row.UpdatedAt.GetDateAt()</td>
                            <td>
                                <div class="d-grid gap-2 d-md-block">
                                    <a href="/Page/Banner/Item/@row.ID/Edit" class="btn btn-sm btn-outline-info">수정</a>
                                    <a href="/Page/Banner/Item/@row.ID/Delete" class="btn btn-sm btn-outline-danger btn-row-delete">삭제</a>
                                </div>
                            </td>
                        </tr>
                    }
                }
            </tbody>
        </table>

        <partial name="_Pagination" model="pagination" />
    </div>
</div>

<script type="module">
    $(document).on("change", "#positionID", function () {
        location.href = ("/Page/Banner/Item/" + $(this).val());
    });
</script>