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


//查询条件
function getQueryParams() {
    var sParam = {};
    sParam.LIKExhdmc = $('#LIKExhdmc').textbox("getValue");
    sParam.type = '1';
    return sParam;
}

//加载列表
function initGrid() {
    var toolbar = [
        {
            text: '查询',
            iconCls: 'icon-search',
            handler: doSearch
        }, {
            text: '增加',
            iconCls: 'icon-add',
            handler: doAddTask
        }, {
            text: '修改',
            iconCls: 'icon-cut',
            handler: doEdit
        }, {
            text: '删除',
            iconCls: 'icon-delete',
            handler: doDelete_batch
        }, {
            text: '选择设备',
            iconCls: 'icon-add',
            handler: doChannelSet
        },
        {
            text: '启用',
            iconCls: 'icon-start',
            handler: switchOpen
        }, {
            text: '停止',
            iconCls: 'icon-end',
            handler: switchClose
        }];
    $('#searchGrid').datagrid({
        toolbar: toolbar,
        rownumbers: true,
        queryParams: getQueryParams(),
        url: 'nhywledydpxjbAction!listNhywledydpxjbtaskByPage.do',
        columns: [[
            {
                field: 'A',
                checkbox: 'true',
                width: 30
            },

            {field: 'id', title: '编码', hidden: false, width: 120},
            {field: 'rw_name', title: '任务名称', hidden: false, width: 120},
            {
                field: 'rw_type', title: '任务类型', hidden: false, width: 120,
                formatter: function showClick(value, record, num) {
                    if (value == "0") {
                        return "设备是否异常";
                    }
                }
            },
            {field: 'zxsj', title: '执行时间', hidden: false, width: 120},
            {field: 'jgsj', title: '间隔时间', hidden: false, width: 120},
            {field: 'create_user', title: '创建人', hidden: false, width: 120},
            {field: 'create_time', title: '创建时间', hidden: false, width: 120},
            {field: 'csmc', title: '是否启用', hidden: false, width: 80}
        ]]

    });
}

//选择集合
function doChannelSet() {
    var selRows = $('#searchGrid').datagrid('getSelected');
    var selChecked = $('#searchGrid').datagrid('getChecked');
    if (selChecked.length > 1) {
        $.showMsg('请选择一条记录关联集合! ');
        return;
    } else {
        if (!selRows) {
            $.showMsg('请选择一条记录选择集合! ');
            return;
        }
        windowId = top.$.createWin({
            title: '增加设备',
            width: 800,
            height: 600,
            data: selRows,
            url: sysUrl + "/yw/xhdmanage/ywvxhdtaskbyslist/addxhdList.jsp?id=" + selRows.id + "&type=1"
        });
    }
}

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

//设置规则或者关联通道集合界面操作完成后关闭弹窗事件
function saveRuleOrSet() {
    //batch_insert
    top.$("#" + windowId).dialog('close');
    doSearch();
}

function switchOpen() {				// 开启
    var selRows = $('#searchGrid').datagrid('getChecked');
//	console.log("selRows：",selRows);
    var ids = '';
    if (selRows === null || selRows.length == 0) {
        $.showMsg('请先选择一行! ');
        return;
    } else {
        selRows.forEach(function (item) {
            ids += item.id + ',';
        })
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'nhywledydpxjbAction!edit_Nhywledydpxjb.do',
            data: {'ids': ids, 'rw_state': 1},
            success: function (res) {
                if (res) {
                    $.showMsg("操作成功！");
                    doSearch();
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });
    }
}

function switchClose() {				// 关闭
    var selRows = $('#searchGrid').datagrid('getChecked');
    var ids = '';
    if (selRows === null) {
        $.showMsg('请先选择一行! ');
        return;
    } else {
        selRows.forEach(function (item) {
            ids += item.id + ',';
        })
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'nhywledydpxjbAction!edit_Nhywledydpxjb.do',
            data: {'ids': ids, 'rw_state': 0},
            success: function (res) {
                if (res) {
                    $.showMsg("操作成功！");
                    doSearch();
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });
    }
}


//增加任务
function doAddTask() {
    var row = {};
    toEdit = false;
    windowId = top.$.createWin({
        title: '增加任务',
        width: 650,
        height: 400,
        data: row,
        buttons: [{
            text: $.fn.textbox.defaults.ok,
            handler: refreshList
        }],
        url: sysUrl + "/yw/xhdmanage/xhdinfo/ywxhdtask_show.jsp?type=1"
    });
}

//编辑
function doEdit() {
    toEdit = true;
    var row = $("#searchGrid").datagrid('getSelected');
    var selRows = $('#searchGrid').datagrid('getChecked');
    if (selRows.length > 1) {
        $.showMsg('请选择一条记录进行修改! ');
    } else {
        if (!row || row == null) {
            $.showMsg("请选择一条记录进行修改！");
            return;
        } else {
            row.flag=true;
            windowId = top.$.createWin({
                title: '编辑任务',
                width: 400,
                height: 450,
                data: row,
                buttons: [{
                    text: $.fn.textbox.defaults.ok,
                    handler: refreshList
                }],
                url: sysUrl + "/yw/xhdmanage/xhdinfo/ywxhdtask_show.jsp?type=2"
            });
        }
    }
}

//批量删除
function doDelete_batch() {
    var selRows = $('#searchGrid').datagrid('getChecked');
    var keyword = [];
    if (selRows === null || !selRows || selRows.length < 1 || selRows == []) {
        $.showMsg('请先选择一行! ');
        return;
    } else {
        for (var i = 0; i < selRows.length; i++) {
            keyword.push(selRows[i].id);
        }
        top.$.dlg.confirm('你确认要删除吗?', function () {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: 'nhywledydpxjbAction!batch_delete_Nhywledydpxjb.do',
                data: {keyword: keyword.join(",")},
                success: function (flag) {
                    if (flag == "success") {
                        $.showMsg("操作成功！");
                        doSearch();
                    } else {
                        $.showMsg("操作失败！");
                    }
                }
            });
        });
    }
}


function refreshList() {

    var win = $.getTopWindowById(windowId);
    var jq = win.$;
    var model = $.getFormValues("childform", windowId);
    if (!win.$('#childform').form('enableValidation').form('validate')) {
        return;
    }
    if (toEdit) {
        model.XGTJid = jq('#id').val();
        //model.checkparam = " and xh='"+model.xh+"' ";
        $.ajax({
            type: 'post',
            dataType: 'json',
            url:'nhywledydpxjbAction!update_Nhywledydpxjb.do',
            data: model,
            success: function (value) {
                if (value == "success") {
                    $.showMsg("操作成功！");
                    top.$("#" + windowId).dialog('close');
                    doSearch();
                } else if (value == "same") {
                    $.showMsg("已存在！");
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });
    } else {
        var insertParam = {};
        insertParam.rw_name = jq("#rw_name").val();
        if (insertParam.rw_name == "") {
            $.showMsg("任务名称不能为空！");
            return;
        }
        insertParam.type = jq("#type").val();
        insertParam.rw_type = jq("#rw_type").combobox('getValue');
        insertParam.rw_state = jq("#rw_state").val();
        insertParam.rwms = jq("#rwms").val();
        insertParam.zxsj = jq("#zxsj").val();
        insertParam.jgsj = jq("#jgsj").val();

        if (insertParam.rwms == Constants.everydayonce) {
            insertParam.daystarttime = jq("#daystarttime").val();
        } else if (insertParam.rwms == Constants.everydaymanytimes) {
            insertParam.daystarttime = jq("#daystarttime").val();
            insertParam.daystarttimebyhour = jq("#daystarttimebyhour").val();
        } else if (insertParam.rwms == Constants.customizingtime) {
            if (jq("#mondayswitch").prop("checked", this.checked)) {
                insertParam.mondayswitch = 1;
                insertParam.mondayruntime = jq("#mondayruntime").val();
                insertParam.mondayrunbyhour = jq("#mondayrunbyhour").val();
            } else {
                insertParam.mondayswitch = 0;
            }
            if (jq("#tuesdayswitch").prop("checked", this.checked)) {
                insertParam.tuesdayswitch = 1;
                insertParam.tuesdayruntime = jq("#tuesdayruntime").val();
                insertParam.tuesdayrunbyhour = jq("#tuesdayrunbyhour").val();
            } else {
                insertParam.tuesdayswitch = 0;
            }
            if (jq("#wednesdayswitch").prop("checked", this.checked)) {
                insertParam.wednesdayswitch = 1;
                insertParam.wednesdayuntime = jq("#wednesdayuntime").val();
                insertParam.wednesdayunbyhour = jq("#wednesdayunbyhour").val();
            } else {
                insertParam.wednesdayswitch = 0;
            }
            if (jq("#thursdayswitch").prop("checked", this.checked)) {
                insertParam.thursdayswitch = 1;
                insertParam.thursdayruntime = jq("#thursdayruntime").val();
                insertParam.thursdayrunbyhour = jq("#thursdayrunbyhour").val();
            } else {
                insertParam.thursdayswitch = 0;
            }
            if (jq("#fridayswitch").prop("checked", this.checked)) {
                insertParam.fridayswitch = 1;
                insertParam.fridayruntime = jq("#fridayruntime").val();
                insertParam.fridayrunbyhour = jq("#fridayrunbyhour").val();
            } else {
                insertParam.fridayswitch = 0;
            }
            if (jq("#saturdayswitch").prop("checked", this.checked)) {
                insertParam.saturdayswitch = 1;
                insertParam.saturdatruntime = jq("#saturdatruntime").val();
                insertParam.saturdatrunbyhour = jq("#saturdatrunbyhour").val();
            } else {
                insertParam.saturdayswitch = 0;
            }
            if (jq("#sundayswitch").prop("checked", this.checked)) {
                insertParam.sundayswitch = 1;
                insertParam.sundayruntime = jq("#sundayruntime").val();
                insertParam.sundayrunbyhour = jq("#sundayrunbyhour").val();
            } else {
                insertParam.sundayswitch = 0;
            }
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'nhywledydpxjbAction!insert_Nhywledydpxjb.do',
            data: insertParam,
            success: function (value) {
                if (value == "success") {
                    jq.showMsg("操作成功！");
                    top.$("#" + windowId).dialog('close');
                } else if (value == "same") {
                    $.showMsg("已存在！");
                } else {
                    $.showMsg("操作失败！");
                }
            }
        });

    }

    doSearch();
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}