@model IEnumerable<iyixiGames.Domain.DTOs.Games.GameResponse>

@{
    ViewData["Title"] = "小游戏管理";
}

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h1>小游戏列表</h1>
        <div>
            <a asp-action="Create" class="btn btn-primary me-2">创建新游戏</a>
            <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#batchCreateModal">
                批量创建
            </button>
        </div>
    </div>

    @if (TempData["SuccessMessage"] != null)
    {
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            @TempData["SuccessMessage"]
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    }
    @if (TempData["ErrorMessage"] != null)
    {
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            @TempData["ErrorMessage"]
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    }

    <table class="table table-striped table-hover">
        <thead class="table-dark">
            <tr>
                <th>@Html.DisplayNameFor(model => model.Name)</th>
                <th>@Html.DisplayNameFor(model => model.Path)</th>
                <th>@Html.DisplayNameFor(model => model.Description)</th> @* New: Description column *@
                <th>@Html.DisplayNameFor(model => model.Category)</th>
                <th>@Html.DisplayNameFor(model => model.Popularity)</th>
                <th>@Html.DisplayNameFor(model => model.AvgRating)</th>
                <th>@Html.DisplayNameFor(model => model.ReleaseDate)</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @if (Model != null && Model.Any())
            {
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@Html.DisplayFor(modelItem => item.Name)</td>
                        <td>@Html.DisplayFor(modelItem => item.Path)</td>
                        <td>@Html.DisplayFor(modelItem => item.Description)</td> @* Display Description *@
                        <td>@(string.Join(", ", item.Category))</td>
                        <td>@Html.DisplayFor(modelItem => item.Popularity)</td>
                        <td>@Html.DisplayFor(modelItem => item.AvgRating)</td>
                        <td>@Html.DisplayFor(modelItem => item.ReleaseDate.ToShortDateString())</td>
                        <td>
                            <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-sm btn-info me-1">编辑</a>
                            <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-sm btn-secondary me-1">详情</a>
                            <a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-sm btn-danger">删除</a>
                        </td>
                    </tr>
                }
            }
            else
            {
                <tr>
                    <td colspan="8" class="text-center text-muted">暂无游戏数据。</td> @* Update colspan count *@
                </tr>
            }
        </tbody>
    </table>
</div>

<div class="modal fade" id="batchCreateModal" tabindex="-1" aria-labelledby="batchCreateModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="batchCreateModalLabel">批量创建小游戏</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form asp-action="BatchCreate" method="post">
                <div class="modal-body">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <p>请输入小游戏的文件夹名称，每行一个。系统将以这些名称创建默认游戏条目。</p>
                    <div class="mb-3">
                        <label for="gamePathsText" class="form-label">游戏文件夹名称列表：</label>
                        <textarea class="form-control" id="gamePathsText" name="GamePathsText" rows="10" 
                                  placeholder="例如：&#10;space-shooter-v1&#10;puzzle-mania&#10;retro-platformer"></textarea>
                        <span id="gamePathsTextValidation" class="text-danger"></span>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">开始批量创建</button>
                </div>
            </form>
        </div>
    </div>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

    <script>
        // Batch Create Modal logic (remains the same)
        var batchCreateModal = document.getElementById('batchCreateModal');
        if (batchCreateModal) {
            batchCreateModal.addEventListener('hidden.bs.modal', function (event) {
                var textarea = batchCreateModal.querySelector('#gamePathsText');
                if (textarea) {
                    textarea.value = '';
                }
                var validationSpan = document.getElementById('gamePathsTextValidation');
                if (validationSpan) {
                    validationSpan.textContent = '';
                }
                var form = batchCreateModal.querySelector('form');
                if (form && typeof $.validator !== 'undefined' && form.dataset.validatorInitialized === 'true') {
                    $(form).validate().resetForm();
                    $(form).find('.is-invalid').removeClass('is-invalid');
                }
            });

            @if (TempData["ErrorMessage"] != null && TempData["ShowBatchModal"] != null && (bool)TempData["ShowBatchModal"])
            {
                <text>
                    var modal = new bootstrap.Modal(document.getElementById('batchCreateModal'));
                    modal.show();
                    var validationSpan = document.getElementById('gamePathsTextValidation');
                    if (validationSpan) {
                        validationSpan.textContent = "@TempData["ErrorMessage"]";
                    }
                </text>
            }
        }
    </script>
}