@page
@model Admin.Pages.Director.Role.IndexModel
@{
    ViewData["Title"] = "역할 관리";
}

<div class="container">
    <div class="row">
        <div class="col">
            <h3>@ViewData["Title"]</h3>
        </div>
        <div class="col text-end align-self-center">
            <a asp-page="/Director/User/Index" class="btn btn-secondary">취소</a>
        </div>
    </div>
    <hr />

    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <partial name="_StatusMessage" />

    <form name="f_admin_write" id="fAdminWrite" method="post" accept-charset="utf-8" autocomplete="off">
        <div class="input-group mb-3">
            <label class="input-group-text" for="roleName">Role:</label>
            <input asp-for="RoleName" class="form-control" placeholder="역할(Role)을 입력해주세요." required maxlength="100"/>
            <button type="submit" class="btn btn-sm btn-success">역할 추가</button>
        </div>
    </form>

    <!-- 삭제를 위한 -->
    <form id="deleteRoleForm" method="post" accept-charset="utf-8" asp-page-handler="Delete">
        @Html.AntiForgeryToken()
        <input type="hidden" id="deleteRoleID" name="id" />
    </form>

    <div class="table-responsive">
        <table class="table table-bordered table-striped table-hover mt-4">
            <thead>
                <tr class="text-center">
                    <th>ID</th>
                    <th>Role Name</th>
                    <th>Permission</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @if (Model.List == null || !Model.List.Any())
                {
                    <tr>
                        <td colspan="4">No Data.</td>
                    </tr>
                }
                else
                {
                    @foreach (var role in Model.List)
                    {
                        <tr>
                            <td>@role.ID</td>
                            <td>@role.Name</td>
                            <td>@role.ClaimsCount</td>
                            <td>
                                <button type="button" class="btn btn-sm btn-danger btn-row-delete" data-id="@role.ID" data-name="@role.Name">삭제</button>
                                <a class="btn btn-sm btn-primary" asp-page="/Director/Role/Permission" asp-route-id="@role.ID">권한 관리</a>
                            </td>
                        </tr>
                    }
                }
            </tbody>
        </table>
    </div>
</div>

@section Scripts {
    <script>
        document.addEventListener("click", function(e) {
            const btn = e.target.closest('.btn-row-delete');
            if (!btn) {
                return;
            }

            const id = btn.dataset.id;
            const name = btn.dataset.name;

            const label = name ? `역할 '${name}'` : '해당 역할';
            if (!confirm(`${label}을(를) 삭제하시겠습니까?`)) {
                return;
            }

            document.getElementById('deleteRoleID').value = id;
            document.getElementById('deleteRoleForm').submit();
        });
    </script>
}