define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'student/practice/index' + location.search,
                    table: 'teacher_collection',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'exam_collection_id',
                sortName: 'exam_collection_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'exam_collection_id', title: __('编号')},
                        {field: 'exam_collection_name', title: __('Exam_collection_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'sim_type', title: __('Sim_type'),searchList:{"0001":"FZD04B型侦毒器","0002":"FZB006型毒剂报警器","0003":"防化兵用毒剂报警器"},formatter: Table.api.formatter.simtype},
                        {field: 'question_setting_method', title: __('Question_setting_method'),searchList:{"1":"系统随机","2":"教师自选","3":"任务自选"},formatter: Table.api.formatter.simmethod},
                        // {field: 'exam_collection_type', title: __('Exam_collection_type'),operate: false,formatter: function (value, row, index) {
                        //         return '考试';
                        //     }
                        // },
                        // {
                        //     field: 'exam_collection_state',searchList:{"2":"打开","3":"关闭"},
                        //     title: __('Exam_collection_state'),
                        //     align: 'center',
                        //     table: table,disable:true,
                        //     formatter: Table.api.formatter.collectonstate
                        // },

                        {field: 'limit_duration', title: __('Limit_duration')},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'create_user_id', title: __('Create_user_id')},
                        {field: 'create_by', title: __('Create_by'), operate: 'LIKE'},
                        // {field: 'createtime', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'update_by', title: __('Update_by'), operate: 'LIKE'},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate,
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'dispatch',
                                    text: '进入',
                                    icon: 'fa fa-hand-pointer-o',
                                    title: '进入',
                                    // visible: function (row){
                                    //     if(row.is_user_examed==0){
                                    //         return true;
                                    //     }
                                    //     return  false;
                                    // },
                                    classname: 'btn btn-info btn-xs btn-magic btn-addtabs',
                                    extend: 'data-area=\'["85%","85%"]\'',
                                    url: 'student/practice/into',
                                }
                            ],
                        }
                    ]

                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        into: function () {
            // Controller.api.bindevent();
            // Form.api.bindevent($("form[role=form]"), function(data, ret) {
            //     if(ret.code==1){
            //         Backend.api.addtabs( ret.url,'正在练习');
            //     }
            // });
            $(document).on('click', '.btn-startexam', function () {
                var index =  Layer.load(2, {
                    shade: [0.5,'#393D49'],
                    content: '正在检查模拟器并下发故障，请稍后...', // 这里是加载的文字
                });
                $(this).removeClass('btn-info');
                $(this).addClass('btn-primary');
                if(Config.is_fault){
                    $.ajax({
                        url: Config.url_type+"/sim/real-exam/student/exercise/start/"+Config.exam_id,
                        type: 'get',
                        success: function (data) {
                            console.log(data);
                            if(data.code==200){
                                window.location.href = '/admin/student/practice/examing/ids/'+Config.exam_id;
                            }else{
                                Layer.alert(data.msg);
                                Layer.close(index);
                                return;
                            }
                        }
                    });
                }else{
                    window.location.href = '/admin/student/practice/examing/ids/'+Config.exam_id;
                }
            });
        },
        examing: function () {
            $(document).ready(function() {
                var countdownTime = Config.timer; // 倒计时时间，单位为秒
                var countdownTime1 = 600; // 10分钟倒计时时间，单位为秒
                var display = $('#timer');
                var fenzhong = 0;
                var timer = setInterval(function() {
                    var minutes = parseInt(countdownTime / 60, 10);
                    var seconds = parseInt(countdownTime % 60, 10);

                    // minutes = minutes < 10 ? "0" + minutes : minutes;
                    // seconds = seconds < 10 ? "0" + seconds : seconds;

                    display.text(minutes + "分" + seconds+'秒');

                    // console.log(minutes.replace("0-",''));
                    // console.log(parseInt(minutes.replace("0-",'')));
                    if (--countdownTime < 0) {
                        $.ajax({
                            url: "student/collection/countdown/ids/"+Config.ids,
                            type: 'post',
                            data: {},
                            success: function (data) {
                                if(data.code==1){
                                    console.log(data.msg);
                                    $(".djs").html(data.msg);
                                    if(data.msg==0){
                                        $('#update-form').submit();
                                        clearInterval(timer);
                                    }
                                }
                                if(data.code==0){
                                    Layer.alert(data.msg);
                                }
                            }
                        });
                        // clearInterval(timer);
                        // $(".djs").html('已经超时，10分钟00秒后系统自动交卷。');
                        // setTimeout(function() {
                        //     var minutes1 = parseInt(countdownTime1 / 60, 10);
                        //     var seconds1 = parseInt(countdownTime1 % 60, 10);
                        //     $(".djs").html('已经超时，'+minutes1 + "分" + seconds1 +'秒后系统自动交卷。');
                        //     if (--countdownTime1>0) {
                        //         var minutes2 = parseInt(countdownTime1 / 60, 10);
                        //         var seconds2 = parseInt(countdownTime1 % 60, 10);
                        //         $(".djs").html('已经超时，'+minutes2 + "分" + seconds2 +'秒后系统自动交卷。');
                        //         console.log(minutes2+'@@@@'+seconds2);
                        //         if(minutes2==0 && seconds2==1){
                        //             $('#update-form').submit();
                        //             clearInterval(timer);
                        //         }
                        //     }
                        // }, 1000); // 第二个定时器2秒后执行
                    }
                    // if(minutes<1){
                    //     fenzhong = parseInt(minutes.replace("0-",''));
                    //     console.log(fenzhong);
                    //     if(fenzhong==7){
                    //         $('#update-form').submit();
                    //         clearInterval(timer);
                    //     }
                    // }

                }, 1000);

                //五秒刷新,同步更换件列表数据
                if(Config.isloading==1 && countdownTime>0){
                    setInterval(function() {
                        window.location.reload();
                    }, 30000);
                }

            });

            $(document).on('click', '.btn-replace', function () {
                console.log($(".other_replace").val());
                if($(".other_replace").val()=='[{"fault_id":"","request_status":"0"}]'){
                    Layer.alert('请先填写更换件');
                    return false;
                }
                $.ajax({
                    url: "student/collection/replace/ids/"+Config.ids,
                    type: 'post',
                    data: {
                        other_replace: $(".other_replace").val(),
                        other_report: $(".other_report").val(),
                    },
                    success: function (data) {
                        if(data.code==1){
                            window.location.reload();
                        }
                        if(data.code==0){
                            Layer.alert(data.msg);
                        }
                    }
                });
            });

            //追加记录
            $(document).on('click', '.btn-addlog', function () {
                var ids = Config.ids;
                var other_report_count = Config.other_report_count;
                if(other_report_count==3){
                    Layer.alert('维修报告最多允许增加三条');
                    return false;
                }
                Fast.api.open('student/collection/addreport/ids/'+ids, __("维修报告"), {area: ["95%", "85%"], maxmin: true, moveOut: false});
            });
            //编辑记录
            $(document).on('click', '.btnedit', function () {
                var ids = Config.ids;
                var xh_id = $(this).attr('xh_id');
                Fast.api.open('student/collection/editreport/ids/'+ids+'/xh_id/'+xh_id, __("维修报告"), {area: ["95%", "85%"], maxmin: true, moveOut: false});
            });
            //移除记录
            $(document).on('click', '.btnmove', function () {
                var ids = Config.ids;
                var xh_id = $(this).attr('xh_id');
                Layer.confirm(
                    __('确定要移除此条记录吗?', ids.length),
                    {icon:3, title: __('Warning'), shadeClose: true, btn: [__('OK'), __('Cancel')]},
                    function (index) {
                        var url = 'student/collection/delreport/ids/'+ids;
                        options = {url: url, data: {xh_id: xh_id}};
                        Fast.api.ajax(options, function (data, ret) {
                            window.location.reload();
                        }, function (data, ret) {
                            var error = $(that).data("error") || $.noop;
                            if (typeof error === 'function') {
                                if (false === error.call(that, data, ret)) {
                                    return false;
                                }
                            }
                        });
                        Layer.close(index);
                    }
                );

            });

            $(document).on("fa.event.appendfieldlist", "#second-fieldlist .btn-append", function (e, obj) {
                //绑定动态下拉组件
                Form.events.selectpage(obj);

            });

            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                if(ret.code==1){
                    Backend.api.addtabs( ret.url,'得分分析');
                    Backend.api.closetabs('/admin/student/practice/index');
                }
            });
        },
        analysis: function () {
            Form.api.bindevent($("form[role=form]"), function(data, ret) {
                if(ret.code==1){
                    Backend.api.addtabs( ret.url);
                    parent.window.reload();
                }
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
