<script>
    function validateAndSubmit() {
        const form = document.getElementById('form-document');
        const requiredFields = form.querySelectorAll('[required]');
        let allFieldsFilled = true;

        requiredFields.forEach(field => {
            if (!field.value.trim()) {
                if (field.name === "document-import") {
                    const fileDoc = document.getElementById('file-document-import');
                    fileDoc.style.borderColor = 'red';
                }
                allFieldsFilled = false;
                field.style.borderColor = 'red';
            } else {
                field.style.borderColor = '';
                if (typeof fileDoc !== 'undefined' && fileDoc !== null) {
                    fileDoc.style.borderColor = '';
                }
            }
        });

        if (allFieldsFilled) {
            form.submit();
        }
    }

    function dargAndDrop() {
        var fileName = $("#document-import").val().split('\\').pop();
        var ext = fileName.split('.').pop();
        if (ext !== "pdf" && ext !== "png" && ext !== "jpg" && ext !== "xml") {
            $("#printToastErrorTxt").html("Seuls les fichiers PDF, JPG, PNG et XML sont acceptés");
            $("#printToastError").toast('show');
            $('#attachement-document').val("");
        } else {
            $(".file-message").text($(".file-message").text().replace("ou drag & drop", fileName));
        }
    }

    function dargAndDropAttachment() {
        var fileName = $("#attachement-document").val().split('\\').pop();
        var ext = fileName.split('.').pop();
        if (ext !== "pdf" && ext !== "png" && ext !== "jpg" && ext !== "xml") {
            $("#printToastErrorTxt").html("Seuls les fichiers PDF, JPG, PNG et XML sont acceptés");
            $("#printToastError").toast('show');
            $('#attachement-document').val("");
        } else {
            $(".file-message").text($(".file-message").text().replace("ou drag & drop", fileName));
        }
    }

    function resetAttachement(id) {
        $('#attach-' + id).css('background-color', '');
        $('#select-attach-' + id).html('');
        $('#button-delete-' + id).html('<?php echo icon::getFont(["icon" => "bi bi-trash"]) ?>');
        $('#button-default-' + id).html('<?php echo icon::getFont(["icon" => "bi bi-paperclip"]) ?>');
    }

    function deleteAttachment(id) {
        if ($('#del-jquery-' + id).length) {
            $('#attach-' + id).css('background-color', '');
            $('#select-attach-' + id).css('color', '');
            $('#select-attach-' + id).html('');
            $('#button-delete-' + id).html('<?php echo icon::getFont(["icon" => "bi bi-trash"]) ?>');
        } else {
            resetAttachement(id);
            $('#attach-' + id).css('background-color', '#f8d7da');
            $('#select-attach-' + id).css('color', 'red');
            $('#select-attach-' + id).html('Cette pièce jointe sera supprimée à la validation du formulaire.<input id="del-jquery-' + id + '" type="hidden" name="delete-attachement[]" value="' + id + '">');
            $('#button-delete-' + id).html('<?php echo icon::getFont(["icon" => "bi bi-x-square-fill"]) ?>');
        }
    }

    function defaultAttachment(id) {

        if ($('#default-jquery-' + id).length) {
            $('#attach-' + id).css('background-color', '');
            $('#select-attach-' + id).css('color', '');
            $('#select-attach-' + id).html('');
            $('#button-default-' + id).html('<?php echo icon::getFont(["icon" => "bi bi-paperclip"]) ?>');
        } else {
            resetAttachement(id);

            $('[data-default="attach"]').css('background-color', '');
            $('[data-default="select-attach"]').css('color', '');
            $('[data-default="select-attach"]').html('');
            $('[data-default="button-default"]').html('<?php echo icon::getFont(["icon" => "bi bi-paperclip"]) ?>');

            $('#attach-' + id).attr('data-default', 'attach');
            $('#attach-' + id).css('background-color', '#cce5ff');
            $('#select-attach-' + id).attr('data-default', 'select-attach');
            $('#select-attach-' + id).css('color', 'blue');
            $('#select-attach-' + id).html('Cette pièce jointe sera définie à défaut à la validation du formulaire.<input id="default-jquery-' + id + '" type="hidden" name="default-attachement" value="' + id + '">');
            $('#button-default-' + id).attr('data-default', 'button-default');
            $('#button-default-' + id).html('<?php echo icon::getFont(["icon" => "bi bi-x-square-fill"]) ?>');
        }
    }

    function upperCase(input) {
        input.value = input.value.toUpperCase();
    }

    $(document).ready(function() {
        $('#tagsUser').inputTags({
            autocomplete: {
                values: <?php echo tags::getJquery(1) ?>,
                only: true
            },
            max: 3
        });

        $('#tagsSupplier').inputTags({
            autocomplete: {
                values: <?php echo tags::getJquery(2) ?>
            },
            editable: false,
            max: 5
        });
    });

    function downloadFile(url, nomFichier) {
        const lien = document.createElement('a');
        lien.href = url;
        lien.download = nomFichier;
        document.body.appendChild(lien);
        lien.click();
        document.body.removeChild(lien);
    }
</script>