<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>新增装置</title>
    <link rel="stylesheet" href="${ctx}/js/plugin/layui/css/layui.css"
          media="all">
    <link rel="stylesheet"
          href="${ctx}/css/plugin/bootstrap/bootstrap.min.css">
    <link rel="stylesheet"
          href="${ctx}/css/plugin/datatables/datatables.bootstrap.css">
    <link rel="stylesheet"
          href="${ctx}/css/plugin/datatables/buttons.dataTables.min.css">
    <link rel="stylesheet" href="${ctx}/css/basic.css">
    <style type="text/css">
        html {
            overflow: hidden;
        }
    </style>
</head>
<body class="bg-white">
<div class="layui-form-item layui-form-text pdt15" style="width:320px">
    <label class="layui-form-label font12" style="width:100px;font-weight: normal;">名称</label>
    <div class="layui-input-block">
        <input type="text" id="devName" placeholder="请输入" autocomplete="off" maxlength="64"
               class="form-control">
    </div>
</div>

<div class="layui-form-item">
    <div class="layui-layer-btn">
        <a class="layui-layer-btn0" id="addDev">确定</a>
        <a class="layui-layer-btn1" id="cancleAdd">取消</a>
    </div>
</div>
<input type="hidden" value="${sessionScope.gdIndex}" id="gdIndex" name="gdIndex">
<input type="hidden" value="${sessionScope.subIndex}" id="subIndex" name="subIndex">
<script src="${ctx}/js/plugin/jquery/jquery.min.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="${ctx}/js/plugin/layui/layer.js"></script>
<script src="${ctx}/js/basic.js"></script>
<script src="${ctx}/js/plugin/xss/xss.js"></script>
<script src="${ctx}/jspJS/utils/judgeAjaxStatus.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("html").css("height", "100px");

        //获取默认装置名称
        var gdIndex = $("#gdIndex").val();
        $.ajax({
            url: '/pqs9900/device/findMaxDeviceName',
            type: 'POST',
            dataType: "text",
            data: {
                gdIndex: gdIndex
            },
            success: function (data) {
                $('#devName').val(data);
            }
        });
    });

    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引

    $("#cancleAdd").click(function () {
        parent.layer.close(index);
    });

    //         $("#choose").change(function(){
    //         	var choose = $(this).val();
    //         	var index = parent.layer.getFrameIndex(window.name);
    //         	if(choose=="1"){
    //         		$("#zdm").removeAttr("hidden");
    //         		$("#my").removeAttr("hidden");
    //             	parent.layer.style(index,{height:"340px"});
    //         	}else{
    //         		$("#zdm").attr("hidden","true");
    //         		$("#my").attr("hidden","true");
    //             	parent.layer.style(index,{height:"220px"});
    //         	}
    //         })

    $("#addDev").click(function () {
        var devName = $("#devName").val();
        devName = filterXSS(devName);
        var gdIndex = $("#gdIndex").val();
        var subIndex = $("#subIndex").val();
        gdIndex = filterXSS(gdIndex);
        subIndex = filterXSS(subIndex);
//             var series = $("#series").val();
//             series=filterXSS(series);
//             var key = $("#key").val();
//             key=filterXSS(key);
//             var choose = $("#choose").val();
        if (devName == null || devName == "") {
            layer.tips('名称不可为空', '#devName', {
                tips: [1, 'red'] //还可配置颜色
            });
        }
//             else if(choose=="1" && series.length<8){
//             	layer.tips('请至少输入8位终端码', '#series', {
//             		 tips: [1, 'red'] //还可配置颜色
//             	});
//             }else if(choose=="1" && key.length<8){
//             	layer.tips('请至少输入8位秘钥', '#key', {
//             		 tips: [1, 'red'] //还可配置颜色
//             	});
//             }
        else {
            $.ajax({
                url: '/pqs9900/device/addDevice',
                type: 'POST',
                dataType: "text",
                data: {
                    devName: devName,
                    gdIndex: gdIndex,
                    subIndex: subIndex
//                         ,series:series,
//                         key:key
                },
                success: function (data) {
                    var info = $.parseJSON(data);
                    if (info.code == 200) {
                        layer.msg('添加成功!', {icon: 1, time: 1000});
                        parent.layer.close(index);
                        var pastIndex = info.body;
                        var node = "4";
                        parent.window.getTree();
                        parent.window.zTreeOnAsyncSuccess(node, pastIndex);
                    } else if (info.code == 400) {
                        layer.msg('添加失败！名称已存在', {icon: 2, time: 1000});
                    } else if (info.code == 300) {
                        layer.msg('添加失败！请先添加服务器信息！', {icon: 2, time: 1000});
                    } else if (info.code == 501) {
                        layer.msg(info.message, {icon: 2, time: 1000});
                    } else {
                        layer.msg('添加失败！', {icon: 2, time: 1000});
                    }
                }

            });
        }
    });

</script>
</body>
</html>