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

    var Controller = {
        index: function () {
            $('.btn-edit').data('area',['55%','95%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher/exams/index' + location.search,
                    add_url: 'teacher/exams/add',
                    edit_url: 'teacher/exams/edit',
                    del_url: 'teacher/exams/del',
                    multi_url: 'teacher/exams/multi',
                    import_url: 'teacher/exams/import',
                    table: 'teacher_exams',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置顶部的企业总数
                $(".pingfen_total").text(data.defen.pingfen);
                $(".max_total").text(data.defen.max);
                $(".min_total").text(data.defen.min);

                var task_tr = '';
                console.log(data.tongji.one_static.length+'###')
                for (i=0;i<data.tongji.one_static.length;++i){
                    var one_data = data.tongji.one_static[i];
                    console.log(one_data+[i]);
                    var epercent = '';
                    if(one_data.epercent!='undefined' && one_data.epercent != undefined){
                        epercent = one_data.epercent;
                    }
                    task_tr += '<tr style="text-align: center">';
                    task_tr += '    <td>'+one_data.name+'</td>';
                    task_tr += '    <td>'+epercent+'</td>';
                    task_tr += '</tr>';
                    $(".tbodyone").html(task_tr);
                }
                var task_tr = '';
                for (j=0;j<data.tongji.two_static.length;++j){
                    var two_data = data.tongji.two_static[j];
                    var epercent = '';
                    if(two_data.epercent!='undefined' && two_data.epercent != undefined){
                        epercent = two_data.epercent;
                    }
                    task_tr += '<tr style="text-align: center">';
                    task_tr += '    <td>'+two_data.name+'</td>';
                    task_tr += '    <td>'+epercent+'</td>';
                    task_tr += '</tr>';
                    $(".tbodytwo").html(task_tr);
                }
                var task_tr = '';
                for (k=0;k<data.tongji.three_static.length;++k){
                    var three_data = data.tongji.three_static[k];
                    var epercent = '';
                    if(three_data.epercent!='undefined' && three_data.epercent != undefined){
                        epercent = three_data.epercent;
                    }
                    task_tr += '<tr style="text-align: center">';
                    task_tr += '    <td>'+three_data.name+'</td>';
                    task_tr += '    <td>'+epercent+'</td>';
                    task_tr += '</tr>';
                    $(".tbodythree").html(task_tr);
                }
            });


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'exam_id',
                sortName: 'exam_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                singleSelect : true, //
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_username', title: __('学号'),operate: 'like'},
                        {field: 'user_nickname', title: __('姓名'),operate: 'like'},
                        // {field: 'exam_status', title: __('Exam_status'),searchList: {"0":"未登录","1":"已登录","2":"模拟器检查并下发故障中","3":"模拟器正常","4":"答题中","5":"已交卷","6":"计算成绩中","7":"获取到成绩报告","80":"教师标记缺考","81":"登录未开始答题","90":"模拟器异常结束"},formatter: Table.api.formatter.simexamstatus},
                        {field: 'exam_collection_name', title: __('考试名称'),operate: 'like'},
                        {field: 'sim_type', title: __('Sim_type'),searchList:{"0001":"FZD04B型侦毒器","0002":"FZB006型毒剂报警器","0003":"防化兵用毒剂报警器"},formatter: Table.api.formatter.simtype},
                        // {
                        //     field: 'user_depart_id',
                        //     title: __('Department'),
                        //     visible: false,
                        //     addclass: 'selectpage',
                        //     extend: 'data-source="department/index/index" data-field="name"',
                        //     operate: Config.groupIds !=8 ? 'in':false,
                        //     formatter: Table.api.formatter.search
                        // },
                        {field: 'total_score', title: __('Total_score'),operate:false},
                        // {field: 'deduction_total_score', title: __('Deduction_total_score')},
                        {field: 'starttime', title: __('考试时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'endtime', title: __('End_time'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
                        // {field: 'login_time', title: __('Login_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'logout_time', title: __('Logout_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {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
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'dispatch',
                                    text: '编辑',
                                    icon: 'fa fa-pencil',
                                    title: '编辑',
                                    classname: 'btn btn-success btn-xs btn-magic btn-dialog',
                                    extend: 'data-area=\'["55%","95%"]\'',
                                    url: 'teacher/exams/edit',
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click', '.btn-view', function () {
                var ids = Table.api.selectedids(table);
                var title = '成绩详情';
                Fast.api.open(Fast.api.fixurl("teacher/exams/view/ids/"+ids), title, { area: ["60%", "95%"] });
            });
        },
        persent: function () {
            Table.api.init({
                extend: {
                    index_url: 'teacher/exams/persent/ids/'+Config.ids + location.search,
                    edit_url: 'teacher/exams/editscore',
                    indexed_url: 'teacher/exams/examing/ids/'+Config.ids + location.search,
                    score_url: 'teacher/exams/score/ids/'+Config.ids  + location.search,
                    table: 'teacher_exams',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'rel_id',
                sortName: 'rel_id',
                toolbar: '#toolbar',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:false,
                showExport:false,
                search:false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'fault_name', title: __('更换件')},
                        {field: 'create_by', title: __('学员姓名')},
                        {field: 'request_status', title: __('状态'),
                            formatter: function(value, row,index) {
                                if(value==1){
                                    return '<span class="text-warning">待处理</span>';
                                }else if(value==2){
                                    return '<span class="text-success">已送</span>';
                                }else if(value==3){
                                    return '<span class="text-danger">已驳回</span>';
                                }
                            }
                        },
                        {field: 'createtime', title: __('申请时间'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
                        {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-check',
                                    title: '已送',
                                    classname: 'btn btn-success btn-xs btn-magic btn-ajax',
                                    url: 'teacher/exams/handle/type/1',
                                    visible: function (rr) {
                                        if(rr.request_status == 1 || rr.request_status == 0){
                                            return true;
                                        }
                                        return  false;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable("refresh");
                                    },
                                },{
                                    name: 'dispatch',
                                    text: '驳回',
                                    icon: 'fa fa-reply-all',
                                    title: '驳回',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: 'teacher/exams/handle/type/2',
                                    visible: function (rr) {
                                        if(rr.request_status == 1 || rr.request_status == 0){
                                            return true;
                                        }
                                        return  false;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable("refresh");
                                    },
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);



            var table1 = $("#table1");
            // 初始化表格
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.indexed_url,
                pk: 'exam_id',
                sortName: 'exam_id',
                toolbar: '#toolbar1',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:false,
                showExport:false,
                search:false,
                pagination:false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'seat_id', title: __('座号')},
                        {field: 'sim_state', title: __('模拟器状态'),
                            formatter: function(value, row,index) {
                                return '<span class="text-success">'+value+'</span>';
                            }
                        },
                        {field: 'user_username', title: __('学员学号')},
                        {field: 'user_nickname', title: __('学员姓名')},
                        {field: 'exam_status', title: __('考试状态'),
                            formatter: function(value, row,index) {
                                return '<span class="text-success">'+value+'</span>';
                            }
                        },
                        {field: 'total_score', title: __('考试成绩')},
                        {field: 'fault_names', title: __('出题题目'),formatter: Table.api.formatter.faultname},
                        // {field: 'xianxiang_names', title: __('故障现象'),formatter: Table.api.formatter.answerName},
                        // {field: 'yuanyin_names', title: __('可能原因'),formatter: Table.api.formatter.answerName},
                        // {field: 'buwei_names', title: __('故障部位'),formatter: Table.api.formatter.answerName},
                        // {field: 'fangfa_names', title: __('排除方法'),formatter: Table.api.formatter.answerName},
                        {field: 'operate', title: __('Operate'), table: table1,
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'dispatch',
                                    text: '成绩详情',
                                    icon: 'fa fa-eye',
                                    title: '成绩详情',
                                    classname: 'btn btn-info btn-xs btn-magic btn-dialog',
                                    extend: 'data-area=\'["55%","95%"]\'',
                                    visible: function (row) {
                                        if(row.total_score>0){
                                            return true;
                                        }
                                        return false;
                                    },
                                    url: 'teacher/exams/view',
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);



            var table2 = $("#table2");
            // 初始化表格
            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.score_url,
                pk: 'exam_id',
                sortName: 'exam_id',
                toolbar: '#toolbar2',
                fixedColumns: true,
                pageSize: 10,
                pagination:false,
                fixedLeftNumber: 4,
                searchFormVisible:false,
                showExport:false,
                search:false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'seat_id', title: __('座号')},
                        {field: 'user_username', title: __('学员学号')},
                        {field: 'user_nickname', title: __('学员姓名')},
                        {field: 'total', title: __('考试成绩')},
                        {field: 'fault_one_score', title: __('故障一扣分'),editable:true},
                        {field: 'fault_two_score', title: __('故障二扣分'),editable:true},
                        {field: 'fault_three_score', title: __('故障三扣分'),editable:true},
                        {field: 'xianxian_score', title: __('故障现象扣分'),editable:true},
                        {field: 'yuanyin_socre', title: __('可能原因扣分'),editable:true},
                        {field: 'buwei_score', title: __('故障部位扣分'),editable:true},
                        {field: 'fangfa_score', title: __('排除方法扣分'),editable:true},
                        {field: 'overtime_score', title: __('超时扣分'),editable:true},
                        {field: 'operate', title: __('Operate'), table: table1,
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'dispatch',
                                    text: '成绩详情',
                                    icon: 'fa fa-eye',
                                    title: '成绩详情',
                                    classname: 'btn btn-info btn-xs btn-magic btn-dialog',
                                    extend: 'data-area=\'["55%","95%"]\'',
                                    url: 'teacher/exams/view',
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table2);

            //关闭考试 并且
            $(document).on('click', '.btn-closes', function () {
                var ids  = Config.ids;
                $.ajax({
                    type:'post',
                    url:'teacher/collection/multi',
                    data:{ids:ids,params:'exam_collection_state=3'},  //重点必须为一个变量如：data
                    success:function(data){
                        if(data.code==1){
                            Backend.api.closetabs('/admin/teacher/exams/persent/ids/'+Config.ids)
                            // Backend.api.addtabs('teacher/collection','考试列表');
                        }
                    },
                    error:function(){
                        layer.alert("操作失败");
                        return;
                    }
                })
            });

            $("button[name=commonSearch]").css('display','none');

            //定时刷新
            setInterval(function() {
                table.bootstrapTable('refresh');
                table1.bootstrapTable('refresh');
                table2.bootstrapTable('refresh');
            }, 30000);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
