var datenow = new Date();
var detailWindow;
var id = $.getUrlParam("id");
var type = $.getUrlParam("type");
$(function () {
    initTabPanel();//加载列表
    initTabPanelNo();
    $("#buttonSelected").bind('click', function () {
        doCodeSearch();
    });
    $("#buttonSelected_").bind('click', function () {
        doNoCodeSearch();
    });

    //批量删除
    $("#buttondelete").bind('click', function () {
        var  keyword=[];
        var selRows = $('#CodeSearch').datagrid('getSelections');
        if (!selRows) {
            $.showMsg('请先选择记录! ');
            return;
        } else {
            for (var i = 0; i < selRows.length; i++) {
                keyword.push(selRows[i].xh);
            }
            top.$.dlg.confirm('确认删除?', function () {
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: 'nhywledydpxjbAction!batchdelete_Nhywxlydp.do',
                    data: {
                        xjid: id,
                        keyword: keyword.join(","),
                    },
                    success: function (flag) {
                        if (flag == "success") {
                            $.showMsg("操作成功！");
                            doSearch();
                        } else {
                            $.showMsg("操作失败！");
                        }
                    }
                });
            });
        }

    });
    //批量add
    $("#buttonAdd_").bind('click', function () {
        var  keyword=[];
        var selRows = $('#noCodeSearch').datagrid('getSelections');
        if (!selRows) {
            $.showMsg('请先选择记录! ');
            return;
        } else {
            for (var i = 0; i < selRows.length; i++) {
                keyword.push(selRows[i].xh);
            }
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: 'nhywledydpxjbAction!batchinsert_Nhywxlydp.do',
                data: {
                    xjid: id,
                    keyword: keyword.join(","),
                    type:type
                },
                success: function (flag) {
                    if (flag == "success") {
                        $.showMsg("操作成功！");
                        doCodeSearch();
                        doNoCodeSearch();
                    } else {
                        $.showMsg("操作失败！");
                    }
                }
            });
        }

    });
});


function initTabPanel() {
    $('#CodeSearch').datagrid({
        singleSelect: false,
        url: "nhywledydpxjbAction!listNhywxlydptaskByPage.do",
        queryParams: getQueryParams(),
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },
            {
                title: "编号",
                field: "xh",
                width: 80,
            },
            {
                title: "设备编号",
                field: "id",
                width: 80
            },
            {
                title: "名称",
                field: "mc",
                width: 120
            },
            {
                title: "区域名称",
                field: "qymc",
                width: 60
            },
            {
                title: "区域编号",
                field: "qybh",
                width: 60
            },
            {
                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="deletedevice(' + num + ')"  group="" id=""><span class="l-btn-left"><span class="l-btn-text">删除</span></span></a>';
                }
            }

        ]]
    });
}

function deletedevice(index) {
    var selRows = $('#CodeSearch').datagrid('getRows')[index];

    if (!selRows) {
        $.showMsg('请先选择记录! ');
        return;
    }
    top.$.dlg.confirm('确认删除?', function () {
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'nhywledydpxjbAction!delete_Nhywxlydp.do',
            data: {
                xjid: id,
                deviceid: selRows.xh,
                type:type
            },
            success: function (flag) {
                if (flag == "success") {
                    $.showMsg("操作成功！");
                    doCodeSearch();
                    doNoCodeSearch();
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });
    });

}

function doCodeSearch() {
    $('#CodeSearch').datagrid('load', getQueryParams());
}

function getQueryParams() {
    var sParam = {};
    sParam.LIKExh = $('#sbbh').textbox("getValue");
    sParam.type=type;
    sParam.xjid=id;
    if (type == '1') {
        sParam.LIKExhdmc = $('#tdmc').textbox("getValue");
    } else if (type == '2') {
        sParam.LIKEledmc = $('#tdmc').textbox("getValue");
    }

    return sParam;
}

function initTabPanelNo() {
    $('#noCodeSearch').datagrid({
        singleSelect: false,
        url: "nhywledydpxjbAction!listNhywledydpxjbNotaskByPage.do",
        queryParams: getQueryParams(),
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },
            {
                title: "编号",
                field: "xh",
                width: 80,
            },
            {
                title: "设备编号",
                field: "id",
                width: 80
            },
            {
                title: "名称",
                field: "mc",
                width: 120
            },
            {
                title: "区域名称",
                field: "qymc",
                width: 60
            },
            {
                title: "区域编号",
                field: "qybh",
                width: 60
            }
        ]]
    });
}

function doNoCodeSearch() {
    $('#noCodeSearch').datagrid('load', getQueryParams());
}
