function updateSousFamillesInventorieesInputs (sousFamillesInventorieesState) {
    const divInputs = $('#input_FAMILLE');

    divInputs.empty();
    for (let key in sousFamillesInventorieesState) {
        let famille = sousFamillesInventorieesState[key];

        $('<input type="hidden" name="SFAMINVE['+key+'][FAM]" value="'+famille.FAM+'">').appendTo(divInputs);
        $('<input type="hidden" name="SFAMINVE['+key+'][SSFAM]" value="'+famille.SSFAM+'">').appendTo(divInputs);

        for (let key in famille.SOUS_FAMILLES) {
            const sousFamille = famille.SOUS_FAMILLES[key];

            $('<input type="hidden" name="SSFAMINV['+famille.FAM+']['+key+'][FAM]" value="'+famille.FAM+'">').appendTo(divInputs);
            $('<input type="hidden" name="SSFAMINV['+famille.FAM+']['+key+'][SSFAM]" value="'+sousFamille.CODE_SFA+'">').appendTo(divInputs);
            $('<input type="hidden" name="SSFAMINV['+famille.FAM+']['+key+'][SEL_ART]" value="'+sousFamille.SEL_ART+'">').appendTo(divInputs);
        }
    }
}

function updateArticlesInventoriesInputs (articlesInventoriesState) {
    const articlesInventoriesInput = $('#input_SARTINVE');

    articlesInventoriesInput.empty();
    for (let key in articlesInventoriesState) {
        $('<input type="hidden" name="SARTINVE['+key+'][ART]" value="'+articlesInventoriesState[key]['ART']+'">').appendTo(articlesInventoriesInput);
        $('<input type="hidden" name="SARTINVE['+key+'][FAM]" value="'+articlesInventoriesState[key]['FAM']+'">').appendTo(articlesInventoriesInput);
        $('<input type="hidden" name="SARTINVE['+key+'][SSFAM]" value="'+articlesInventoriesState[key]['SSFAM']+'">').appendTo(articlesInventoriesInput);
    }
}

function updateCahiersInventaireInputs (state) {
    const cahiersInventaireInputs = $('#input_SCINVENT');

    cahiersInventaireInputs.empty();
    for (let key in state) {
        $('<input type="hidden" name="SCINVENT['+key+'][SITE]" value="'+state[key]['SITE']+'">').appendTo(cahiersInventaireInputs);
        $('<input type="hidden" name="SCINVENT['+key+'][MAG]" value="'+state[key]['MAG']+'">').appendTo(cahiersInventaireInputs);
        $('<input type="hidden" name="SCINVENT['+key+'][DEVISE]" value="'+state[key]['DEVISE']+'">').appendTo(cahiersInventaireInputs);
        $('<input type="hidden" name="SCINVENT['+key+'][VAL_DEVIS]" value="'+state[key]['VAL_DEVIS']+'">').appendTo(cahiersInventaireInputs);
    }
}

function updateArticlesInventoriesState (stockArticles) {
    setState('input_SARTINVE', stockArticles);

    return stockArticles;
}

function updateArticlesInventories (path, data, onSuccess = null) {
    $.ajax({
        type: 'POST',
        url: path,
        data: data,
        success: (stockArticles) => {
            updateArticlesInventoriesInputs(updateArticlesInventoriesState(stockArticles));

            if (onSuccess) {
                onSuccess(stockArticles);
            }

            desactiveLoader();
        }
    });
}

function updateFamillesInventories (path) {
    $.ajax({
        type: 'POST',
        url: path,
        data: {
            FAMILLES: getState('input_FAMILLE'),
            SARTICLES: getState('input_SARTINVE'),
            SITE: getSiteParameters()
        },
        success: (famillesInventorieesState) => {
            /**
             * @todo supprimer ce patch et améliorer la création du state en back
             */
            for (const i in famillesInventorieesState) {
                if (typeof famillesInventorieesState[i].SOUS_FAMILLES === 'object') {
                    famillesInventorieesState[i].SOUS_FAMILLES = Object.values(famillesInventorieesState[i].SOUS_FAMILLES);
                }
            }

            updateSousFamillesInventorieesInputs(famillesInventorieesState);
            setState('input_FAMILLE', famillesInventorieesState);
        }
    });
}

function filterArticlesInventories (articlesInventoriesStatePath, famillesInventorieesStatePath, payload) {
    updateArticlesInventories(articlesInventoriesStatePath, payload, (stockArticles) => {
        setState('input_SARTINVE', []);
        updateArticlesInventoriesInputs(updateArticlesInventoriesState(stockArticles));
        updateFamillesInventories(famillesInventorieesStatePath);
    });

    desactivePopup();
}

function getSiteParameters () {
    return getState('input_SCINVENT').map((cahierInventaire) => cahierInventaire.SITE);
}