<!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 도구를 사용할 운영 사용자를 관리합니다. 게임 서비스 이용자나 Oracle DB 계정과는 별개의 백오피스 관리 대상입니다.</p>
    </details>
  </div>

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

  <section class="content-band">
    <h2>사용자 추가</h2>
    <form method="post" action="/users" class="form-grid">
      <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
      <label>
        사용자명
        <input class="form-control" name="username" required>
      </label>
      <label>
        사번
        <input class="form-control" name="empNo" required>
      </label>
      <label>
        부서코드
        <input class="form-control" name="deptCode" required>
      </label>
      <button class="btn 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>
          <th></th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="user : ${users}">
          <td th:text="${user.userId()}">1</td>
          <td th:text="${user.username()}">이에이치알</td>
          <td th:text="${user.empNo()}">E10234</td>
          <td th:text="${user.deptCode()}">HR</td>
          <td><span class="badge" th:classappend="${user.active()} ? ' text-bg-success' : ' text-bg-secondary'" th:text="${user.activeYn()}">Y</span></td>
          <td>
            <form method="post" action="/users/active" class="inline-form">
              <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
              <input type="hidden" name="userId" th:value="${user.userId()}">
              <input type="hidden" name="active" th:value="${!user.active()}">
              <button class="btn btn-sm btn-outline-secondary" type="submit" th:text="${user.active()} ? '비활성화' : '활성화'">변경</button>
            </form>
          </td>
        </tr>
        <tr th:if="${#lists.isEmpty(users)}">
          <td colspan="6" class="text-muted">등록된 사용자가 없습니다.</td>
        </tr>
        </tbody>
      </table>
    </div>
  </section>

  <section class="content-band user-role-master">
    <h2>사용자 역할 부여</h2>
    <div class="master-detail">
      <form method="post" action="/users/roles" class="form-grid">
        <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
        <label>
          사용자
          <select class="form-select" id="userRoleMaster" name="userId" required>
            <option th:each="user : ${users}" th:value="${user.userId()}" th:text="${user.username()}"></option>
          </select>
        </label>
        <label>
          부여할 역할
          <select class="form-select" name="roleId" required>
            <option th:each="role : ${roles}" th:value="${role.roleId()}" th:text="${role.roleName()}"></option>
          </select>
        </label>
        <button class="btn rw-btn-primary" type="submit">부여</button>
      </form>
    </div>

    <h2 class="mt-4">선택 사용자 역할</h2>
    <div class="table-responsive">
      <table class="table table-sm align-middle" id="userRoleDetail">
        <thead>
        <tr>
          <th>사용자</th>
          <th>역할</th>
          <th></th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="mapping : ${userRoles}" th:attr="data-user-id=${mapping.userId()}">
          <td th:text="${mapping.username()}">이에이치알</td>
          <td th:text="${mapping.roleName()}">HR_DEPT_ROLE</td>
          <td>
            <form method="post" action="/users/roles/delete" class="inline-form">
              <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
              <input type="hidden" name="userId" th:value="${mapping.userId()}">
              <input type="hidden" name="roleId" th:value="${mapping.roleId()}">
              <button class="btn btn-sm btn-outline-danger" type="submit">해제</button>
            </form>
          </td>
        </tr>
        <tr class="empty-user-role-row" th:if="${#lists.isEmpty(userRoles)}">
          <td colspan="3" class="text-muted">부여된 역할이 없습니다.</td>
        </tr>
        </tbody>
      </table>
    </div>
  </section>
</main>
</body>
</html>
