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

//默认
function initDefault() {
    ywXhd();
    initGrid();
}

function ywXhd() {
    $.ajax({
        type: 'post',
        async: true,
        dataType: 'json',
        url: 'xtclAction!listXtclByPage.do',//dac 行政树
//		 url:'ptxzsAction!query_Ptxzs.do' ,//本地 行政树
        data: {
            LIKEcsdl: "nhywjtxhdglCurrent",
            LIKEcslm: "",
            LIKEcsdm: "",
            LIKEcsmc: "",
            order: "order by csdl asc",
            page: 1,
            rows: 20,
        },
    }).done(function (data) {
        var arr = [];
        if (data.rows.length > 0) {
            for (var i in data.rows) {
                arr.push({name: data.rows[i].csmc, value: data.rows[i].csdm});
            }
        }
        arrs = arr;
        initCacheStatus();
        // var a =data;
    });
}

//初始化下拉框
function initCacheStatus() {
    var ywDeviceStatusArr = [];
    // var videoStatus = Constants.videoStatus;
    var videoStatus = arrs;
    for (var i in videoStatus) {
        ywDeviceStatusArr.push({"id": videoStatus[i].value, "text": videoStatus[i].name})
    }
    $("#currentstate").combobox("loadData", ywDeviceStatusArr);
    $('#currentstate').combobox({
        formatter: function (row) {
            var opts = $(this).combobox('options');
            return row[opts.textField];
        }
    });
    var firstShowPanel = true;
    var ht = [];
    for (var i = 0; i < arrs.length; i++) {
        var str = "#_easyui_combobox_i1_" + i;
        ht.push({id: str});
    }
    for (var i = 0; i < ht.length; i++) {
        $(ht[i].id).after("<div onclick='handleA' class='handleCom1' style='display:none;'>" + $(ht[i].id).html() + "</div>");
    }
    $('#currentstate').combobox({
        onUnselect: function (record) {
            var opts = $(this).combobox('options');
            var values = $('#currentstate').combobox('getValues');
            if (record.id == 1) {
                for (var i = 0; i < arrs.length; i++) {
                    $("#_easyui_combobox_i1_" + i).show();
                }
                $(".handleCom").hide();
            } else {
                if (values.length <= 1) {
                    var values = $('#currentstate').combobox('getValues');
                    $(".handleCom1").hide();
                    $("#_easyui_combobox_i1_0").show();
                }
            }
        },
        onChange: function () {
            var values = $('#currentstate').combobox('getValues');
        },
        onHidePanel: function () {
            selectedValues = $('#currentstate').combobox('getValues');
            firstShowPanel = false;
        },
        onShowPanel: function () {
            if (!firstShowPanel) {
                if (selectedValues.indexOf("1") != -1) {
                    for (var i = 0; i < arrs.length; i++) {
                        $("#_easyui_combobox_i1_" + i).hide();
                    }
                } else {
                    if (selectedValues.length > 0) {
                        $("#_easyui_combobox_i1_0").hide();
                    }
                }
            }
        },
        onSelect: function (record) {
            var values = $('#currentstate').combobox('getValues');
            var opts = $(this).combobox('options');
            if (record.id == 1) {
                for (var i = 0; i < arrs.length; i++) {
                    $("#_easyui_combobox_i1_" + i).hide();
                }
                $(".handleCom").show();
            } else {
                $("#_easyui_combobox_i1_0").hide();
                $(".handleCom1").hide();
            }
            return false;
        }
    });
}

//查询条件
function getQueryParams() {
    var sParam = {};
//	var startTime = $('#startTime').datetimebox("getValue");	
//	var endTime = $('#endTime').datetimebox("getValue");

    sParam.LIKExhdmc = $('#LIKEledmc').textbox("getValue");
    sParam.LIKElkmc = $('#LIKElkmc').textbox("getValue");
    sParam.LIKEqymc = $('#LIKEqymc').textbox("getValue");
    sParam.LIKEbz = $('#bz').textbox("getValue");
    sParam.emptyremarks = $('#combobz').combobox("getValue");
//	sParam.startTime = startTime;
//	sParam.endTime = endTime;
//	sParam.order = " order by createtime desc"
    var chosed = $('#currentstate').combobox('getValues');
    var currentstate = "";
    for (var i in chosed) {
        if (chosed[i] == "0") {
            currentstate += " or STATE is null or STATE=0";
        } else {
            currentstate += " or STATE= " + chosed[i];
        }
    }
    if (currentstate == "") {
        sParam.tableparam = "";
    } else {
        if (currentstate != "") {
            sParam.tableparam = " and (" + currentstate.substr(3) + ") ";
        }
    }
    return sParam;
}

//加载列表
function initGrid() {
    var toolbar = [{
        text: '查询',
        iconCls: 'icon-search',
        handler: doSearch
    }];
    $('#tableGrid').datagrid({
        toolbar: toolbar,
        singleSelect: true,
        rownumbers: true,
        queryParams: getQueryParams(),
        url: 'nhywjtxhdglAction!listNhywjtxhdglStatusByPage.do',
        columns: [[
            /* {
                 field: 'A',
                 checkbox:'true',
                 width:30
             },*/
            {field: 'xhdmc', title: '信号灯名称', hidden: false, width: 120},
            {field: 'xhdbh', title: '信号灯编号', hidden: false, width: 120},
            {field: 'qymc', title: '区域名称', hidden: false, width: 120},
            {field: 'lkmc', title: '路口名称', hidden: false, width: 120},
            {
                field: 'time', title: '检测时间', hidden: false, width: 125,
                formatter: function (value, row, index) {
                    if (value == null || value == '' || value == 'null' || value === undefined) {
                        return "";
                    } else {
                        return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                    }
                }
            },
            {field: 'repairstatename', title: '维修状态', hidden: false, width: 120},
            {
                field: 'status', title: '当前状态', hidden: false, width: 60,
                formatter: function showClick(value, record, num) {
                    if (value == "20") {
                        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 "<div style='height:13px;width:13px;background-color:green;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
                    }
                }
            },
            {
                field: 'bz', title: '备注', width: 300, hidden: false,
                formatter: function showClick(value, record, num) {
                    if (value == null)
                        return "<a  href='javascript:void(0);' class='ahref' onclick='showupdate(" + num + ")'>无 </a>";
                    else
                        return "<a  href='javascript:void(0);' class='ahref' onclick='showupdate(" + num + ")'>" + value + "</a>";
                }
            }
        ]]
    });
}


function showupdate(index) {
    var rows = $('#tableGrid').datagrid('getRows')[index];
    windowId = top.$.createWin({
        title: '备注',
        width: 600,
        height: 300,
        data: {"currentstateid": rows.xh, "remarks": rows.bz},
        buttons: [{
            text: $.fn.textbox.defaults.ok,
            handler: saveNodeInfo
        }],
        url: sysUrl + '/yw/videoalarm/ywvideocurrentstateDetail.jsp'
    });

}


//查询
function doSearch() {
    $('#tableGrid').datagrid('load', getQueryParams());
}


//保存
function saveNodeInfo() {
    var win = $.getTopWindowById(windowId);
    var jq = win.$;
    $.ajax({
        type: 'post',
        dataType: 'json',
        url: 'nhywjtxhdglAction!update_NhywjtxhdglRemarks.do',
        data: {"bz": jq('#bz').val(), "XGTJxh": jq('#XGTJid').val()},
        success: function (value) {
            if (value == "success") {
                $.showMsg("操作成功！");
                top.$("#" + windowId).dialog('close');
                doSearch();
            } else if (value == "same") {
                $.showMsg("已存在！");
            } else {
                $.showMsg("操作失败！");
            }
        }
    });
}

