<!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">
  <style>
    .col-form-label{
      text-align: right;
    }
  </style>
</th:block>
<th:block layout:fragment="script">
  <script type="text/javascript">
    const pageType = '[[${type}]]';
  </script>
  <script type="text/javascript" th:src="@{/js/user/modifyRequestWrite.js}"></script>
</th:block>
<div layout:fragment="content">
  <main class="pt-3">
    <h4 th:text="${type=='new'?'수정 요청 등록':'수정 요청 변경'}"></h4>
    <div class="row mx-0">
      <div class="col-7 card">
        <div class="card-body">
          <div class="row mb-3 justify-content-end">
            <div class="col-auto">
              <button class="bi bi-save btn btn-primary" id="saveBtn"> 저장</button>
            </div>
          </div>
          <form id="requestForm" th:action="@{/info/saveModifyRequest}" method="post">
            <th:block th:if="${type!='new'}">
              <input type="hidden" name="requestSeq" th:value="${modifyRequest.requestSeq}">
            </th:block>
            <div class="row mb-3">
              <label for="title" class="col-sm-2 col-form-label">분류 선택</label>
              <div class="col-sm">
                <select class="form-select" id="categorySelector" name="requestType">
                  <option value="" disabled th:selected="${modifyRequest.requestType==null}">분류를 선택해주세요</option>
                  <option value="userInfo" th:selected="${modifyRequest.requestType=='userInfo'}">계정정보</option>
                  <option value="categoryRole" th:selected="${modifyRequest.requestType=='categoryRole'}">권한정보</option>
                  <option value="etc" th:selected="${modifyRequest.requestType=='etc'}">기타</option>
                </select>
              </div>
            </div>
            <div class="row mb-3">
              <label for="title" class="col-sm-2 col-form-label">제목</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="title" name="title" autocomplete="off" th:value="${modifyRequest.title}">
              </div>
            </div>
            <div class="row mb-3">
              <label for="createName" class="col-sm-2 col-form-label">작성자</label>
              <div class="col-sm-4">
                <input type="hidden" name="createId" th:value="${type!='new'?modifyRequest.createId:loginUser.userId}">
                <input type="text" class="form-control" id="createName" th:value="${type!='new'?modifyRequest.createName:loginUser.name}" readonly>
              </div>
              <label for="createDate" class="col-sm-2 col-form-label">작성일</label>
              <div class="col-sm-4">
                <input type="text" class="form-control" id="createDate"
                       th:value="${type!='new'?(#temporals.format(modifyRequest.createDate, 'yyyy-MM-dd HH:mm:dd')):'저장시 생성'}" readonly>
              </div>
            </div>
            <div class="row mb-3">
              <label for="description" class="col-sm-2 col-form-label">변경 내용</label>
              <div class="col-sm-10" id="modifyInfoDiv">
                <div class="p-3 border rounded">분류를 선택해주세요.</div>
              </div>
            </div>
            <div class="row mb-3">
              <label for="description" class="col-sm-2 col-form-label">설명</label>
              <div class="col-sm-10">
                <textarea id="description" name="description" th:text="${modifyRequest.description}"></textarea>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </main>
  <div class="d-none">
    <div id="etcDiv">
      <div class="p-3 border rounded">
        설명란에 내용 직접 입력.
      </div>
    </div>
    <div id="userInfoDiv">
      <div class="p-3 border rounded">
        <div class="row mb-3">
          <label for="requestName" class="col-sm-2 col-form-label">이름</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="requestName" name="requestName" th:value="${type!='new'?modifyRequest.requestName:loginUser.name}">
          </div>
        </div>
        <div class="row mb-3">
          <label for="requestDepartment" class="col-sm-2 col-form-label">부서</label>
          <div class="col-sm-10">
            <select class="form-select" id="requestDepartment" name="requestDepartment">
              <th:block th:each="deparement:${session.departmentList}">
                <option th:value="${deparement.codeSq}" th:text="${deparement.value}" th:selected="${type!='new'?modifyRequest.requestDepartment:loginUser.department}"></option>
              </th:block>
            </select>
          </div>
        </div>
        <div class="row mb-3">
          <label for="requestPosition" class="col-sm-2 col-form-label">직책</label>
          <div class="col-sm-10">
            <select class="form-select" id="requestPosition" name="requestPosition">
              <th:block th:each="position:${session.positionList}">
                <option th:value="${position.codeSq}" th:text="${position.value}" th:selected="${type!='new'?modifyRequest.requestPosition:loginUser.position}"></option>
              </th:block>
            </select>
          </div>
        </div>
      </div>
    </div>
    <div id="categoryRoleDiv">
      <div class="p-3 border rounded">
        <div class="row justify-content-between">
          <div class="col-auto"><button type="button" class="btn btn-info" id="moveLeftBtn"><i class="bi bi-arrow-left"></i></button></div>
          <div class="col-auto"><button type="button" class="btn btn-info" id="moveRightBtn"><i class="bi bi-arrow-right"></i></button></div>
        </div>
        <div class="row overflow-auto flex-nowrap" id="categorySelectBody">
          <th:block th:each="depth1:${session.categoryList}">
            <div class="col-auto">
              <table class="table table-striped">
                <thead>
                <tr>
                  <th>
                    <th:block th:if="${type=='new'}">
                      <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                             th:value="${depth1.categorySeq}" th:checked="${#lists.contains(categorySeqList, depth1.categorySeq)}">
                    </th:block>
                    <th:block th:unless="${type=='new'}">
                      <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                             th:value="${depth1.categorySeq}" th:checked="${#lists.contains(modifyRequest.requestCategoryList, depth1.categorySeq)}">
                    </th:block>
                  </th>
                  <th colspan="3" class="text-center" th:text="${depth1.categoryName}"></th>
                </tr>
                <tr>
                  <th></th>
                  <th>연도</th>
                  <th>중분류</th>
                  <th>소분류</th>
                </tr>
                </thead>
                <tbody>
                <th:block th:each="depth2:${depth1.childCategoryList}">
                  <tr>
                    <td>
                      <th:block th:if="${type=='new'}">
                        <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                               th:value="${depth2.categorySeq}" th:data-parentseq="${depth2.parentSeq}"
                               th:checked="${#lists.contains(categorySeqList, depth2.categorySeq)}">
                      </th:block>
                      <th:block th:unless="${type=='new'}">
                        <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                               th:value="${depth2.categorySeq}" th:data-parentseq="${depth2.parentSeq}"
                               th:checked="${#lists.contains(modifyRequest.requestCategoryList, depth2.categorySeq)}">
                      </th:block>
                    </td>
                    <td th:text="${depth2.categoryName}"></td>
                    <td></td>
                    <td></td>
                  </tr>
                  <th:block th:each="depth3:${depth2.childCategoryList}">
                    <tr>
                      <td>
                        <th:block th:if="${type=='new'}">
                          <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                                 th:value="${depth3.categorySeq}" th:data-parentseq="${depth3.parentSeq}"
                                 th:checked="${#lists.contains(categorySeqList, depth3.categorySeq)}">
                        </th:block>
                        <th:block th:unless="${type=='new'}">
                          <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                                 th:value="${depth3.categorySeq}" th:data-parentseq="${depth3.parentSeq}"
                                 th:checked="${#lists.contains(modifyRequest.requestCategoryList, depth3.categorySeq)}">
                        </th:block>
                      </td>
                      <td></td>
                      <td th:text="${depth3.categoryName}"></td>
                      <td></td>
                    </tr>
                    <th:block th:each="depth4:${depth3.childCategoryList}">
                      <tr>
                        <td>
                          <th:block th:if="${type=='new'}">
                            <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                                   th:value="${depth4.categorySeq}" th:data-parentseq="${depth4.parentSeq}"
                                   th:checked="${#lists.contains(categorySeqList, depth4.categorySeq)}">
                          </th:block>
                          <th:block th:unless="${type=='new'}">
                            <input type="checkbox" class="categoryCheckBox parentSeq" name="categorySeq"
                                   th:value="${depth4.categorySeq}" th:data-parentseq="${depth4.parentSeq}"
                                   th:checked="${#lists.contains(modifyRequest.requestCategoryList, depth4.categorySeq)}">
                          </th:block>
                        </td>
                        <td></td>
                        <td></td>
                        <td th:text="${depth4.categoryName}"></td>
                      </tr>
                    </th:block>
                  </th:block>
                </th:block>
                </tbody>
              </table>
            </div>
          </th:block>
        </div>
      </div>
    </div>
  </div>
</div>
</html>