<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>WBX Admin - User Detail</title><link rel="stylesheet" th:href="@{/admin/css/admin.css}"></head>
<body>
<div class="admin-layout">
    <div th:replace="~{admin/fragments :: sidebar('users')}"></div>
    <div class="admin-content" th:if="${user}">
        <div class="page-header">
            <h1>사용자 상세: <span th:text="${user.email}"></span></h1>
            <p><a th:href="@{/admin/users}" style="color:#1e3c78;">← 사용자 목록으로</a></p>
        </div>
        <div th:if="${message}" class="alert alert-success" th:text="${message}"></div>
        <div th:if="${error}" class="alert" style="background:#ffebee;color:#c62828;border:1px solid #ffcdd2;padding:12px 16px;border-radius:6px;margin-bottom:16px;font-size:13px;" th:text="${error}"></div>

        <!-- 사용자 정보 수정 폼 -->
        <div style="background:#fff;border-radius:8px;padding:20px;margin-bottom:20px;box-shadow:0 1px 3px rgba(0,0,0,0.08);">
            <h3 style="margin-bottom:12px;font-size:15px;color:#1e3c78;">기본 정보</h3>
            <form th:action="@{/admin/users/{id}/update(id=${user.id})}" method="post">
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-bottom:16px;">
                    <div>
                        <label style="font-size:12px;color:#888;display:block;margin-bottom:4px;">이메일 (변경 불가)</label>
                        <input th:value="${user.email}" disabled style="width:100%;padding:6px 10px;border:1px solid #eee;border-radius:4px;font-size:13px;background:#f5f5f5;">
                    </div>
                    <div>
                        <label style="font-size:12px;color:#888;display:block;margin-bottom:4px;">사용자명 (변경 불가)</label>
                        <input th:value="${user.username}" disabled style="width:100%;padding:6px 10px;border:1px solid #eee;border-radius:4px;font-size:13px;background:#f5f5f5;">
                    </div>
                    <div>
                        <label style="font-size:12px;color:#888;display:block;margin-bottom:4px;">이름</label>
                        <input name="fullName" th:value="${user.fullName}" style="width:100%;padding:6px 10px;border:1px solid #ddd;border-radius:4px;font-size:13px;">
                    </div>
                    <div>
                        <label style="font-size:12px;color:#888;display:block;margin-bottom:4px;">전화번호</label>
                        <input name="phone" th:value="${user.phone}" style="width:100%;padding:6px 10px;border:1px solid #ddd;border-radius:4px;font-size:13px;">
                    </div>
                    <div>
                        <label style="font-size:12px;color:#888;display:block;margin-bottom:4px;">직위</label>
                        <input name="positionTitle" th:value="${user.positionTitle}" style="width:100%;padding:6px 10px;border:1px solid #ddd;border-radius:4px;font-size:13px;">
                    </div>
                    <div>
                        <label style="font-size:12px;color:#888;display:block;margin-bottom:4px;">사번</label>
                        <input name="employeeNumber" th:value="${user.employeeNumber}" style="width:100%;padding:6px 10px;border:1px solid #ddd;border-radius:4px;font-size:13px;">
                    </div>
                </div>
                <div style="display:flex;align-items:center;gap:16px;">
                    <label style="display:flex;align-items:center;gap:4px;font-size:13px;">
                        <input name="isAdmin" type="checkbox" value="true" th:checked="${user.admin}"> 관리자 (SA)
                    </label>
                    <button type="submit" class="btn btn-primary">정보 수정</button>
                </div>
            </form>
        </div>

        <!-- 상태 정보 + 액션 버튼 -->
        <div style="background:#fff;border-radius:8px;padding:20px;margin-bottom:20px;box-shadow:0 1px 3px rgba(0,0,0,0.08);">
            <h3 style="margin-bottom:12px;font-size:15px;color:#1e3c78;">계정 상태</h3>
            <div class="detail-grid" style="box-shadow:none;padding:0;margin-bottom:12px;">
                <div class="label">상태</div>
                <div class="value">
                    <span th:if="${user.active}" class="badge badge-success">활성</span>
                    <span th:unless="${user.active}" class="badge badge-danger">비활성</span>
                </div>
                <div class="label">잠금</div>
                <div class="value">
                    <span th:if="${user.locked}" class="badge badge-danger">잠금 (실패 <span th:text="${user.failedLoginAttempts}"></span>회)</span>
                    <span th:unless="${user.locked}">정상</span>
                </div>
                <div class="label">MFA</div>
                <div class="value" th:text="${user.mfaEnabled} ? '활성화' : '미설정'"></div>
                <div class="label">SSO</div>
                <div class="value" th:text="${user.ssoProvider != null} ? ${user.ssoProvider} : '미연동'"></div>
                <div class="label">최종 로그인</div>
                <div class="value" th:text="${user.lastLoginAt != null} ? ${#temporals.format(user.lastLoginAt, 'yyyy-MM-dd HH:mm:ss')} : '없음'"></div>
                <div class="label">생성일</div>
                <div class="value" th:text="${user.createdAt != null} ? ${#temporals.format(user.createdAt, 'yyyy-MM-dd HH:mm')} : ''"></div>
            </div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <form th:action="@{/admin/users/{id}/unlock(id=${user.id})}" method="post" style="display:inline">
                    <button class="btn btn-primary" type="submit">잠금 해제</button>
                </form>
                <form th:action="@{/admin/users/{id}/reset-password(id=${user.id})}" method="post" style="display:inline">
                    <button class="btn btn-outline" type="submit">비밀번호 초기화</button>
                </form>
                <form th:action="@{/admin/users/{id}/toggle-status(id=${user.id})}" method="post" style="display:inline">
                    <button class="btn" th:classappend="${user.active} ? 'btn-danger' : 'btn-primary'" type="submit" th:text="${user.active} ? '비활성화' : '활성화'"></button>
                </form>
                <form th:action="@{/admin/users/{id}/delete(id=${user.id})}" method="post" style="display:inline;margin-left:auto;" onsubmit="return confirm('이 사용자를 삭제하시겠습니까? 복구할 수 없습니다.')">
                    <button class="btn btn-danger" type="submit">사용자 삭제</button>
                </form>
            </div>
        </div>

        <!-- 역할 할당 -->
        <div style="background:#fff;border-radius:8px;padding:20px;margin-bottom:20px;box-shadow:0 1px 3px rgba(0,0,0,0.08);">
            <h3 style="margin-bottom:12px;font-size:15px;color:#1e3c78;">역할 할당</h3>
            <form th:action="@{/admin/users/{id}/roles/add(id=${user.id})}" method="post" style="display:flex;gap:10px;align-items:end;margin-bottom:16px;">
                <div>
                    <label style="font-size:12px;color:#888;display:block;margin-bottom:4px;">역할 선택</label>
                    <select name="roleId" style="padding:6px 10px;border:1px solid #ddd;border-radius:4px;font-size:13px;">
                        <option th:each="role : ${allRoles}" th:value="${role.id}" th:text="${role.code + ' — ' + role.name}"></option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">역할 추가</button>
            </form>
            <div class="data-table" th:if="${!userRoles.isEmpty()}">
                <table>
                    <thead><tr><th>역할 코드</th><th>역할 이름</th><th>할당일</th><th>관리</th></tr></thead>
                    <tbody>
                    <tr th:each="ur : ${userRoles}">
                        <td>
                            <span th:each="role : ${allRoles}" th:if="${role.id == ur.roleId}" class="badge badge-info" th:text="${role.code}"></span>
                        </td>
                        <td>
                            <span th:each="role : ${allRoles}" th:if="${role.id == ur.roleId}" th:text="${role.name}"></span>
                        </td>
                        <td th:text="${ur.grantedAt != null} ? ${#temporals.format(ur.grantedAt, 'yy-MM-dd HH:mm')} : ''"></td>
                        <td>
                            <form th:action="@{/admin/users/{uid}/roles/{urid}/delete(uid=${user.id},urid=${ur.id})}" method="post" style="display:inline;" onsubmit="return confirm('이 역할을 해제하시겠습니까?')">
                                <button type="submit" class="btn btn-danger" style="padding:3px 10px;font-size:12px;">해제</button>
                            </form>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <p th:if="${userRoles.isEmpty()}" style="color:#888;font-size:13px;">할당된 역할이 없습니다.</p>
        </div>

        <!-- 로그인 이력 -->
        <div style="background:#fff;border-radius:8px;padding:20px;box-shadow:0 1px 3px rgba(0,0,0,0.08);">
            <h3 style="margin-bottom:12px;font-size:15px;color:#1e3c78;">로그인 이력 (최근 10건)</h3>
            <div class="data-table">
                <table>
                    <thead><tr><th>시간</th><th>액션</th><th>IP</th><th>인증방법</th></tr></thead>
                    <tbody>
                    <tr th:each="log : ${loginHistory}">
                        <td th:text="${#temporals.format(log.createdAt, 'yy-MM-dd HH:mm')}"></td>
                        <td>
                            <span th:if="${log.action == 'LOGIN_SUCCESS'}" class="badge badge-success">성공</span>
                            <span th:if="${log.action == 'LOGIN_FAILURE'}" class="badge badge-danger">실패</span>
                            <span th:if="${log.action == 'LOGOUT'}" class="badge badge-info">로그아웃</span>
                        </td>
                        <td th:text="${log.ipAddress}"></td>
                        <td th:text="${log.authMethod}"></td>
                    </tr>
                    <tr th:if="${#lists.isEmpty(loginHistory)}"><td colspan="4" style="text-align:center;color:#888;">이력이 없습니다.</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>
