var height = $(window).height() - 70;
$(function () {
    $(".halfHeight").css("height", height);
    refresh();
    getDeviceType()


});

window.refreshs = function () {
    refresh()
}
//新增版本
$("#addVersion").on("click", function () {

        let username = $('#username').val()
        let resetName = '新增升级版本'
        passwordLocalIndentity(username,resetName,function () {
            layer.open({
                type: 2,
                title: '新增版本',
                maxmin: false,
                area: ['300px', '460px'],
                content: 'versionadd?type=1',
                scrollbar: false,
                end: function () {

                }
            })
        })
});

//系列过滤
$("#query").on("click", function () {
    refresh()
});




/**
 * 初始化获取后台数据
 */
function refresh() {
    let dev = $('#devType').val()
    /*获取各项条件 */
    var indexi;
     $.ajax({
         type: "get",
         url: "/pqs9900/version/getVersionList",
         dataType: "json",
         data: {devType:dev},
         beforeSend: function () {
             indexi = ityzl_SHOW_LOAD_LAYER();
         },
         success: function (data) {
             if (data.code === 200) {
                 //初始化表格
                 initTransactionTable(data.body);
             } else {
                 layer.msg(data.message, {icon: 2, time: 2000})
                 initTransactionTable(null);
             }
             ityzl_CLOSE_LOAD_LAYER(indexi);
         }
     })
}


var dataTable;

//初始化表格数据
function initTransactionTable(tableData) {
    var tableHeight = height - 75;
    var length = Math.floor(tableHeight / 32);
    if (length < 1) {
        length = 1;
    }
    var tableCellHeight = 32 * length;
    dataTable = $("#deviceTableV").DataTable();
    dataTable.clear().draw();
    dataTable = $('#deviceTableV').DataTable({
        order: [6, 'desc'],
        columnDefs: [
            {
                orderable: false,
                targets: [0,6,10]
            }
        ],
        columns: [
            {width: "4%",data: null,"targets": 0},
            {width: "9%", data: "devType"},
            {width: "7%", data: "edName"},
            {width: "7%", data: "edProtocol"},
            {width: "9%", data: "edDate",render:function (data) {
                   return  formatTime(data,2)
                }},
            {width: "8%", data: "updateUser"},
            {width: "10%", data: "updateTime",render:function (data) {
                    return formatTime(data,1)
                }},
            {width: "12%", data: "edDescribe"},
            {width: "9%", data: "type",render:function (data) {
                    if(data ==null){
                        return '通用'
                    }else {
                        return data
                    }
                }},
            {width: "6%",data:null,render:function (data) {
                console.log(data)
                    if(data.status==0){
                        return '<input value="'+data.edIndex+'" type="checkbox" class="checkbox"/>'
                    }else {
                        return '<input value="'+data.edIndex+'" type="checkbox" checked class="checkbox"/>'
                    }

                }},
            {
                width: "7%", data: "edIndex", render: function (data, type, full, meta) {
                    return "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);'  onclick='excuteEvent(\"" + data + "\",2)' >修改</button>" +
                        "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);'  onclick='excuteEvent(\"" + data + "\",1)' >删除</button>";
                }
            }
        ],
        destroy: true,
        data: tableData,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: tableCellHeight,
        autoWidth:true,
        lengthMenu: [length],
        language: {
            url: '/pqs9900/json/chine.json'
        },
        fnDrawCallback: function (oTable) {
            //初始化按钮开关
            $(".checkbox").bootstrapSwitch({
                onText: "启用",      // 设置ON文本
                offText: "关闭",    // 设置OFF文本
                onColor: "primary",// 设置ON文本颜色     (info/success/warning/danger/primary)
                offColor: "primary",  // 设置OFF文本颜色        (info/success/warning/danger/primary)
                size: "mini",    // 设置控件大小,从小到大  (mini/small/normal/large)
                handleWidth: "30",//设置控件宽度
                // 当开关状态改变时触发
                onSwitchChange: function (event, state) {
                    let tid = $(this).val()
                    $.ajax({
                        type: 'POST',
                        dataType: "json",
                        url: '/pqs9900/version/enableDisable',
                        data: {edIndex: tid, status: state?1:0},
                        success: function (data) {
                            if (data.code == 200) {
                                refresh()
                                layer.msg(data.message, {icon: 1,time:2000})
                            }else {
                                refresh()
                                layer.msg(data.message, {icon: 2,time:2000})
                            }
                        },
                        error: function () {
                            layer.msg('未知错误', {icon: 2})
                        }
                    });
                }
            });
            var api = this.api();
            api.column(0).nodes().each(function(cell, i) {
                cell.innerHTML =  i + 1;
            });

            $("#deviceTableV_info").addClass("fl");
            $("#deviceTableV_paginate").addClass("fr");
            $("#deviceTableV_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
            var table = $("#deviceTableV").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });
}

//搜索表中的数据
function searchResult(value) {
    dataTable.search(value).draw();
}

//加载所有装置系列
function getDeviceType() {
    $.ajax({
        type: 'post',
        url: '/pqs9900/device/getselect',
        data: {dictypeName: '装置系列'},
        dataType: 'json',
        success: function (data) {
            $('#devType').empty()
            var html = '<option value="">全部</option>'
            data.forEach(function (item) {
                let op = '<option value="' + item.dicIndex + '">' + item.dicName + '</option>'
                html += op;
            })
            $('#devType').append(html)
        }

    })
}

//获取启用状态
function getWarnInfo(data) {
    data = Number(data);
    if (data === 1) {
        info = '<font style="color:#A52a2a">中断</font>'
    }else {
        info = '<font style="color:black">正常</font>'
    }
    return info;
}

//删除修改按钮操作
function excuteEvent(id, type) {
    let username = $('#username').val()
    let resetName = ''
    passwordLocalIndentity(username,resetName,function () {
    if(type == 1){
        //删除
        layer.confirm('确认删除吗?',function (index) {
            $.ajax({
                type : "get",
                url : "/pqs9900/version/delVersion",
                dataType : "json",
                data:{
                    edIndex:id
                },
                success : function(data) {
                    if(data.code === 200){
                        layer.msg(data.message,{icon:1},function (){
                            refresh()
                        })

                    }else {
                        layer.msg(data.message,{icon:2})
                    }
                }
            });
            layer.close(index);
        })
    }else {
            //修改
            layer.open({
                type: 2,
                title: '修改终端版本',
                maxmin: false,
                area: ['300px', '460px'],
                content: 'versionadd?type=2&edIndex=' + id,
                scrollbar: false,
                end: function () {
                    // refresh();
                }
            })
    }
    })
}




