<!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/board/contentWrite.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 my-auto pe-0">
              <input type="checkbox" id="status" name="status" value="M" th:checked="${type=='new'?false:(content.status=='M'?true:false)}">
            </div>
            <label for="status" class="col-auto col-form-label">메인페이지 노출</label>
            <div class="col-auto">
              <button class="bi bi-save btn btn-primary" id="saveBtn"> 저장</button>
            </div>
          </div>
          <form id="contentForm" th:action="@{/board/saveContent}" method="post">
            <th:block th:if="${type!='new'}">
              <input type="hidden" name="contentSeq" th:value="${content.contentSeq}">
            </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 categorySelector" data-depth="1">
                  <option value="" selected>분류를 선택해주세요</option>
                  <th:block th:each="depth1:${session.categoryList}">
                    <option th:class="${#strings.contains(userRole,'ADMIN')?'bg-success bg-opacity-25':(#lists.contains(categoryRole, depth1.categorySeq)?'bg-success bg-opacity-25':'')}"
                            th:value="${depth1.categorySeq}" th:text="${depth1.categoryName}"></option>
                  </th:block>
                </select>
              </div>
              <div class="col-sm">
                <select class="form-select categorySelector" data-depth="2" disabled>
                  <option value="" selected>분류를 선택해주세요</option>
                  <th:block th:each="depth1:${session.categoryList}">
                    <th:block th:each="depth2:${depth1.childCategoryList}">
                      <option th:class="${#strings.contains(userRole,'ADMIN')?'bg-success bg-opacity-25':(#lists.contains(categoryRole, depth2.categorySeq)?'bg-success bg-opacity-25':'')}"
                              th:value="${depth2.categorySeq}" th:data-parentseq="${depth2.parentSeq}" th:text="${depth2.categoryName}"></option>
                    </th:block>
                  </th:block>
                </select>
              </div>
              <div class="col-sm">
                <select class="form-select categorySelector" data-depth="3" disabled>
                  <option value="" selected>분류를 선택해주세요</option>
                  <th:block th:each="depth1:${session.categoryList}">
                    <th:block th:each="depth2:${depth1.childCategoryList}">
                      <th:block th:each="depth3:${depth2.childCategoryList}">
                        <option th:class="${#strings.contains(userRole,'ADMIN')?'bg-success bg-opacity-25':(#lists.contains(categoryRole, depth3.categorySeq)?'bg-success bg-opacity-25':'')}"
                                th:value="${depth3.categorySeq}" th:data-parentseq="${depth3.parentSeq}" th:text="${depth3.categoryName}"></option>
                      </th:block>
                    </th:block>
                  </th:block>
                </select>
              </div>
              <div class="col-sm">
                <select class="form-select categorySelector" data-depth="4" name="categorySeq" id="categorySeq" disabled>
                  <option value="" th:selected="${type=='new'}">분류를 선택해주세요</option>
                  <th:block th:each="depth1:${session.categoryList}">
                    <th:block th:each="depth2:${depth1.childCategoryList}">
                      <th:block th:each="depth3:${depth2.childCategoryList}">
                        <th:block th:each="depth4:${depth3.childCategoryList}">
                          <option class="bg-opacity-25" th:value="${depth4.categorySeq}" th:data-parentseq="${depth4.parentSeq}"
                                  th:classappend="${#strings.contains(userRole,'ADMIN')?'bg-success':(#lists.contains(categoryRole, depth4.categorySeq)?'bg-success':'bg-danger')}"
                                  th:text="${depth4.categoryName}" th:selected="${type=='modify' && content.categorySeq == depth4.categorySeq}"></option>
                        </th:block>
                      </th:block>
                    </th:block>
                  </th:block>
                </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="${type!='new'?content.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'?content.createId:userId}">
                <input type="text" class="form-control" id="createName" th:value="${type!='new'?content.createName:userName}" 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(content.createDate, 'yyyy-MM-dd HH:mm:dd')):'저장시 생성'}" readonly>
              </div>
            </div>
            <div class="row mb-3">
              <label for="fileInputer" class="col-sm-2 col-form-label">업로드 자료</label>
              <div class="col-sm-10" style="min-height: 70px;">
                <div class="w-100 h-100 border border-info rounded text-center" id="uploadDiv">
                  <th:block th:if="${type=='new'}">
                    <br>파일을 업로드 해주세요.
                  </th:block>
                  <th:block th:if="${type=='modify'}">
                    <div class='row-col-6' th:each="fileInfo:${content.childFileList}">
                      <span th:data-fileseq="${fileInfo.fileSeq}" th:text="|${fileInfo.originalName}.${fileInfo.extention} ${fileInfo.fileSize}|"></span>
                      <a href='#' class='uploadedFileDelete text-danger text-decoration-none'>삭제</a>
                    </div>
                  </th:block>
                </div>
              </div>
              <input type="file" class="d-none" id="fileInputer" multiple>
            </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="${type!='new'?content.description:''}"></textarea>
              </div>
            </div>
            <div class="row mb-3">
              <label for="hashTag" class="col-sm-2 col-form-label">해시태그</label>
              <div class="col-sm-10">
                  <input type="text" class="form-control" id="hashTag" name="hashTagStr" th:value="${type!='new'?content.hashTagStr:''}"
                         placeholder="태그는 앞에 #을 붙여 띄어쓰기 없이 작성해주세요. ex) #태그1, #태그2, ..." autocomplete="off">
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </main>
</div>
</html>