<!doctype html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/layout :: head('역할')}"></head>
<body>
<nav th:replace="~{fragments/layout :: nav}"></nav>
<main class="container py-4">
  <div class="page-title">
    <h1>역할</h1>
    <details class="explanation-details">
      <summary>도움말</summary>
      <p>스마일게이트 Data & AI PoC 운영 역할을 관리합니다. 역할은 사용자에게 직접 부여하거나 그룹을 통해 부여할 수 있습니다.</p>
    </details>
  </div>

  <div class="alert alert-success" th:if="${message}" th:text="${message}"></div>
  <div class="alert alert-danger" th:if="${error}" th:text="${error}"></div>

  <section class="content-band">
    <h2>역할 추가</h2>
    <form method="post" action="/roles" class="form-grid">
      <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
      <label>
        역할명
        <input class="form-control" name="roleName" placeholder="HR_DEPT_ROLE" required>
      </label>
      <label>
        설명
        <input class="form-control" name="description" maxlength="200">
      </label>
      <label>
        데이터 취급 등급
        <select class="form-select" name="maxSensitivityLevel">
          <option value="PUBLIC">PUBLIC</option>
          <option value="INTERNAL">INTERNAL</option>
          <option value="CONFIDENTIAL">CONFIDENTIAL</option>
          <option value="RESTRICTED">RESTRICTED</option>
        </select>
        <span class="form-hint">PoC 운영 시 역할별 데이터 취급 기준을 표현하는 분류값입니다.</span>
      </label>
      <button class="btn rw-btn-primary" type="submit">추가</button>
    </form>
  </section>

  <section class="content-band">
    <h2>역할 목록</h2>
    <div class="table-responsive">
      <table class="table table-sm align-middle">
        <thead>
        <tr>
          <th>ID</th>
          <th>역할명</th>
          <th>데이터 취급 등급</th>
          <th>삭제 영향</th>
          <th></th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="role : ${roles}"
            th:with="impact=${roleImpactByRoleId[role.roleId()]},
                     hasDependencies=${impact != null && (!#lists.isEmpty(impact.directUsers()) || !#lists.isEmpty(impact.groups()) || impact.permissionCount() > 0)}">
          <td th:text="${role.roleId()}">10</td>
          <td th:text="${role.roleName()}">HR_DEPT_ROLE</td>
          <td>
            <span class="badge text-bg-light" th:title="${'역할 데이터 취급 등급: ' + role.maxSensitivityLevel()}"
                  th:text="${role.maxSensitivityLevel()}">PUBLIC</span>
            <form method="post" action="/roles/max-sensitivity" class="inline-form">
              <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
              <input type="hidden" name="roleId" th:value="${role.roleId()}">
              <select class="form-select form-select-sm" name="maxSensitivityLevel">
                <option value="PUBLIC" th:selected="${role.maxSensitivityLevel() == 'PUBLIC'}">PUBLIC</option>
                <option value="INTERNAL" th:selected="${role.maxSensitivityLevel() == 'INTERNAL'}">INTERNAL</option>
                <option value="CONFIDENTIAL" th:selected="${role.maxSensitivityLevel() == 'CONFIDENTIAL'}">CONFIDENTIAL</option>
                <option value="RESTRICTED" th:selected="${role.maxSensitivityLevel() == 'RESTRICTED'}">RESTRICTED</option>
              </select>
              <button class="btn btn-sm btn-outline-primary" type="submit">저장</button>
            </form>
            <small class="text-muted d-block mt-1">PoC 운영 기준 분류값</small>
          </td>
          <td class="delete-impact">
            <div>
              <span>직접 사용자</span>
              <strong th:text="${impact == null || #lists.isEmpty(impact.directUsers()) ? '없음' : #strings.listJoin(impact.directUsers(), ', ')}">없음</strong>
            </div>
            <div>
              <span>연결 그룹</span>
              <strong th:text="${impact == null || #lists.isEmpty(impact.groups()) ? '없음' : #strings.listJoin(impact.groups(), ', ')}">없음</strong>
            </div>
            <div>
              <span>영향 사용자</span>
              <strong th:text="${impact == null || #lists.isEmpty(impact.affectedUsers()) ? '없음' : #strings.listJoin(impact.affectedUsers(), ', ')}">없음</strong>
            </div>
            <div>
              <span>권한/객체</span>
              <strong th:text="${impact == null ? '0건' : impact.permissionCount() + '건'}">0건</strong>
              <small th:text="${impact == null || #lists.isEmpty(impact.objectNames()) ? '보호 객체 없음' : #strings.listJoin(impact.objectNames(), ', ')}">보호 객체 없음</small>
            </div>
            <p class="text-danger small mb-0" th:if="${hasDependencies}">연결을 먼저 해제해야 역할을 삭제할 수 있습니다.</p>
          </td>
          <td>
            <form method="post" action="/roles/delete" class="inline-form">
              <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
              <input type="hidden" name="roleId" th:value="${role.roleId()}">
              <label class="delete-confirm" th:classappend="${hasDependencies} ? ' disabled'">
                <input type="checkbox" name="confirmImpact" value="true" required th:disabled="${hasDependencies}">
                영향 확인
              </label>
              <button class="btn btn-sm btn-outline-danger" type="submit"
                      th:disabled="${hasDependencies}"
                      th:text="${hasDependencies} ? '연결 해제 필요' : '삭제'">삭제</button>
            </form>
          </td>
        </tr>
        <tr th:if="${#lists.isEmpty(roles)}">
          <td colspan="5" class="text-muted">등록된 역할이 없습니다.</td>
        </tr>
        </tbody>
      </table>
    </div>
  </section>
</main>
</body>
</html>
