<!DOCTYPE html>
<html th:replace="~{modele-page :: layout('Ajouter un article',~{::link} , ~{::#container-main})}" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">

</head>
<body>
<div id="container-main" class="container py-5">
    <h1 class="mb-4" th:text="#{article.add.heading}">Nouvelle vente</h1>
    <form th:action="@{/article/new}" method="post" th:object="${article}" enctype="multipart/form-data" class="needs-validation" novalidate>
        <div class="mb-3">
            <label for="nom" class="form-label" th:text="#{article.add.form.label.name}">Article:</label>
            <input type="text" class="form-control" th:field="*{nom}" id="nom" required>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>

        <div class="mb-3">
            <label for="description" class="form-label" th:text="#{article.add.form.label.description}">Description:</label>
            <textarea class="form-control" th:field="*{desc}" id="description" required></textarea>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>

        <div class="mb-3">
            <label for="categorie" class="form-label" th:text="#{article.add.form.label.category}">Catégorie:</label>
            <select class="form-control" th:field="*{numCategorie}" id="categorie" required>
                <option th:each="categorie : ${categories}" th:value="${categorie.id}" th:text="${categorie.libelle}"></option>
            </select>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.category}">Veuillez sélectionner une catégorie.</div>
        </div>

        <div class="mb-3">
            <label for="photo" class="form-label" th:text="#{article.add.form.label.photo}">Photo de l'article:</label>
            <input type="file" class="form-control" th:field="*{photo}" id="photo" accept="image/jpeg">
        </div>

        <div class="mb-3">
            <label for="prix" class="form-label" th:text="#{article.add.form.label.starting_price}">Mise à prix:</label>
            <input type="number" class="form-control" th:field="*{prixInitial}" id="prix" min="0" max="2000000000" step="0.01" required>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>

        <div class="mb-3">
            <label for="dateDebut" class="form-label" th:text="#{article.add.form.label.start_date}">Date début enchère:</label>
            <input type="date" class="form-control" id="dateDebut" name="dateDebut" required>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>

        <div class="mb-3">
            <label for="dateFin" class="form-label" th:text="#{article.add.form.label.end_date}">Date fin enchère:</label>
            <input type="date" class="form-control" id="dateFin" name="dateFin" required>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>

        <h4 th:text="#{article.add.form.label.removal}">Retrait</h4>
        <div class="mb-3">
            <label for="rue" class="form-label" th:text="#{article.add.form.label.street}">Rue:</label>
            <input type="text" class="form-control" id="rue" name="rue" th:field="${user.rue}" required>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>
        <div class="mb-3">
            <label for="codePostal" class="form-label" th:text="#{article.add.form.label.postal_code}">Code postal:</label>
            <input type="text" class="form-control" id="codePostal" name="code_postal" th:field="${user.code_postal}" required>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>
        <div class="mb-3">
            <label for="ville" class="form-label" th:text="#{article.add.form.label.city}">Ville:</label>
            <input type="text" class="form-control" id="ville" name="ville" th:field="${user.ville}" required>
            <div class="invalid-feedback" th:text="#{article.add.form.validation.required}">Ce champ est requis.</div>
        </div>

        <button type="submit" class="btn btn-primary" th:text="#{article.add.form.button.save}">Enregistrer</button>

        <div th:if="${param.erreur != null}" class="mt-3">
            <p style="color: red;" th:text="${param.erreur}"></p>
        </div>
    </form>
    <form th:action="@{/enchere}" method="post" class="mt-3">
        <button type="submit" class="btn btn-secondary" th:text="#{article.add.form.button.cancel}">Annuler</button>
    </form>
    <script>
        var today = new Date();
        var tomorrow = new Date(today);
        tomorrow.setDate(tomorrow.getDate() + 1);
        var formattedDateToday = today.toISOString().split('T')[0];
        var formattedDateTomorrow = tomorrow.toISOString().split('T')[0];
        document.getElementById('dateDebut').min = formattedDateToday;
        document.getElementById('dateFin').min = formattedDateTomorrow;
    </script>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
</body>
</html>
