/*!
 * @module 无限极下拉框
 * 2022-2-9
 * by 庞东旭
 */

let array = [];

let childrenArray = [];

function InfinitePullDown(boxId,data){

    $('' + boxId + ' > select').change(function () {

        const index = $('' + boxId + ' > select').index(this) + 1;

        const selectLength = $(boxId).children('select').length;

        let id = $(this).val();

        console.log(index+'-----------------'+selectLength)

        if (index == 1){
            childrenArray = data ;
        }

        forArray(childrenArray,index,id)

        if (index < selectLength){

            for (let i = index ; i < selectLength ; i++){

                $($(boxId).find("select")[i].remove());

            }

        }

        // if (index == 1){

        array = eval(data).filter(function (e) {
            return e.id == id;
        })

        // }else{
        //
        //     childrenArray = eval(childrenArray[0].children).filter(function (e) { return e.id == id; })
        //
        // }

        console.log(childrenArray)


        addArray(boxId,array[0])

    })

};

function addArray(boxId,data){

    if (data.children.length>0){

        var options = '';

        for (var i = 0 ; i < data.children.length ; i++){

            options += '<option value="' + data.children[i].id + '">' + data.children[i].classificationName + '</option>' ;

        }

        $(boxId).append(

            '<select style="width: 49%;">' +

            options +
            '</select>'

        )

        InfinitePullDown('#projectContent',data.children)

    }
};

let num = 0;

function forArray(data,index,id){

    num++;

    let temporaryData = [];

    if (num == index){return;}

    temporaryData = eval(data).filter(function (e) { return e.id == id; });

    for (let i = 0 ; i < data.length ; i++){

        console.log(temporaryData.length)

        console.log(data)

        if (temporaryData.length == 0){
            forArray(data[i].children,index,id)
        }else{
            temporaryData = eval(data).filter(function (e) { return e.id == id; });
            return;
        }
        console.log(temporaryData)
        // forArray(array,index);
    }

}