<%@ page language="java" import="java.util.*" pageEncoding="gbk" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
    <%@include file="../../frame/assets/header-all.jsp" %>
    <script type="text/javascript" src="../nhyw/js/jquery.form.js"></script>
    <script type="text/javascript" src=""></script>
</head>
<body>

<div class="easyui-layout">
    <form id="childform" class="easyui-form" data-options="novalidate:true">
        <div class="form-line clearfix">
            <div class="fl">
                <span class="text-content">设备类型：</span>
                <input id="alarmdevicetype" class="easyui-combobox"
                       data-options="required:true,validType:'length[0,128]'">
            </div>
            <div class="fl" id="machine" style="display: none">
                <span class="text-content">服务器名称：</span>
                <input id="machineid" class="easyui-combobox" data-options="required:true,validType:'length[0,128]'">
            </div>
            <div class="fl" id="video" style="display: none">
                <span class="text-content"><em class="must"></em>设备名称：</span>
                <input id="gysmc" disable class="easyui-textbox" data-options="required:false">
                <div class="input-addimg" style="margin-top: -22px; margin-left: 245px;">
                    <a href="javascript:selectGys();" class="icon-add img-btn1"></a>
                </div>
            </div>
        </div>
        <%--        根据选择设备类型展示是服务器数据还是监控数据--%>
        <div id="videoparam">
            <div class="form-line clearfix">
                <div class="fl">
                    <span class="text-content"><em class="must"></em>通道编号：</span>
                    <input id="LIKEtdbh" class="easyui-textbox" data-options="required:false,validType:'length[0,40]'"
                    >
                </div>
                <div class="fl">
                    <span class="text-content"><em class="must"></em>设备编码：</span>
                    <input id="sbbh" disabled class="easyui-textbox"
                           data-options="required:false,validType:'length[0,40]'"
                    >
                </div>
            </div>

        </div>
        <%--        <div class="form-line clearfix"  >--%>

        <%--            <div class="fl">--%>
        <%--                <span class="text-content"><em class="must"></em>CPU使用率：</span>--%>
        <%--                <input id="cpuusag" class="easyui-textbox" data-options="required:false,validType:'length[0,10]'">--%>
        <%--            </div>--%>

        <%--            <div class="fl">--%>
        <%--                <span class="text-content"><em class="must"></em>内存使用率：</span>--%>
        <%--                <input id="memoryusag" class="easyui-textbox" data-options="required:false,validType:'length[0,10]'">--%>
        <%--            </div>--%>
        <%--            <div class="fl">--%>
        <%--                <span class="text-content"><em class="must"></em>硬盘使用率：</span>--%>
        <%--                <input id="harddiskusag" class="easyui-textbox" data-options="required:false,validType:'length[0,10]'">--%>
        <%--            </div>--%>
        <%--         </div>--%>
        <div class="form-line clearfix">
            <div class="fl">
                <span class="text-content"><em class="must"></em>告警名称：</span>
                <input id="alarrulename" disable class="easyui-textbox">
                <div class="input-addimg" style="margin-top: -22px; margin-left: 245px;">
                    <a href="javascript:selectgj();" class="icon-add img-btn1"></a>
                </div>
            </div>
            <div class="fl">
                <span class="text-content"><em class="must"></em>告警子类：</span>
                <select class="easyui-combobox" id="alarmsubtypes">
                </select>
            </div>
        </div>
        <div class="form-line clearfix">
            <div class="fl">
                <span class="text-content"><em class="must"></em>告警等级：</span>
                <select class="easyui-combobox" id="alarmgrade">
                    <option value="1">重要</option>
                    <option value="2">次要</option>
                    <option value="3">一般</option>
                    <option value="4">提示</option>
                </select>
            </div>
            <div class="fl">
                <span class="text-content"><em class="must"></em>发生时间：</span>
                <input id="faultoccurtime" class="easyui-datetimebox">
            </div>
        </div>
        <div class="form-line clearfix">

            <div class="fl">
                <span class="text-content"><em class="must"></em>故障描述：</span>
                <textarea id="falutdescription" style="width:400px;height:60px"> </textarea>
            </div>
        </div>
        <div id="upload">

            <div class="form-line clearfix">
                <div class="fl">
                    <span class="text-content"><em class="must"></em>故障图片：</span>
                    <input disabled id="xctp2" class="easyui-textbox"
                           data-options="required:false,validType:'length[0,128]'">
                </div>
                <div class="fl">
                    <form id="uploadForm2" class="form" action="" method="post" enctype="multipart/form-data">
                        <input type="file" name="myFile" multiple style="margin-left: -85px !important;"/>
                        <input type="button" id="submitPic2" value="上传"/>
                    </form>
                    <span id="uploadResultPic2" style="color:blue" class="text-content"></span>
                </div>
            </div>
            <div class="form-line clearfix">
                <div class="fl">
                    <span class="text-content"><em class="must"></em>视频地址：</span>
                    <input disabled id="xcsp" class="easyui-textbox"
                           data-options="required:false,validType:'length[0,128]'">
                </div>
                <div class="fl">
                    <form id="uploadFormVideo" class="form" action="" method="post" enctype="multipart/form-data">
                        <input type="file" name="myFile" id="xxxxx" multiple/>
                        <input type="button" id="submitVideo" value="上传"/>
                    </form>
                    <span id="uploadResultVideo" class="text-content" style="color:blue"></span>
                </div>
            </div>
        </div>
    </form>
</div>
</div>
</body>
</html>
<script type="text/javascript">
    var sel = {};
    var winId = $.getUrlParam("winId");
    var jobid = $.getUrlParam("jobid");
    var isdelayatbefor = false;
    var alarmeventinfo = {};
    var alarmsubtypeObj=[];
    var devicetypedata = [
        {"id": "1", "text": "监控", "selected": true},
        {"id": "2", "text": "服务器"}
    ];
    $(function () {
        //根据选择的设备类型加载设备信息
        $('#alarmdevicetype').combobox({
            data: devicetypedata,//加载数据
            valueField: 'id',
            textField: 'text',
            //onselect事件查询设备信息
            onSelect: function (record) {
                if (record.id == '1') {
                    //如果选择的是监控
                    $("#machine").css("display", "none");
                    $("#video").css("display", "block");
                    $("#videoparam").css("display", "block");
                    $("#upload").css("display", "block");

                } else if (record.id == '2') {
                    $("#machine").css("display", "block");
                    $("#video").css("display", "none");
                    $("#videoparam").css("display", "none");
                    $("#upload").css("display", "none");
                }
            }
        });

        $('#machineid').combobox({
            url: 'ywmachineinfoAction!query_Ywmachineinfo.do',
            textField: "machinename",
            valueField: "machineid",
            panelWidth: 148,
            value: ""
        });
        $("#faultoccurtime").datetimebox("setValue", $.getTodayStart(0, "00:00:00"));
        // 确认  反馈人相关信息
        $('#jobacceptcompany').combobox({				// 确认反馈单位
            url: 'ywrepairjobinfoAction!getReportBm.do',    // 查询有审批工单权限的部门
            textField: "bmmc",
            valueField: "bmbh",
            panelWidth: 148,
            value: ""
        });
        $("#jobacceptcompany").combobox({
            onChange: function (n, o) {
                $('#jobreportpeople').combobox({				// 选取确认反馈人
                    url: 'ywrepairjobinfoAction!getReportUser.do?bmbh=' + n,    // 查询部门下有审批工单权限的人
                    textField: "yhmc",
                    valueField: "yhzh",
                    value: ""
                });
            }
        })
        // 点击确认提交数据
        parent.parent.$(".l-btn-text").on("click", function () {
            if ($(this).text() == "确定") {
                //新增到库中
                saveInfo();
            }
        })
    });

    function doSearch() {
        //channelSmallType
        var param = {
            LIKEsbbh: $('#sbbh').textbox('getValue'),
            LIKEtdmc: $('#tdmc').textbox('getValue'),
            tdlx: $('#tdlx').combobox('getValue')
        };
        $('#tableGrid').datagrid('options').url = "sbtdspsrAction!query_Sbtdspsr_ext.do";
        $("#tableGrid").datagrid('load', param);
    }

    function selectGys() {
        var url = "";
        if (top.sysUrl) {
            url = top.sysUrl + "/yw/ptxztree/ptxzs_show.jsp";
        }
        gysWin = top.$.createWin({
            title: "设备",
            width: 800,
            height: 500,
            url: url,
            buttons: [{
                text: $.fn.textbox.defaults.ok,
                handler: doUpdateGysValue
            }]
        });

    }

    function selectgj() {
        var url = "";
        if (top.sysUrl) {
            url = top.sysUrl + "/yw/alarmtaskmanage/alarmrule/ywalarmruleinfolist_show.jsp";
        }
        gjWin = top.$.createWin({
            title: "告警规则",
            width: 700,
            height: 500,
            url: url,
            buttons: [{
                text: $.fn.textbox.defaults.ok,
                handler: selectrule
            }]
        });


    }

    function doUpdateGysValue() {
        var win = $.getTopWindowById(gysWin);
        var selRows_no = win.$('#noCodeSearch').datagrid('getSelections');
        var selRows_out = win.$('#outCodeSearch').datagrid('getSelections');

        if (selRows_no.length <= 0 && selRows_out.length <= 0) {
            $.showMsg('请先选择一条记录! ');
            return;
        }

        if (selRows_no.length > 1) {
            $.showMsg('选择记录数量大于1! ');
            return;
        }
        var idArray = [];

        if (selRows_no.length > 0) {
            for (var i = 0; i < selRows_no.length; i++) {
                alarmeventinfo.alarmdeviceid = selRows_no[i].sbbh;
                $("#gysmc").textbox("setValue", selRows_no[i].tdmc);
                $("#LIKEtdbh").textbox("setValue", selRows_no[i].tdbh);
                $("#sbbh").textbox("setValue", selRows_no[i].sbbh);
            }
        }

        if (selRows_out.length > 0) {
            for (var i = 0; i < selRows_out.length; i++) {
                alarmeventinfo.alarmdeviceid = selRows_out[i].sbbh;
                $("#gysmc").textbox("setValue", selRows_out[i].tdmc);
                $("#LIKEtdbh").textbox("setValue", selRows_out[i].tdbh);
                $("#sbbh").textbox("setValue", selRows_out[i].sbbh);
            }
        }

        top.$("#" + gysWin).dialog("close");
    }

    function selectrule() {
        var win = $.getTopWindowById(gjWin);
        var selRows_rule = win.$('#ruleGrid').datagrid('getSelections');
        if (selRows_rule.length > 0) {
            for (var i = 0; i < selRows_rule.length; i++) {
                alarmeventinfo.alarmtype = parseInt(selRows_rule[i].alarmsource);
                //alarmeventinfo.alarmsource = parseInt(selRows_rule[i].alarmsource);
                //加载到二级目录中，将1,2,3,4转化为数据表中的subtypecode
                var  types= selRows_rule[i].alarmtype.split(',');
                for (var j = 0; j<types.length; j++) {
                    getalarmtype(selRows_rule[i].alarmsource,types[j],j);
                }
                $('#alarmsubtypes').combobox({
                    data: alarmsubtypeObj,//加载数据
                    valueField: 'value',
                    textField: 'text'});
                alarmeventinfo.alarmruleid = selRows_rule[i].alarmruleid;
                alarmeventinfo.alarmsourcename = parseInt(selRows_rule[i].alarmsourcemc);
                $("#alarrulename").textbox("setValue", selRows_rule[i].alarmrulename);
                $("#faulttypeMc").textbox("setValue", selRows_rule[i].alarmsourcemc);
                $('#alarmgrade').combobox('select', selRows_rule[i].alarmgrade);
            }
        }
        top.$("#" + gjWin).dialog("close");
    }
    function  getalarmtype(alarmsource,type,index) {
        if(alarmsource=="1") {
            if (type== "2") {
                alarmsubtypeObj[index] = {value: "101", text: "网络异常"};
            } else if (type == "3") {
                alarmsubtypeObj[index] = {value: "102", text: "设备不在线"};
            }
            else if (type == "4") {
                alarmsubtypeObj[index] = {value: "104", text: "视频抽帧异常"};
            }
            else if (type == "5") {
                alarmsubtypeObj[index] = {value: "105", text: "视频质量诊断异常"};
            }
            else if (type == "6") {
                alarmsubtypeObj[index] = {value: "107", text: "录像异常"};
            }
        }else if( alarmsource=="2"){
            if (type== "2") {
                alarmsubtypeObj[index] = {value: "201", text: "主机不在线或程序异常"};
            } else if (type == "3") {
                alarmsubtypeObj[index] = {value: "202", text: "CPU异常"};
            }
            else if (type == "4") {
                alarmsubtypeObj[index] = {value: "203", text: "内存异常"};
            }
            else if (type == "5") {
                alarmsubtypeObj[index] = {value: "204", text: "硬盘异常"};
            }
            else if (type == "6") {
                alarmsubtypeObj[index] = {value: "6", text: "超时未上报"};
            }

        }

    }
    //查询条件
    function getQueryParams() {
        var model = {};
        var tableparam = "";
        model.xzId = xzTree.tree("getSelected").xzId;
        model.LIKEvdId = $('#vdId').textbox('getValue');
        model.LIKEchannelName = $('#channelName').textbox('getValue');
        model.channelSmallType = $('#channelSmallType').combobox('getValue');
        model.tableparam = tableparam;
        return model;
    }

    $("#submitPic2").on('click', function () {
        $.ajax({
            url: "uploadPictureAction!uploadPicture.do",
            type: "POST",
            data: new FormData($('#uploadForm2')[0]),//$( '#uploadForm1').serialize(),    //表单序列化 ,【注意】上传文件的文件流是无法被序列化并传递的
            processData: false,   //  告诉jquery不要处理发送的数据
            contentType: false,    // 告诉jquery不要设置content-Type请求头
            success: function (data) {
                if (data && data != "error") {
                    $.showMsg("上传成功");
                    $("#xctp2").textbox("setValue", data);
                    $("#uploadResultPic2").text("上传成功");
                } else {
                    $.showMsg("上传失败");
                    $("#uploadResultPic2").text("上传失败");
                }
                //$("#uploadForm2").resetForm();
            }
        });
    });
    $("#submitVideo").on('click', function () {
        $.ajax({
            url: "uploadPictureAction!uploadPicture.do",
            type: "POST",
            data: new FormData($('#uploadFormVideo')[0]),//$( '#uploadForm1').serialize(),    //表单序列化 ,【注意】上传文件的文件流是无法被序列化并传递的
            processData: false,   //  告诉jquery不要处理发送的数据
            contentType: false,    // 告诉jquery不要设置content-Type请求头
            success: function (data) {
                if (data && data != "error") {
                    $.showMsg("上传成功");
                    $("#xcsp").textbox("setValue", data);
                    $("#uploadResultVideo").text("上传成功");
                } else {
                    $.showMsg("上传失败");
                    $("#uploadResultVideo").text("上传失败");
                }
                //$("#uploadFormVideo").resetForm();
            }
        });

    });

    function saveInfo() {								// 数据入库
        alarmeventinfo.alarmdevicetype = parseInt($('#alarmdevicetype').combobox("getValue"));
        if (alarmeventinfo.alarmdevicetype == "2") {
            alarmeventinfo.alarmdeviceid = $('#machineid').combobox("getValue");
            if (isNull(alarmeventinfo.alarmdeviceid)) {
                top.$.dlg.alertError('服务器不能为空! ');
                return;
            }
            alarmeventinfo.alarmdevicename = $('#machineid').combobox("getText");
        } else {
            if (isNull($("#sbbh").textbox("getValue"))) {
                top.$.dlg.alertError('监控不能为空! ');
                return;
            }
            alarmeventinfo.channelnum = parseInt($("#LIKEtdbh").textbox("getValue"));
            alarmeventinfo.pictureurl = $("#xctp2").textbox("getValue");
            alarmeventinfo.videourl = $("#xcsp").textbox("getValue");
            alarmeventinfo.alarmdevicename = $("#gysmc").textbox("getValue");
        }
        alarmeventinfo.alarmgrade = parseInt($("#alarmgrade").textbox("getValue"));
        alarmeventinfo.alarmrulename = $("#alarrulename").textbox("getValue");
        if (isNull(alarmeventinfo.alarmrulename)) {
            top.$.dlg.alertError('告警不能为空! ');
            return;
        }
        alarmeventinfo.alarmreason = $("#falutdescription").val();
        alarmeventinfo.alarmtime = $('#faultoccurtime').datetimebox('getValue');
        alarmeventinfo.alarmsubtypecode= $('#alarmsubtypes').combobox("getValue");
        alarmeventinfo.alarmsubtypename= $('#alarmsubtypes').combobox("getText");
        alarmeventinfo.processstatus = 900;
        $.ajax({
            url: "ywalarmeventinfoAction!insert_Ywalarmeventinfo.do",
            type: "POST",
            dataType: "JSON",
            data: alarmeventinfo,
            success: function (res) {
                if (res == 'success') {
                    $.showMsg("操作成功");
                    top.$("#" + winId).dialog('close');
                }
            }
        })
    }

    function isNull(str) {
        if (str == null || str == '' || str == " " || str == "null" || str == undefined) {
            return true;
        } else {
            return false;
        }
    }

</script>