/**
 * Created by Administrator on 2021/4/5.
 */
define(['jquery', "template", "Tools", "cupload"], function ($, template, Tools) {
    //数据存储
    var module = {
        data: {

        },
    };
    var tools = new Tools();

    module.init = function (page) {

        tools.doGet(dictType + '/supply_demand_sort', {}, module.dictType , true);
        tools.doGet(productCategories , {}, module.projectList , true);

        var cupload2 = new Cupload ({
            ele: '#cupload-3',
            num: 5,
        });

    };

    //类别
    module.projectList = function (data) {
        if (data.code == 200) {
            var content = data.data;
            module.data.projectList = content;

            addSelect(content[0].id);

            var projectData = template('projectData', module.data);
            $("#projectContent").html(projectData);
            // InfinitePullDown('#projectContent',content);
        }
    }

    addSelect = function(id){

        tools.doGet(productDetails + '/'+id, {}, function(data){

            if ( data.data.length == 0 ){

                return;
            }

            var options = '';

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

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

            }

            $('#projectContent').append(

                '<select onchange="changeSelect(this)" style="width: 49%;">' +

                options +

                '</select>'

            )

            addSelect(data.data[0].id)

        } , true);

    }

    changeSelect = function(data){
        const index = $('#projectContent > select').index(data) + 1;

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

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

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

            console.log($('#projectContent').find("select")[i])
            $('#projectContent').find("select")[i].remove();

        }
        setTimeout(function(){
            addSelect(data.value)
        },1000)

    }

    module.dictType = function (data) {
        var content = data.data;
        module.data.sortOptions = content;
        var sortData = template('sortData', module.data);
        $("#sortContent").html(sortData);
    }

    return module;
});