<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{layout/default_layout}">
<body>

<section layout:fragment="contentFragment">
    <div class="container-fluid">
        <div class="card mt-2">
            <div class="card-header">
                <h3 th:if="${user.esntlId == null}">내부 사용자 등록</h3>
                <h3 th:if="${user.esntlId != null}">내부 사용자 수정</h3>
            </div>
            <div class="card-body">
                <form name="staffUserForm" id="staffUserForm" th:action="${user.esntlId==null}?@{/mgmt/users/register.do}:@{/mgmt/users/update.do}" th:object='${user}' method="post">
                    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
                    <div th:if="${user.esntlId != null}" class="form-floating">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('esntlId')}? 'is-invalid'" th:field="*{esntlId}" name="esntlId" id="esntlId" readonly/>
                        <label for="esntlId" class="form-label must" th:text="#{esntlId}">고유ID</label>
                    </div>
                    <div class="form-floating mt-2">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('userId')}? 'is-invalid'" th:field="*{userId}" name="userId" id="userId" required th:readonly="${user.esntlId != null}"/>
                        <label for="userId" class="form-label must">사용자 ID</label>
                        <th:block th:each="err : ${#fields.errors('userId')}">
                            <div th:text="${err}" class="invalid-feedback"></div>
                        </th:block>
                    </div>
                    <div class="form-floating mt-2" th:if="${user.esntlId == null}">
                        <input type="password" class="form-control" th:classappend="${#fields.hasErrors('password')}? 'is-invalid'" th:field="*{password}" name="password" id="password" required/>
                        <label for="password" class="form-label must" th:text="#{password}">비밀번호</label>
                        <th:block th:each="err : ${#fields.errors('password')}">
                            <div th:text="${err}" class="invalid-feedback"></div>
                        </th:block>
                    </div>
                    <div class="form-floating mt-2" th:if="${user.esntlId == null}">
                        <input type="password" class="form-control" th:classappend="${#fields.hasErrors('password2')}? 'is-invalid'" th:field="*{password2}" name="password2" id="password2" required/>
                        <label for="password2" class="form-label must">비밀번호 확인</label>
                        <th:block th:each="err : ${#fields.errors('password2')}">
                            <div th:text="${err}" class="invalid-feedback"></div>
                        </th:block>
                    </div>

                    <div class="form-floating mt-2">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('username')}? 'is-invalid'" th:field="*{username}" name="username" id="username" required/>
                        <label for="username" class="form-label must" th:text="#{username}">이름</label>
                        <th:block th:each="err : ${#fields.errors('username')}">
                            <div th:text="${err}" class="invalid-feedback"></div>
                        </th:block>
                    </div>

                    <div class="form-floating mt-2">
                        <select class="form-select"  th:field="*{userSecurity}">
                            <option th:selected="${user.userSecurity!= null && user.userSecurity == 'ROLE_ADMIN'}" value="ROLE_ADMIN">관리자</option>
                            <option th:selected="${user.userSecurity!= null && user.userSecurity == 'ROLE_USER'}" value="ROLE_USER">사용자</option>
                        </select>
                        <label class="form-label must">권한</label>
                    </div>

                    <div class="form-floating mt-2">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('mobilePhone')}? 'is-invalid'" th:field="*{mobilePhone}" name="mobilePhone" id="mobilePhone" required/>
                        <label for="mobilePhone" class="form-label must" th:text="#{mobilePhone}">연락처</label>
                        <th:block th:each="err : ${#fields.errors('mobilePhone')}">
                            <div th:text="${err}" class="invalid-feedback"></div>
                        </th:block>
                    </div>

                    <div th:if="${user.esntlId != null}" class="form-floating mt-2">
                        <select name="status" class="form-select" th:classappend="${#fields.hasErrors('status')}? 'is-invalid'" id="status">
                            <option th:selected="${user.status.toString() == 'WAITING'}" value="WAITING">가입승인 대기</option>
                            <option th:selected="${user.status.toString() == 'ACTIVE'}" value="ACTIVE">활성화</option>
                            <option th:selected="${user.status.toString() == 'INACTIVE'}" value="INACTIVE">비활성화</option>
                            <option th:selected="${user.status.toString() == 'LOCKED'}" value="LOCKED">잠김</option>
                        </select>
                        <label for="status" class="form-label must" th:text="#{status}">상태</label>
                    </div>
                    <div th:if="${user.esntlId != null}" class="form-floating mt-2">
                        <select name="lockAt" class="form-select" th:classappend="${#fields.hasErrors('lockAt')}? 'is-invalid'" id="lockAt">
                            <option th:selected="${user.lockAt.toString() == 'N'}" value="N">해제</option>
                            <option th:selected="${user.lockAt.toString() == 'Y'}" value="Y">잠금</option>
                        </select>
                        <label for="lockAt" class="form-label must" th:text="#{lockAt}">잠금여부</label>
                    </div>
                    <div th:if="${user.esntlId != null}" class="form-floating mt-2">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('lockCnt')}? 'is-invalid'" th:field="*{lockCnt}" name="lockCnt" id="lockCnt" readonly/>
                        <label for="lockCnt" class="form-label must" th:text="#{lockCnt}">잠금 횟수</label>
                    </div>
                    <div th:if="${user.esntlId != null}" class="form-floating mt-2">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('createdAt')}? 'is-invalid'" th:field="*{createdAt}" name="createdAt" id="createdAt" readonly/>
                        <label for="createdAt" class="form-label must">등록 일시</label>
                    </div>
                    <div th:if="${user.esntlId != null}" class="form-floating mt-2">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('lastLockDate')}? 'is-invalid'" th:field="*{lastLockDate}" name="lastLockDate"
                               id="lastLockDate" readonly/>
                        <label for="lastLockDate" class="form-label must" th:text="#{lastLockDate}">잠금최종시점</label>
                    </div>
                    <div th:if="${user.esntlId != null}" class="form-floating mt-2">
                        <input type="text" class="form-control" th:classappend="${#fields.hasErrors('lastPasswordChangeDate')}? 'is-invalid'" th:field="*{lastPasswordChangeDate}"
                               name="lastPasswordChangeDate"
                               id="lastPasswordChangeDate" readonly/>
                        <label for="lastPasswordChangeDate" class="form-label must" th:text="#{lastPasswordChangeDate}">비밀번호 변경일자</label>
                    </div>
                </form>
            </div>
            <div class="card-footer">
                <button type="button" class="btn btn-primary btn_list"><i class="fa-solid fa-list"></i> 목록</button>
                <button type="button" class="btn btn-primary btn_register"><i class="fa-sharp fa-solid fa-floppy-disk"></i> 저장</button>
            </div>
        </div>
    </div>
</section>
</body>

<th:block layout:fragment="contentScript">
    <script>
        $(function () {
            let fnRegister = document.querySelector(".btn_register");
            fnRegister.addEventListener("click", function (event) {
                let form = document.getElementById("staffUserForm");
                if (!form.checkValidity()) {
                    event.preventDefault()
                    event.stopPropagation()
                } else {
                    form.submit();
                }
                form.classList.add('was-validated');
            });

            let fnList = document.querySelector(".btn_list");

            fnList.addEventListener("click", function () {
                location.href = "[[@{/mgmt/users/list_view.do}]]";
            });
        });
    </script>
</th:block>
</html>

