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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'task_id',
                sortName: 'task_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'task_id', title: __('Task_id'),operate: false,width:'80px'},
                        {field: 'name', title: __('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: 'fault_name', title: __('具体故障清单'),formatter: Table.api.formatter.faultname, operate: 'LIKE'},
                        {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=\'["75%","75%"]\'',
                                    url: 'teacher/task/edit',
                                },{
                                    name: 'dispatch',
                                    text: '删除',
                                    icon: 'fa fa-trash',
                                    title: '删除',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: 'teacher/task/del',
                                    confirm: '确定要删除吗？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                var rel_Ids = [];
                var rel_Names = [];
                var prel_Ids = [];
                if(Config.rel_Ids){
                    $.each(Config.rel_Ids, function(index, value) {
                        rel_Ids.push(value);
                    });
                    $.each(Config.rel_Names, function(index, value) {
                        rel_Names.push(value);
                    });
                    $.each(Config.prel_Ids, function(index, value) {
                        prel_Ids.push(value);
                    });
                }
                $('select[name="row[sim_type]"]').change(function () {
                    $(".fault_ids").val('');
                    $(".fault_names").val('');
                    $(".pfault_ids").val('');
                    rel_Ids = [];
                    rel_Names = [];
                    prel_Ids = [];
                    console.log($(this).val());
                    console.log( $(this).find("option:selected").text())
                    var xinghao = $(this).find("option:selected").text();
                    $.ajax({
                        url: "fault/getdatalist",
                        type: 'post',
                        data: {
                            simtype: $(this).val(),
                        },
                        success: function (data) {
                            if(data.code==1){
                                var task_tr = '';
                                var ret = data.data;
                                for (var i = 0; i < ret.length; i++) {
                                    if(ret[0].sim_type == '0001'){
                                        task_tr += '<tr>';
                                        if(i==0){
                                            task_tr += '    <td rowspan="11">'+xinghao+'</td>';
                                        }
                                        if(i<4){
                                            task_tr += '    <td rowspan="2">'+ret[i].name+'</td>';
                                        }else{
                                            task_tr += '    <td rowspan="1">'+ret[i].name+'</td>';
                                        }
                                        for (var j = 0; j < ret[i].children.length; j++) {
                                            var ret1 = ret[i].children;
                                            if(j>0){
                                                task_tr += '    </tr><tr><td>'+ret1[j].name+'</td>';
                                                task_tr += '    <td><input type="checkbox" name="checkbox" value="'+ret1[j].fault_id+'" vname="'+ret1[j].name+'" pvalue="'+ret[i].fault_id+'"></td>';
                                            }else{
                                                task_tr += '    <td>'+ret1[j].name+'</td>';
                                                task_tr += '    <td><input type="checkbox" name="checkbox" value="'+ret1[j].fault_id+'" vname="'+ret1[j].name+'" pvalue="'+ret[i].fault_id+'"></td>';
                                            }
                                        }
                                        task_tr += '</tr>';
                                    }else if(ret[0].sim_type == '0002'){
                                        task_tr += '<tr>';
                                        if(i==0){
                                            task_tr += '    <td rowspan="15">'+xinghao+'</td>';
                                        }
                                        if(i<5){
                                            task_tr += '    <td rowspan="2">'+ret[i].name+'</td>';
                                        }else{
                                            task_tr += '    <td rowspan="1">'+ret[i].name+'</td>';
                                        }
                                        for (var j = 0; j < ret[i].children.length; j++) {
                                            var ret1 = ret[i].children;
                                            if(j>0){
                                                task_tr += '    </tr><tr><td>'+ret1[j].name+'</td>';
                                                task_tr += '    <td><input type="checkbox" name="checkbox" value="'+ret1[j].fault_id+'" vname="'+ret1[j].name+'" pvalue="'+ret[i].fault_id+'"></td>';
                                            }else{
                                                task_tr += '    <td>'+ret1[j].name+'</td>';
                                                task_tr += '    <td><input type="checkbox"  name="checkbox" value="'+ret1[j].fault_id+'" vname="'+ret1[j].name+'" pvalue="'+ret[i].fault_id+'"></td>';
                                            }
                                        }
                                    }else if(ret[0].sim_type == '0003'){
                                        task_tr += '<tr>';
                                        if(i==0){
                                            task_tr += '    <td rowspan="15">'+xinghao+'</td>';
                                        }
                                        if(i<4){
                                            task_tr += '    <td rowspan="2">'+ret[i].name+'</td>';
                                        }else if(i==4){
                                            task_tr += '    <td rowspan="3">'+ret[i].name+'</td>';
                                        }else if(i==5){
                                            task_tr += '    <td rowspan="2">'+ret[i].name+'</td>';
                                        }else{
                                            task_tr += '    <td rowspan="1">'+ret[i].name+'</td>';
                                        }
                                        for (var j = 0; j < ret[i].children.length; j++) {
                                            var ret1 = ret[i].children;
                                            if(j>0){
                                                task_tr += '    </tr><tr><td>'+ret1[j].name+'</td>';
                                                task_tr += '    <td><input type="checkbox" name="checkbox" value="'+ret1[j].fault_id+'" vname="'+ret1[j].name+'" pvalue="'+ret[i].fault_id+'"></td>';
                                            }else{
                                                task_tr += '    <td>'+ret1[j].name+'</td>';
                                                task_tr += '    <td><input type="checkbox"  name="checkbox" value="'+ret1[j].fault_id+'" vname="'+ret1[j].name+'" pvalue="'+ret[i].fault_id+'"></td>';
                                            }
                                        }
                                    }
                                }
                                $(".task_list").html(task_tr);
                            }
                        }
                    });
                });

                //只允许选择三个故障
                $(document).on('click', "input[type='checkbox']", function () {
                    if ($(this).attr("checked")) {
                        var index = rel_Ids.indexOf($(this).val());
                        if (index > -1) {
                            rel_Ids.splice(index, 1);
                            rel_Names.splice(rel_Names.indexOf($(this).attr('vname')), 1);
                            prel_Ids.splice(prel_Ids.indexOf($(this).attr('pvalue')), 1);
                        }
                        $(".fault_ids").val(rel_Ids.join(','));
                        $(".fault_names").val(rel_Names.join(','));
                        $(".pfault_ids").val(prel_Ids.join(','))
                        $(this).removeAttr("checked");
                    } else {
                        if(rel_Ids.length>2){
                            Layer.alert('故障内容只允许选择3个');
                            return false
                        }
                        var pfault_id = $(this).attr('pvalue');
                        var index2 = prel_Ids.indexOf(pfault_id);
                        if (index2 > -1) {
                            Layer.alert('一个故障现象下只允许申请一个故障部位');
                            return false
                        }
                        prel_Ids.push(pfault_id);
                        $(".pfault_ids").val(prel_Ids.join())
                        $(this).attr("checked", "true");
                        rel_Ids.push($(this).val());
                        $(".fault_ids").val(rel_Ids.join())
                        rel_Names.push($(this).attr('vname'));
                        $(".fault_names").val(rel_Names.join(','));
                   }
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
