define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

    var Controller = {
        index: function () {    
            //选择设备编号
            $(document).on("change", ".simNum1", function () {
                var customValue = $('.simNum1 option:selected').val();
                $.ajax({
                    url: Config.url_type+"/sim/fault/listTypeRealGZBW/"+customValue,
                    type: 'get',
                    success: function (data) {
                        if(data.code==200){
                            var task_tr = '';
                            var ret = data.data;
                            for (var i = 0; i < ret.length; i++) {
                                task_tr += '<tr>';
                                task_tr += '    <td><input type="checkbox" name="checkbox" value="'+ret[i].faultId+'"></td>';
                                task_tr += '    <td>'+ret[i].name+'</td>';
                                task_tr += '</tr>';
                            }
                            $(".task_list").html(task_tr)
                        }else{
                            Layer.alert(data.msg);
                            return;
                        }
                    }
                });
            });
            //通过simNum清除一台模拟器所有故障
            $(document).on("click", ".btn-clear-fault", function () {
                var customValue = $('.simNum1 option:selected').text();
                if(customValue=='请选择'){
                    Layer.alert('选择设备编号');
                    return;
                }
                $.ajax({
                    url: Config.url_type+"/sim/debug/debugClearAllFaultBySimNum/"+customValue,
                    type: 'get',
                    success: function (data) {
                        console.log(data.code);
                        if(data.code==200){
                            $(".content").val(data.msg);
                        }else{

                            $(".content").val(data.msg);
                        }
                        return;
                    }
                });
            });

            //清除所有在线模拟器所有故障
            $(document).on("click", ".btn-clearall-fault", function () {
                $.ajax({
                    url: Config.url_type+"/sim/debug/debugClearAllOnlineSimAllFault/",
                    type: 'get',
                    success: function (data) {
                        console.log(data);
                        if(data.code==200){
                            $(".content").val(data.msg);
                        }else{
                            $(".content").val(data.msg);
                        }
                    }
                });
            });

            //只允许选择三个故障
            var rel_Ids = [];
            $(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);
                    }
                    console.log(rel_Ids+'@@@');
                    $(this).removeAttr("checked");
                } else {
                    rel_Ids.push($(this).val());
                    console.log(rel_Ids+'###');
                    $(this).attr("checked", "true");
                }
            });

            //下发故障
            $(document).on("click", ".btn-issued-fault", function () {
                var customValue = $('.simNum1 option:selected').text();
                if(customValue=='请选择'){
                    Layer.alert('选择设备编号');
                    return;
                }
                if(rel_Ids==""){
                    Layer.alert('请选择故障');
                    return;
                }
                $.ajax({
                    url: Config.url_type+"/sim/debug/debugWriteSelectedFaultBySimNum/"+customValue+"/"+rel_Ids+"?checkReplace=false",
                    type: 'get',
                    success: function (data) {
                        console.log(data.code);
                        if(data.code==200){
                            $(".content").val(data.msg);
                        }else{
                            $(".content").val(data.msg);
                        }
                        return;
                    }
                });
                
            });

            //读取当前状态
            $(document).on("click", ".btn-read-fault", function () {
                var customValue = $('.simNum1 option:selected').text();
                if(customValue=='请选择'){
                    Layer.alert('选择设备编号');
                    return;
                }
                $.ajax({
                    url: Config.url_type+"/sim/debug/debugReadAllFaultResistanceBySimNum/"+customValue,
                    type: 'get',
                    success: function (data) {
                        console.log(data.code);
                        if(data.code==200){
                            $.ajax({
                                url: 'simulator/readfault',
                                type: 'get',
                                success: function (data) {
                                    var task_tr1 = '';
                                    if(data.code==1){
                                        var ret1 = data.data;
                                        for (var i = 0; i < ret1.length; i++) {
                                            task_tr1 += '<tr>';
                                            task_tr1 += '    <td>'+ret1[i].fault_name+'</td>';
                                            task_tr1 += '    <td>'+ret1[i].xiufu_name+'</td>';
                                            task_tr1 += '</tr>';
                                        }
                                    }else{
                                        task_tr1 += '<tr>';
                                        task_tr1 += '    <td colspan="2">暂无记录</td>';
                                        task_tr1 += '</tr>';
                                    }
                                    $(".task_list1").html(task_tr1)
                                }
                            });
                        }else{
                            Layer.alert(data.msg);
                            return;
                        }
                    }
                });
                
            });

        }
    };

    return Controller;
});
