@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
    <body>
        <div class="layuimini-container">
            <div class="layuimini-main">
                <blockquote class="layui-elem-quote layui-text">
                    注意!IP地址格式：192.168.0.1；192.168.0.*
                </blockquote>
                <div class="layui-form layuimini-form" lay-filter="adminform">
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">策略类型</label>
                        <div class="layui-input-block">
                            <select id="F_Type" name="F_Type" lay-verify="required">
                                <option value="false" selected>拒绝访问</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">起始IP</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_StartIP" name="F_StartIP" maxlength="50" lay-verify="required" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label">结束IP</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_EndIP" name="F_EndIP" maxlength="50" value="" class="layui-input ">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">到期时间</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_EndTime" name="F_EndTime" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label">有效</label>
                        <div class="layui-input-block">
                            <input type="checkbox" checked="" id="F_EnabledMark" name="F_EnabledMark" lay-skin="switch" lay-filter="switchTest" value="true" lay-text="ON|OFF">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text layui-hide">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea id="F_Description" name="F_Description" class="layui-textarea" placeholder="请输入备注"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>
    </body>
<script>
    layui.use(['jquery', 'form', 'laydate', 'common', 'optimizeSelectOption'], function () {
        var form = layui.form,
            $ = layui.$,
            common = layui.common,
            laydate = layui.laydate;
        var keyValue = $.request("keyValue");
        //执行一个laydate实例
        laydate.render({
            elem: '#F_EndTime'
            , trigger: 'click'
            , type: 'datetime'
            , format: 'yyyy/MM/dd HH:mm:ss',
        });
        //权限字段
        common.authorizeFields('adminform');
        $(function () {
            if (!!keyValue) {
                common.ajax({
                    url: "/SystemSecurity/FilterIP/GetFormJson",
                    dataType: "json",
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                        form.render();
                    }
                });
            }
        });
        wcLoading.close();
        //select验证
        form.verify({
            required: function (value, item) {
                var msg = "必填项不能为空";
                value = $.trim(value);
                var isEmpty = !value || value.length < 1;
                // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                if (item.tagName == 'SELECT' && isEmpty) {
                    $("html").animate({
                        scrollTop: $(item).siblings(".layui-form-select").offset().top - 74
                    }, 50);
                }
                if (isEmpty) {
                    return msg;
                }
            }
        });
        function compareIP(ipBegin, ipEnd) {
            var temp1 = ipBegin.split(".");
            var temp2 = ipEnd.split(".");
            if ((temp1[0] + temp1[1] + temp1[2]) == (temp2[0] + temp2[1] + temp2[2])) {
                if (temp2[3] >= temp1[3]) {
                    return 1;
                } else {
                    return 0;
                }
            } else {
                return -1;//不在同一个网段内
            }
        }
        //监听提交
        form.on('submit(saveBtn)', function (data) {
                var postData = data.field;
                var StartIP = data.field.F_StartIP;
                var EndIP = data.field.F_EndIP;
                if (!postData["F_EnabledMark"]) postData["F_EnabledMark"] = false;
                if (!!EndIP) {

                    if (compareIP(StartIP, EndIP) == -1) {
                        common.modalMsg("不在同一个网段内");
                        return false;
                    }
                    if (compareIP(StartIP, EndIP) == 0) {
                        common.modalMsg("结束IP不能大于开始IP");
                        return false;
                    }
                }
                common.submitForm({
                    url: "/SystemSecurity/FilterIP/SubmitForm?keyValue=" + keyValue,
                    param: postData,
                    success: function () {
                        common.parentreload("data-search-btn");
                    }
                })
            return false;
        });
    });
</script>

