<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <title>PQS9300技术监督管理系统</title>
    <link rel="stylesheet" href="${ctx}/css/basic.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/bootstrap/bootstrap.css">

    <style>

    </style>
</head>
<body class="container-fluid">
    <div class="form-group" style="margin-top: 15px">
        <label style="margin-right: 15px">名称</label>
        <input type="text" id="wf_name"
               autocomplete="off" placeholder="请输入名称" class="form-control" style="width: 170px;display: inline-block;">
    </div>
    <div class="form-group">
        <label style="margin-right: 15px">等级</label>
        <select  id="grade"  class="form-control" style="width: 170px;display: inline-block;">
            <option value="0">一级</option>
            <option value="1">二级</option>
            <option value="2">三级</option>
        </select>
    </div>
    <div  style="text-align: center;margin-top: 30px">

        <button type="submit" id="submit" class="btn btn-primary">提交</button>

    </div>
</body>
<script src="${ctx}/js/plugin/jquery/jquery.min.js"></script>
<script src="${ctx}/jspJS/utils/judgeAjaxStatus.js"></script>
<script src="${ctx}/js/plugin/xss/xss.js"></script>
<script src="${ctx}/jspJS/utils/loading.js"></script>
<script src="${ctx}/js/plugin/layui/layer.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap.min.js"></script>

<script>
        var loading;
        var types = ''
        var wfIndex = null;

        $(function () {
            types = getQueryString('type')

        if (types == 1) {
            //新增


        } else {
            //修改
            wfIndex = getQueryString('wfIndex')
            //加载信息
            $.ajax({
                type: 'GET',
                dataType: "json",
                url: '/pqs9300/strategy/getEarlyWarnById',
                contentType: 'application/json;charset=UTF-8',
                async: false,
                data: {wfIndex: wfIndex},
                beforeSend: function () {
                    loading = layer.load(1)
                },
                success: function (data) {
                    if (data.code == 200) {
                        let da = data.body
                        $('#wf_name').val(da.wfName)
                        $('#grade').val(da.grade)
                    } else {
                        layer.msg(data.message, {icon: 2})
                    }
                    layer.close(loading)
                },
                error: function () {

                }
            });
        }

        //
        $('body').on('click','#submit',function () {
            let wf_name = $('#wf_name').val()
            let flag= verify(wf_name)
            if(!flag){
                layer.msg('名称过长',{icon:2})
                return
            }
            let grade = $('#grade').val()
            var map = {}
            var url = null
            if (types == 1) {
                //新增
                map = {
                    wfName: filterXSS(wf_name),
                    grade: filterXSS(grade)
                }
                url = '/pqs9300/strategy/addEarlyWarn'
            } else {
                //修改
                url = '/pqs9300/strategy/updateEarlyWarn'
                map = {
                    wfIndex: wfIndex,
                    wfName: filterXSS(wf_name),
                    grade: filterXSS(grade)
                }
            }

            $.ajax({
                type: 'POST',
                dataType: "json",
                url: url,
                contentType: 'application/json;charset=UTF-8',
                async: false,
                data: JSON.stringify(map),
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.message, {icon: 1})
                        window.parent.refresh()
                        window.setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }, 1000)

                    } else {
                        layer.msg(data.message, {icon: 2})
                    }

                },
                error: function () {
                    layer.msg('未知错误', {icon: 2})
                }
            });
        })

        })



        function verify(value) {
            var j, sum;
            sum = 0;
            if(value!=null && value!=''){
                for (j = 0; j < value.length; j++) {
                    if ((value.charCodeAt(j) >= 0) && (value.charCodeAt(j) <= 1024))
                        sum = sum + 1;
                    else
                        sum = sum + 2;
                }
            }
            if (sum > 36) {
                return false;
            }else {
                return true
            }
        }



        /**
         * 获取url参数
         * @param name 参数名
         * @returns 值
         */
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var result = window.location.search.substr(1).match(reg);
            return result ? decodeURIComponent(result[2]) : '';
        }

</script>
</html>
