var windowId, proName = window.location.pathname.split("/")[1],
    sysUrl = "http://" + window.location.host + "/" + proName, toEdit;
$(function () {
    $('#devicetype').combobox({
        url: 'xtclAction!query_Xtcl.do?csdl=nhywjtsblx',
        textField: "csmc",
        valueField: "csdm",
        panelWidth: 148,
        loadFilter: function (data) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].csdm == '2') {
                    data.splice(i, 1);
                    i--;
                }
            }
            return data;
        },
        onLoadSuccess: function (data) {
            $("#devicetype ").combobox('select', data[0].csdm);
        }
    });
    $("#startTime").datetimebox("setValue", $.getTodayStart(0, "00:00:00"));
    $("#endTime").datetimebox("setValue", $.getTodayEnd("23:59:59"));
    setTimeout(initDefault, 500);


});

//默认
function initDefault() {

    initGrid();
}

function onSelectT() {
    var now = new Date($("#startTime").datetimebox('getValue'));
    var sd = $('#startTime').datebox('getValue').replace(/-/g, '/'),
        ed = $('#endTime').datebox('getValue').replace(/-/g, '/');
    if (sd != '' && ed != '') {
        var totalMS = new Date(ed).getTime() - new Date(sd).getTime();//得到相差的毫秒数
        day = Math.ceil(totalMS / 1000 / 24 / 60 / 60);//得到相差天数，不满一天不算一天将Math.ceil改为Math.floor
        if (day > 7) {
            var date = new Date(now.getTime() + 7 * 24 * 3600 * 1000);
            $("#endTime").datetimebox("setValue", getdate(date));
            $.showMsg('只查询一周记录! ');
        }
    }
}

function getdate(date) {
    var month = date.getMonth() + 1;
    return date.getFullYear() + '-' + month + '-' + date.getDate()+ '-' + ' ' + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}

//查询条件
function getQueryParams() {
    onSelectT();
    var sParam = {};
    var startTime = $('#startTime').datetimebox("getValue");
    var endTime = $('#endTime').datetimebox("getValue");
    sParam.LIKEmc = $('#LIKEmc').textbox("getValue");
    //sParam.TaskMachine="0";
    $("#taskDiv").show();
    //$("#notaskDiv").hide();

    //
    // 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;
    sParam.devicetype = $('#devicetype').combobox('getValue');
    sParam.startdate = startTime;
    sParam.enddate = endTime;
    return sParam;
}

//加载列表
function initGrid() {
    //$("#taskDiv").show();
    //$("#notaskDiv").hide();
    var toolbar = [{
        text: '查询',
        iconCls: 'icon-search',
        handler: doSearch
    }];
    $('#tableGrid').datagrid({
        toolbar: toolbar,
        singleSelect: true,
        rownumbers: true,
        queryParams: getQueryParams(),
        url: 'ywvideotaskAction!listYwjtqueryTimeParam.do',
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },
            {field: 'taskid', title: '任务id', hidden: true, width: 120},
            {field: 'taskname', title: '任务名称', hidden: false, width: 130},
            {field: 'tasktype', title: '任务类型', hidden: true, width: 120},
            {field: 'tasktypename', title: '任务类型', hidden: false, width: 120},
            {field: 'starttime', title: '巡检时间', hidden: false, width: 130},
            {field: 'sbzs', title: '任务设备量', hidden: false, width: 90},
            {
                field: 'sbdrzs', title: '在线率', hidden: false, width: 250,
                formatter: function showClick(value, record, num) {
                    var tasktype = record.tasktype;
                    var sbzs = 0;
                    var onlinestotal = 0;
                    if (record.sbzs == null || record.sbzs == "" || record.sbzs == "0") {
                        return "\"0%\"";
                    } else {
                        sbzs = record.sbzs;
                    }
                    if (record.onlinestotal != null && record.onlinestotal != "") {
                        onlinestotal = record.onlinestotal;
                    }
                    var zxl = (onlinestotal / sbzs) * 100;
                    return parseFloat(zxl.toFixed(2)) + "%";
                }
            },
            {field: 'onlinestotal', title: '在线总数', hidden: true, width: 120},
            {
                field: 'runsetid', 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>';
                }
            }
        ]]
    });
    $('#tableNoTaskGrid').datagrid({
        toolbar: toolbar,
        singleSelect: true,
        rownumbers: true,
        queryParams: getQueryParams(),
        url: 'nhywjtxhdglAction!listYwxhdstatusinfoByPage.do',
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },
            {
                field: 'xh', title: 'xh', hidden: true, width: 120,
                formatter: function showClick(value, record, num) {
                    return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate(" + num + ")'>" + value + "</a>";
                }
            },
            {field: 'xhdbh', title: '编号', hidden: false, width: 120},
            {field: 'xhdmc', title: '信号灯名称', hidden: false, width: 100},
            {field: 'csmc', title: '是否启用', hidden: false, width: 80}
        ]]
    });
}

function lookdetail(index) {
    displayType = "list";
    var selRows = $('#tableGrid').datagrid('getRows')[index];
    if (!selRows) {
        $.showMsg('请先选择记录! ');
        return;
    }
    toolbar = [{
        text: '普通列表',
        iconCls: 'icon-showList',
        handler: function () {
            showList(selRows)
        }
    },
        // 	{
        // 	text:'图片列表',
        // 	iconCls:'icon-showPic',
        // 	handler:function() {showPicture(selRows)}
        // },
        {
            text: '未巡检设备清单',
            iconCls: 'icon-showList',
            handler: function () {
                showNoTaskVideo(selRows)
            }
        }, {
            text: '返回',
            iconCls: 'icon-undo',
            handler: doSearch
        }];
    showList(selRows);
}

function showList(selRows) {
    $("#searchGridDiv").hide();
    $("#searchGridListDiv").show();
    //$("#searchGridPicDiv").hide();
    //$("#searchGridvideoDiv").hide();
    displayType = "list";
    $("#searchGridList").datagrid({
        toolbar: toolbar,
        singleSelect: true,
        queryParams: getQueryParamsForDetile(selRows),
        url: 'ywvideotaskAction!listYwtaskrunresulthistoryByPage.do',
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },
            // {field:'id',title:'id',hidden:true,width:120,
            // 	formatter:function showClick(value,record,num){
            // 		return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
            // 	}
            // },
            {field: 'taskname', title: '任务名称', hidden: false, width: 120},
//			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
            {field: 'name', title: '设备名称', hidden: false, width: 120},
            {
                field: 'xh', title: '设备编号', hidden: false, width: 160,
                formatter: function (value, row, index) {
                    if (row.channelnum != 0) {
                        return row.videonum + "-" + row.channelnum;
                    } else return row.videonum;
                }
            },
            {
                field: 'state', title: '是否在线', hidden: false, width: 60,
                formatter: function showClick(value, record, num) {
                    if (record.state == '1') {
                        return "<div style='height:13px;width:13px;background-color:#60b631;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
                    } else {
                        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>";
                    }
                }
            },
            {field: 'xjsj', title: '巡检时间', hidden: false, width: 130}
        ]],
        onLoadSuccess: function (data) {
            $(".tip").tooltip({
                onShow: function () {
                    $(this).tooltip('tip').css({
                        width: '100',
                        boxShadow: '1px 1px 3px #292929'
                    });
                }
            })
        }
    });
}

function getQueryParamsForDetile(selRows) {
//	var selRows = $('#tableGrid').datagrid('getSelected');
    var sParam = {};
    sParam.startTime = selRows.starttime;
    sParam.endTime = selRows.endtime;
    sParam.devicetype = selRows.devicetype;
    sParam.taskid = selRows.taskid;
    return sParam;
}

//查询当前任务没有涉及到的设备清单
function showNoTaskVideo(selRows) {
    $("#searchGridDiv").hide();
    $("#searchGridListDiv").hide();
    //$("#searchGridPicDiv").hide();
    $("#searchGridvideoDiv").show();

    displayType = "video";
    $('#searchGridvideo').datagrid({
        toolbar: toolbar,
        queryParams: getQueryParamsForDetile(selRows),
        url: 'ywvideotaskAction!listYwDevicetaskNoTaskByPage.do',
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },
            {
                title: "设备编号",
                field: "sbbh",
                width: 160,
            },
            {
                title: "通道编号",
                field: "tdbh",
                width: 80
            },
            {
                title: "通道名称",
                field: "tdmc",
                width: 200
            },
            {
                title: "行政目录",
                field: "xzmc",
                width: 120
            },

        ]],
        rownumbers: true,
    });
}

//查询
function doSearch() {
    var displayType = "VideotaskrunresultList";
    $("#searchGridDiv").show();
    $("#searchGridListDiv").hide();
    $("#searchGridPicDiv").hide();
    $('#tableGrid').datagrid('load', getQueryParams());
}

