<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<html>
<head>
    <title><spring:message code="label.addAppUser"/></title>
    <!-- Captcha Google -->
    <script src="https://www.google.com/recaptcha/api.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        .container {
            margin-top: 50px;
            padding: 20px;
            border: 1px solid #ced4da;
            border-radius: 5px;
            background-color: #fff;

        }

        .container h1 {
            margin-bottom: 20px;
        }

        .container table {
            width: 100%;
        }

        .container input[type="text"],
        .container input[type="password"],
        .container select {
            width: 100%;
            padding: 8px 12px;
            margin-bottom: 10px;
            border: 1px solid #ced4da;
            border-radius: 4px;
            font-size: 14px;
        }

        .container input[type="submit"] {
            padding: 8px 12px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: #fff;
            font-size: 14px;
            cursor: pointer;
        }

        .container input[type="submit"]:hover {
            background-color: #0069d9;
        }

        .container .error-message {
            color: red;
        }
    </style>
</head>
<body>
<header data-bs-theme="dark">
    <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">Car Dealer Shop</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarCollapse">
                <ul class="navbar-nav me-auto mb-2 mb-md-0">
                    <sec:authorize access="isAnonymous()">
                        <li class="nav-item">
                            <a class="nav-link" href="/login"><spring:message code="label.login"/></a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/register"><spring:message code="label.register"/></a>
                        </li>
                    </sec:authorize>
                    <sec:authorize access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')">
                        <li class="nav-item">
                            <a class="nav-link" href="/appCars"><spring:message code="label.addCar"/></a>
                        </li>
                    </sec:authorize>
                    <li class="nav-item">
                        <a class="nav-link" href="/appCarList"><spring:message code="label.carheader"/></a>
                    </li>
                    <sec:authorize access="hasRole('ROLE_ADMIN')">
                        <li class="nav-item">
                            <a class="nav-link" href="/appUsers"><spring:message code="label.addAppUser"/></a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="appUserRole"><spring:message code="label.role"/></a>
                        </li>
                    </sec:authorize>
                    <sec:authorize access="hasRole('ROLE_USER')">
                        <li class="nav-item">
                            <a class="nav-link" href="/exampleOne"><spring:message code="label.example"/> 1</a>
                        </li>
                    </sec:authorize>
                    <sec:authorize access="hasRole('ROLE_STUDENT')">
                        <li class="nav-item">
                            <a class="nav-link" href="exampleThree"><spring:message code="label.example"/> 3</a>
                        </li>
                    </sec:authorize>
                </ul>
                <form class="d-flex" role="search">
                    <!-- Search bar removed -->
                </form>
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="?lang=pl">pl</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="?lang=en">en</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="?lang=de">de</a>
                    </li>
                    <sec:authorize access="isAuthenticated()">
                        <li class="nav-item">
                            <a class="nav-link" href="javascript:formSubmit()">Logout</a>
                        </li>
                    </sec:authorize>
                </ul>
            </div>
        </div>
    </nav>
</header>

<div class="container">
    <h1>Add App User Info:</h1>
    <form:form method="post" action="addAppUser" modelAttribute="appUser">
        <table>
            <tr>
                <td><form:hidden path="id" /></td>
            </tr>
            <tr>
                <td><form:label path="login"><spring:message code="label.login" /></form:label></td>
                <td><form:input path="login" /></td>
                <td><form:errors path="login" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="password"><spring:message code="label.password" /></form:label></td>
                <td><form:input type="password" path="password" /></td>
                <td><form:errors path="password" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="enabled"><spring:message code="label.enabled" /></form:label></td>
                <td><form:checkbox path="enabled" /></td>
                <td><form:errors path="enabled" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="firstName"><spring:message code="label.firstName" /></form:label></td>
                <td><form:input path="firstName" /></td>
                <td><form:errors path="firstName" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="lastName"><spring:message code="label.lastName" /></form:label></td>
                <td><form:input path="lastName" /></td>
                <td><form:errors path="lastName" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="email"><spring:message code="label.email" /></form:label></td>
                <td><form:input path="email" /></td>
                <td><form:errors path="email" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="telephone"><spring:message code="label.telephone" /></form:label></td>
                <td><form:input path="telephone" /></td>
                <td><form:errors path="telephone" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="pesel.PESEL"><spring:message code="label.pesel" /></form:label></td>
                <td><form:input path="pesel.PESEL" /></td>
                <td><form:errors path="pesel" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="address"><spring:message code="label.address" /></form:label></td>
                <td><form:select path="address">
                    <c:forEach items="${addressesList}" var="address">
                        <option value="${address.id}" ${address.id == selectedAddress ? 'selected="selected"' : ''}>${address.street}</option>
                    </c:forEach>
                </form:select></td>
                <td><form:errors path="address" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td><form:label path="appUserRole"><spring:message code="label.role" /></form:label></td>
                <td><form:select path="appUserRole" multiple="true">
                    <form:options items="${appUserRoleList}" itemValue="id" itemLabel="role" />
                </form:select></td>
                <td><form:errors path="appUserRole" cssClass="error-message" /></td>
            </tr>
            <tr>
                <td colspan="3">
                    <div class="g-recaptcha" data-sitekey="6LecyigmAAAAACaS3sPkEZPVxJzT7jUmX_Zl4r3o"></div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <c:if test="${appUser.id == 0}">
                        <input type="submit" value="<spring:message code="label.addAppUser" />" class="btn btn-primary" />
                    </c:if>
                    <c:if test="${appUser.id != 0}">
                        <input type="submit" value="<spring:message code="label.editAppUser" />" class="btn btn-primary" />
                    </c:if>
                </td>
            </tr>
        </table>
    </form:form>

    <h3><spring:message code="label.userList" /></h3>
    <c:if test="${!empty appUserList}">
        <table class="table">
            <thead>
            <tr>
                <th><spring:message code="label.firstName" /></th>
                <th><spring:message code="label.lastName" /></th>
                <th><spring:message code="label.email" /></th>
                <th><spring:message code="label.telephone" /></th>
                <th>&nbsp;</th>
                <th>&nbsp;</th>
                <th>&nbsp;</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${appUserList}" var="appUser">
                <tr>
                    <td>${appUser.firstName}</td>
                    <td>${appUser.lastName}</td>
                    <td>${appUser.email}</td>
                    <td>${appUser.telephone}</td>
                    <td><a href="delete/${appUser.id}">delete</a></td>
                    <td><a href="appUsers?appUserId=${appUser.id}">edit</a></td>
                    <td><a href="generatePdf-${appUser.id}">pdf</a></td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </c:if>
</div>
<script>
    function formSubmit() {
        document.getElementById("logoutForm").submit();
    }
</script>

<!-- csrf for log out-->
<form action="/logout" method="post" id="logoutForm">
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>

</body>
</html>