<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/layout}">
<th:block layout:fragment="css">
</th:block>
<th:block layout:fragment="script">
  <script type="text/javascript" th:src="@{/js/admin/userMgt.js}"></script>
</th:block>
<div layout:fragment="content">
  <main class="pt-3">
    <input type="hidden" name="_csrf_header" th:value="${_csrf.headerName}"/>
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
    <h4>사용자 관리</h4>
    <div class="row mx-0">
      <div class="col-12 card text-center">
        <div class="card-body">
          <div class="row justify-content-start">
            <div class="col-7">
              <form method="get" th:action="@{/admin/userMgt}">
                <input type="hidden" name="pageIndex" id="pageIndex" th:value="${searchParams.pageIndex}">
                <div class="row justify-content-between">
                  <div class="col-auto row">
                    <div class="col-auto">
                      <label for="rowCnt" class="col-form-label">데이터 수</label>
                    </div>
                    <div class="col-auto">
                      <select class="form-select" name="rowCnt" id="rowCnt">
                        <th:block th:each="num : ${#numbers.sequence(1,5)}">
                          <option th:value="${num*10}" th:text="${num*10}" th:selected="${searchParams.rowCnt==num*10}"></option>
                        </th:block>
                      </select>
                    </div>
                  </div>
                  <div class="col-auto card">
                    <div class="row justify-content-end pt-1">
                      <div class="col-auto">
                        <div class="row mb-1">
                          <label for="searchUserId" class="col-sm-2 col-form-label">아이디</label>
                          <div class="col-sm-4">
                            <input type="text" class="form-control form-control-sm" id="searchUserId" name="userId" th:value="${searchParams.userId}">
                          </div>
                          <label for="searchName" class="col-sm-2 col-form-label">이름</label>
                          <div class="col-sm-4">
                            <input type="text" class="form-control form-control-sm" id="searchName" name="name" th:value="${searchParams.name}">
                          </div>
                        </div>
                        <div class="row mb-1">
                          <label for="searchPosition" class="col-sm-2 col-form-label">직책</label>
                          <div class="col-sm-4">
                            <select class="form-select" id="searchPosition" name="position">
                              <option value="0" selected>선택해주세요</option>
                              <th:block th:each="commonCode:${session.positionList}">
                                <option th:value="${commonCode.codeSq}" th:text="${commonCode.value}" th:selected="${commonCode.codeSq == searchParams.position}"></option>
                              </th:block>
                            </select>
                          </div>
                          <label for="searchDepartment" class="col-sm-2 col-form-label">부서</label>
                          <div class="col-sm-4">
                            <select class="form-select" id="searchDepartment" name="department">
                              <option value="0" selected>선택해주세요</option>
                              <th:block th:each="commonCode:${session.departmentList}">
                                <option th:value="${commonCode.codeSq}" th:text="${commonCode.value}" th:selected="${commonCode.codeSq == searchParams.department}"></option>
                              </th:block>
                            </select>
                          </div>
                        </div>
                        <div class="row">
                          <label for="startDate" class="col-sm-2 col-form-label">생성일</label>
                          <div class="col-sm-6">
                            <div class="col-auto input-group w-auto input-daterange" id="dateSelectorDiv">
                              <input type="text" class="form-control form-control-sm" id="startDate" name="startDate" placeholder="시작일" autocomplete="off" th:value="${searchParams.startDate}" readonly>
                              <input type="text" class="form-control form-control-sm" id="endDate" name="endDate" placeholder="종료일" autocomplete="off" th:value="${searchParams.endDate}" readonly>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="col-auto my-auto">
                        <input type="submit" class="btn btn-primary py-4" id="searchBtn" value="검색">
                      </div>
                    </div>
                  </div>
                </div>
              </form>
              <div class="row-cols-6">
                <table class="table table-striped">
                  <thead>
                  <tr>
                    <th></th>
                    <th>아이디</th>
                    <th>이름</th>
                    <th>부서</th>
                    <th>직급</th>
                    <th>생성일</th>
                    <th>상태</th>
                  </tr>
                  </thead>
                  <tbody>
                  <tr class="userInfoTr" th:each="userInfo:${userInfoList}">
                    <td>
                      <input type="checkbox" class="userInfoCheckBox" th:value="${userInfo.userSeq}">
                    </td>
                    <td th:text="${userInfo.userId}"></td>
                    <td th:text="|${userInfo.name}${#strings.contains(userInfo.userRole,'ADMIN')?'(관리자)':''}|"></td>
                    <td th:text="${userInfo.positionName}"></td>
                    <td th:text="${userInfo.departmentName}"></td>
                    <td th:text="${#temporals.format(userInfo.createDate, 'yyyy-MM-dd')}"></td>
                    <td th:text="${#strings.contains(userInfo.userStatus,'T')?'활성화':'비활성화'}"></td>
                  </tr>
                  </tbody>
                </table>
              </div>
              <div class="row justify-content-between">
                <div class="col-auto"></div>
                <div class="col-auto">
                  <nav aria-label="Page navigation">
                    <ul class="pagination">
                      <th:block th:if="${searchParams.pageIndex>3}">
                        <li class="page-item" th:data-pageindex="${(searchParams.pageIndex)-3}">
                          <a class="page-link" href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                          </a>
                        </li>
                      </th:block>
                      <th:block th:each="num : ${#numbers.sequence(searchParams.startNum, searchParams.endNum)}">
                        <li class="page-item" th:data-pageindex="${num}" th:classappend="${searchParams.pageIndex==num?'active':''}">
                          <a class="page-link" href="#" th:text="${num}"></a>
                        </li>
                      </th:block>
                      <th:block th:if="${searchParams.maxNum>searchParams.endNum+2}">
                        <li class="page-item" th:data-pageindex="${(searchParams.pageIndex)+3}">
                          <a class="page-link" href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                          </a>
                        </li>
                      </th:block>
                    </ul>
                  </nav>
                </div>
                <div class="col-auto">
                  <input type="button" class="btn btn-success" value="사용자 생성" data-bs-toggle="modal" data-bs-target="#userInfoModal">
                </div>
              </div>
            </div>
            <div class="col-5">
              <ul class="nav nav-tabs" id="userTab" role="tablist">
                <li class="nav-item" role="presentation">
                  <button class="nav-link active" id="infoTab" data-bs-toggle="tab" type="button" role="tab">개인정보</button>
                </li>
                <li class="nav-item" role="presentation">
                  <button class="nav-link" id="categoryTab" data-bs-toggle="tab" type="button" role="tab">권한</button>
                </li>
              </ul>
              <div class="tab-content border border-top-0" id="userContent">
                <div class="py-5">
                  <h3>왼쪽 목록에서 선택해주세요.</h3>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </main>

  <div class="modal fade" id="userInfoModal" tabindex="-1" aria-labelledby="userInfoModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="userInfoModalLabel">사용자 생성</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div  class="modal-body">
          <form id="userInfoInsert" action="#" th:action="@{/admin/insertUserInfo}" method="post">
            <div class="mb-3 row">
              <label for="userId" class="col-sm-4 col-form-label">아이디</label>
              <div class="col-sm-6">
                <input type="text" class="form-control" id="userId" name="userId" autocomplete="off">
              </div>
              <div class="col-sm-auto form-text mx-auto">
                6~20자 사이의 알파벳, 숫자를 입력해주세요.
              </div>
            </div>
            <div class="mb-3 row">
              <label for="password" class="col-sm-4 col-form-label">비밀번호</label>
              <div class="col-sm-6">
                <input type="password" class="form-control" id="password" name="password">
              </div>
              <div class="col-sm-auto form-text mx-auto">
                8~16자 사이의 알파벳, 숫자 조합을 입력해주세요.
              </div>
            </div>
            <div class="mb-3 row">
              <label for="passwordConfirm" class="col-sm-4 col-form-label">비밀번호 확인</label>
              <div class="col-sm-6">
                <input type="password" class="form-control" id="passwordConfirm">
              </div>
            </div>
            <div class="mb-3 row">
              <label for="name" class="col-sm-4 col-form-label">이름</label>
              <div class="col-sm-6">
                <input type="text" class="form-control" id="name" name="name" autocomplete="off">
              </div>
            </div>
            <div class="mb-3 row">
              <label for="department" class="col-sm-4 col-form-label">부서</label>
              <div class="col-sm-6">
                <select class="form-select" id="department" name="department">
                  <th:block th:each="commonCode:${session.departmentList}">
                    <option th:value="${commonCode.codeSq}" th:text="${commonCode.value}"></option>
                  </th:block>
                </select>
              </div>
            </div>
            <div class="mb-3 row">
              <label for="position" class="col-sm-4 col-form-label">직급</label>
              <div class="col-sm-6">
                <select class="form-select" id="position" name="position">
                  <th:block th:each="commonCode:${session.positionList}">
                    <option th:value="${commonCode.codeSq}" th:text="${commonCode.value}"></option>
                  </th:block>
                </select>
              </div>
            </div>
            <div class="mb-3 row">
              <div class="col-sm-4"></div>
              <div class="col-sm-auto form-check ms-3">
                <input type="hidden" name="userRole" value="ROLE_USER">
                <input class="form-check-input" type="checkbox" value="ROLE_ADMIN" id="roleAdmin" name="userRole">
                <label class="form-check-label" for="roleAdmin">관리자 계정</label>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal" id="closeModalBtn">닫기</button>
          <button type="button" class="btn btn-primary" id="saveBtn">저장</button>
        </div>
      </div>
    </div>
  </div>
</div>
</html>