var windowId, proName = window.location.pathname.split("/")[1],
    sysUrl = "http://" + window.location.host + "/" + proName, toEdit;
var sParam = {};
$(function () {
    initDefault();
});

//默认
function initDefault() {
    $("#startTime").datetimebox("setValue", $.getTodayStart(0, "00:00:00"));
    $("#endTime").datetimebox("setValue", $.getTodayEnd("23:59:59"));
    initGrid();
}

//查询条件
function getQueryParams() {
    var startTime = $('#startTime').datetimebox("getValue");
    var endTime = $('#endTime').datetimebox("getValue");
    sParam.LIKEmachinename = $('#LIKEmachinename').textbox("getValue");
    sParam.TaskMachine = $('#TaskMachine').combobox("getValue");
    var tableparam = "";
    if (!(startTime == null || startTime == "" || startTime == undefined)) {
        tableparam += " AND statusuptime > '" + startTime + "'";
    }
    if (!(endTime == null || endTime == "" || endTime == undefined)) {
        tableparam += " AND statusuptime < '" + endTime + "'";
    }
    sParam.tableparam = tableparam;
    return sParam;
}

//加载列表
function initGrid() {
    var toolbar = [{
        text: '查询',
        iconCls: 'icon-search',
        handler: doSearch
    }];
    $('#tableGrid').datagrid({
        toolbar: toolbar,
        singleSelect: true,
        rownumbers: true,
        queryParams: getQueryParams(),
        url: 'ywmachinestatusinfoAction!listYwmachinestatusinfoByPage.do',
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },
            {field: 'machineid', title: 'id', hidden: true, width: 120},
            {
                field: 'machinestatusid', title: 'machinestatusid', hidden: true, width: 120,
                formatter: function showClick(value, record, num) {
                    return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate(" + num + ")'>" + value + "</a>";
                }
            },
            {field: 'machinename', title: '机器名称', hidden: false, width: 120},
            {field: 'machineip', title: '设备ip', hidden: false, width: 110},
            {
                field: 'zjzt', title: '主机状态', hidden: true, width: 60,
                formatter: function showClick(value, record, num) {
                    //				console.log("主机：",value,record);
                    if (value == 1) {
                        return "<div style='height:13px;width:13px;background-color:green;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
                    } else if (value == 0) {
                        return "<div style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
                    } else {
                        return value;
                    }
                }
            },
            {
                field: 'cpuzt', title: 'cpu使用率', hidden: false, width: 120,
                formatter: function (value, record, num) {
                    var cpu = record.cpusy;
                    if (cpu != null) {
                        cpu = cpu.indexOf('%') > 0 ? cpu.replace("%", "") : cpu;
                        if (value == 0) {
                            return '<div class="progress"><div class="progress-value"  style="background-color:red;width: ' + (cpu || 0) + 'px;"><div class="progress-text" style="border:1px solid red;">' + (cpu + "%" || "") + "</div></div></div>"
                        } else {
                            return $.gridProgressBar(cpu + "%", 100 * cpu / 100.00);
                        }
                    } else {
                        return "未知";
                    }

                }
            },
            {
                field: 'nczt', title: '内存使用率', hidden: false, width: 225,
                formatter: function (value, record, num) {
                    var nc = record.ncsy;
                    var ncrl = Math.round(record.ncrl);
                    var sync = "";
                    if (nc != null) {
                        nc = Math.round(nc.indexOf('%') > 0 ? nc.replace("%", "") : nc);
                        sync = (ncrl * nc) / 100;
                        sync = sync.toFixed(2);
                        if (value == 0) {
                            return '<div class="progress">' +
                                '<div class="progress-value"  style="background-color:red;width: ' + (nc || 0) + 'px;">' +
                                '<div class="progress-text progress-textRed"  style="border:1px solid red;">' + (nc + "%" || "") + "</div>" +
                                "</div>" +
                                "<div style='position: relative;left: 105px; top: -18px;'>" + parseFloat(sync) + "G / " + parseFloat(ncrl) + "G(总)</div>" +
                                "</div>";
                        } else {
                            return '<div class="progress">' +
                                '<div class="progress-value"  style="width: ' + (nc || 0) + 'px;">' +
                                '<div class="progress-text">' + (nc + "%" || "") + "</div>" +
                                "</div>" +
                                "<div style='position: relative;left: 105px; top: -18px;'>" + parseFloat(sync) + "G / " + parseFloat(ncrl) + "G(总)</div>" +
                                "</div>";

                        }
                        //	return $.gridProgressBar(nc+"%",100*nc/100.00);
                    } else {
                        return "未知";
                    }

                }
            },
            {
                field: 'ypzt', title: '硬盘使用率', hidden: false, width: 250,
                formatter: function (value, record, num) {
                    var yp = record.ypsy;
                    var yprl = Math.round(record.yprl);
                    var ypsy = "";
                    if (yp != null) {
                        yp = Math.round(yp.indexOf('%') > 0 ? yp.replace("%", "") : yp);
                        ypsy = (yprl * yp) / 100;
                        ypsy = ypsy.toFixed(2);
                        if (value == 0) {
                            return '<div class="progress">' +
                                '<div class="progress-value"  style="background-color:red;width: ' + (yp || 0) + 'px;">' +
                                '<div class="progress-text progress-textRed"  style="border:1px solid red;">' + (yp + "%" || "") + "</div>" +
                                "</div>" +
                                "<div style='position: relative;left: 105px; top: -18px;'>" + parseFloat(ypsy) + "G / " + parseFloat(yprl) + "G(总)</div>" +
                                "</div>";
                        } else {
                            return '<div class="progress">' +
                                '<div class="progress-value"  style="width: ' + (yp || 0) + 'px;">' +
                                '<div class="progress-text">' + (yp + "%" || "") + "</div>" +
                                "</div>" +
                                "<div style='position: relative;left: 105px; top: -18px;'>" + parseFloat(ypsy) + "G / " + parseFloat(yprl) + "G(总)</div>" +
                                "</div>";
                        }
                    } else {
                        return "未知";
                    }
                }
            },
            {field: 'statusuptime', title: '检测时间', hidden: false, width: 125},
            {field: 'csmc', title: '是否启用', hidden: true, width: 125},
            {
                field: 'xxx', title: '操作', hidden: false, width: 70,
                formatter: function showClick(value, record, num) {
                    return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px 5px;" class="l-btn l-btn-small" onclick="lookdetail(' + num + ')"  group="" id=""><span class="l-btn-left"><span class="l-btn-text">详情</span></span></a>';
                }
            }
        ]]
    });
}

function lookdetail(index) {
    var selRows = $('#tableGrid').datagrid('getRows')[index];
    windowId = top.$.createWin({
        title: '查看详情',
        width: 800,
        height: 500,
        data: selRows,
        url: sysUrl + "/yw/patroltask/machinetaskhistory/ywmachinestatusinfo_show.jsp"
    });
}

//查询
function doSearch() {
    $('#tableGrid').datagrid('load', getQueryParams());
    //控制列显示隐藏
    if (sParam.TaskMachine == "0") {
        $('#tableGrid').datagrid('hideColumn', 'zjzt');
        $('#tableGrid').datagrid('hideColumn', 'cpuzt');
        $('#tableGrid').datagrid('hideColumn', 'ypzt');
        $('#tableGrid').datagrid('hideColumn', 'statusuptime');
        $('#tableGrid').datagrid('hideColumn', 'xxx');
        $('#tableGrid').datagrid('showColumn', 'csmc');
    } else {
        $('#tableGrid').datagrid('showColumn', 'zjzt');
        $('#tableGrid').datagrid('showColumn', 'cpuzt');
        $('#tableGrid').datagrid('showColumn', 'ypzt');
        $('#tableGrid').datagrid('showColumn', 'statusuptime');
        $('#tableGrid').datagrid('showColumn', 'xxx');
        $('#tableGrid').datagrid('hideColumn', 'csmc');
    }
}
