define(['jquery', "template", "Tools",'swiper',], function ($, template, Tools, swiper) {
    //数据存储
    var module = {
        data: {
            //服务器地址
            serverApi: '',
            trainStudentsList:[{
                name:'',
                phone:'',
                sex: '',
                trainId:''
            }]
        },
    };
    var tools = new Tools();

    module.init = function (page) {
        //新闻管理列表
        tools.doGet(dictType + '/training_type', {}, module.dictType , true);
        tools.doGet(dictType + '/apply_state', {}, function(data){module.data.studentsTypeOptions = data.data;} , true);
        module.data.trainStudentsList[0].trainId = getQueryVariable('id');

    };

    module.dictType = function (data) {
        module.data.trainingTypeOptions = data.data;
        tools.doPost(technologyTrainingDetails + '/' + getQueryVariable('id'), {}, module.technologyTrainingDetails, true);
    }

    //获取地址栏参数
    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    //焦点图滚动
    module.swiperBanner = function (type) {
        var viewSwiper = new Swiper('.view .swiper-container', {
            onSlideChangeStart: function() {
                updateNavPosition()
            }
        })

        $('.view .arrow-left,.preview .arrow-left').on('click', function(e) {
            e.preventDefault()
            if (viewSwiper.activeIndex == 0) {
                viewSwiper.swipeTo(viewSwiper.slides.length - 1, 1000);
                return
            }
            viewSwiper.swipePrev()
        })
        $('.view .arrow-right,.preview .arrow-right').on('click', function(e) {
            e.preventDefault()
            if (viewSwiper.activeIndex == viewSwiper.slides.length - 1) {
                viewSwiper.swipeTo(0, 1000);
                return
            }
            viewSwiper.swipeNext()
        })

        var previewSwiper = new Swiper('.preview .swiper-container', {
            visibilityFullFit: true,
            slidesPerView: 'auto',
            onlyExternal: true,
            onSlideClick: function() {
                viewSwiper.swipeTo(previewSwiper.clickedSlideIndex)
            }
        })

        function updateNavPosition() {
            $('.preview .active-nav').removeClass('active-nav')
            var activeNav = $('.preview .swiper-slide').eq(viewSwiper.activeIndex).addClass('active-nav')
            if (!activeNav.hasClass('swiper-slide-visible')) {
                if (activeNav.index() > previewSwiper.activeIndex) {
                    var thumbsPerNav = Math.floor(previewSwiper.width / activeNav.width()) - 1
                    previewSwiper.swipeTo(activeNav.index() - thumbsPerNav)
                } else {
                    previewSwiper.swipeTo(activeNav.index())
                }
            }
        }
    }

    //专家列表
    module.technologyTrainingDetails = function (data) {
        if (data.code == 200) {
            var content = data.data;
            content.trainingType = module.selectDictLabel(module.data.trainingTypeOptions,content.trainingType);

            for (var i = 0 ; i < content.tEntityTrainStudentsList.length ; i++){
                content.tEntityTrainStudentsList[i].applyState = module.selectDictLabel(module.data.studentsTypeOptions,content.tEntityTrainStudentsList[i].applyState);
            }

            const masterMapList = content.masterMap.split(",");
            for (var i = 0 ; i < masterMapList.length ; i++){
                masterMapList[i] = serverApi + masterMapList[i];
            }
            console.log(masterMapList)
            content.masterMap = masterMapList;

            module.data.technologyDetail = content;
            var technologyData = template('technologyData', module.data);
            $("#technologyContent").html(technologyData);
            $('#detailBox').html(content.briefIntroduction)
            module.swiperBanner();
        }
    }

    module.selectDictLabel = function (datas, value) {
        var actions = [];
        Object.keys(datas).some((key) => {
            if (datas[key].dictValue == ('' + value)) {
                actions.push(datas[key].dictLabel);
                return true;
            }
        })
        return actions.join('');
    }

    goDelete = function(index){
        tools.doPost(technologyTrainingStudentsRemove+'/'+module.data.technologyDetail.tEntityTrainStudentsList[index].id, {}, module.updateList , true);
        module.data.technologyDetail.tEntityTrainStudentsList.splice(index, 1);
        var technologyData = template('technologyData', module.data);
        $("#technologyContent").html(technologyData);
    }

    goStudentsDelete = function(index){
        module.data.trainStudentsList.splice(index, 1);
        var technologyData = template('technologyData', module.data);
        $("#technologyContent").html(technologyData);
    }

    goSubmit = function(index){
        tools.doPost(technologyTrainingStudents, module.data.trainStudentsList[index], module.updateList , true);
        var technologyData = template('technologyData', module.data);
        $("#technologyContent").html(technologyData);
    }

    module.updateList = function(data){
        if (data.code == 200){
            tools.doPost(technologyTrainingDetails + '/' + getQueryVariable('id'), {}, module.technologyTrainingDetails, true);
            var technologyData = template('technologyData', module.data);
            $("#technologyContent").html(technologyData);
        }
    }

    inputNameChange = function(index){
        module.data.trainStudentsList[index].name = $('#name'+index+'').val();
    }

    inputPhoneChange = function(index){
        module.data.trainStudentsList[index].phone = $('#phone'+index+'').val();
    }

    inputSexChange = function(index){
        module.data.trainStudentsList[index].sex = $('#sex'+index+'').val();
    }

    goAdd = function(){
        for (var i = 0 ; i < module.data.trainStudentsList.length ; i++){
            if (module.data.trainStudentsList[i].name == '' || module.data.trainStudentsList[i].phone == '' || module.data.trainStudentsList[i].sex == ''){
                return;
            }
        }
        module.data.trainStudentsList.push({
            name:'',
            phone:'',
            sex:'',
            trainId:getQueryVariable('id')
        });
        var technologyData = template('technologyData', module.data);
        $("#technologyContent").html(technologyData);
    }

    return module;
})