<%@ 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>终端版本管理</title>
    <link rel="stylesheet" href="${ctx}/css/plugin/bootstrap/bootstrap.min.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatimepicker/bootstrap-datetimepicker.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/fontawesome/font-awesome.min.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatables/buttons.dataTables.min.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatables/datatables.bootstrap.css">
    <link rel="stylesheet" href="${ctx}/css/basic.css">
    <style>
        .form-control {
            height: 22px;
            padding: 1px 5px;
        }

        #deviceTable {
            min-width: 100%;
        }
        .btn {
            padding: 1px 6px;
            font-size:12px;
        }
        .tr {
            text-align: center;
        }

        .td {
            text-align: center;
            height: 48px;
            line-height: 48px
        }

        .table > tbody > tr > td {
            padding: 5px;
            line-height: 1.42857143;
            vertical-align: middle;
            border-top: 1px solid #ddd;
        }

        .pagination > li > a, .pagination > li > span {
            padding: 6px 12px;
        }
    </style>
</head>
<body class="container-fluid">
<div class="row">
    <%--页面头--%>
    <div class="col-md-12 mt10">
        <div class="form-inline">
            <div class="form-group fr mr10">
                <label>筛选数据：</label>
                <input style="margin-right: 20px" type="text" class="form-control" onkeyup="searchResult(this.value)" placeholder="空格分开多条件筛选数据">

                <button type="button" class="btn btn-primary " id="add_strategy">
                    <i class="glyphicon glyphicon-plus"></i>新增
                </button>

            </div>
        </div>
    </div>
    <%--页面表格--%>
    <div class="col-md-12 mt10">
        <div class="width100 halfHeight">
            <table id="deviceTable" class="table table-striped table-bordered dataTable no-footer width100">
                <thead>
                <tr>
                    <th>策略名</th>
                    <th>类型</th>
                    <th>修改人</th>
                    <th>修改时间</th>
                    <th>普通占比(%)</th>
                    <th>预警占比(%)</th>
                    <th>告警占比(%)</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
<input type="hidden" id="username" value="${username}">
<script src="${ctx}/js/plugin/jquery/jquery.min.js"></script>
<script src="${ctx}/js/plugin/layui/layer.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap.min.js"></script>
<script src="${ctx}/js/plugin/datatables/datatables.jquery.js"></script>
<script src="${ctx}/js/plugin/datatables/dataTables.buttons.min.js"></script>
<script src="${ctx}/js/plugin/datatables/datatables.bootstrap.js"></script>
<script src="${ctx}/js/plugin/datatables/buttons.html5.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}/jspJS/utils/timeControl.js"></script>
<script src="${ctx}/jspJS/utils/loading.js"></script>
<script src="${ctx}/jspJS/utils/judgeAjaxStatus.js"></script>
<script src="${ctx}/jspJS/confirmIndentity.js"></script>
<script src="${ctx}/js/plugin/xss/aes.js"></script>
<script src="${ctx}/jspJS/sm2.js"></script>
<script src="${ctx}/jspJS/sm3.js"></script>
<script>
    var dataTable;
    var height = $(window).height() - 70;
    var username = $('#username').val()
    var resetName = '一次'
    $("#interval").val("月份");
    $(function () {
        $(".halfHeight").css("height", height);
        refresh();


        //新增策略
        $('#add_strategy').click(function () {
            passwordLocalIndentity(username,resetName,function () {
                layer.open({
                    type: 2,
                    title: '新增流量策略',
                    maxmin: false,
                    area: ['25%', '80%'],
                    content: 'flowstrategyopen?type=1',
                    scrollbar: false,
                    end: function () {
                        //  refresh();
                    }
                })
            })
        })



    });

    /**
     * 初始化获取后台数据
     */
    function refresh() {
        /*获取各项条件 */
        var indexi;
         $.ajax({
             type: "get",
             url: "/pqs9900/flowstrategy/getStrategyList",
             dataType: "json",
             beforeSend: function () {
                 indexi = ityzl_SHOW_LOAD_LAYER();
             },
             success: function (data) {
                 if (data.code === 200) {
                     //初始化表格
                     initTransactionTable(data.body);
                 } else {
                     layer.msg(data.message, {icon: 2, time: 2000})
                     initTransactionTable(null);
                 }
                 ityzl_CLOSE_LOAD_LAYER(indexi);
             }
         })
    }




    //初始化表格数据
    function initTransactionTable(tableData) {
        var tableHeight = height - 40;
        var length = Math.floor(tableHeight / 32);
        if (length < 1) {
            length = 1;
        }
        var tableCellHeight = 32 * length-10;
        dataTable = $("#deviceTable").DataTable();
        dataTable.clear().draw();
        dataTable = $('#deviceTable').DataTable({
            order: [1, 'desc'],
            columnDefs: [
                {
                    orderable: false,
                    targets: [0,7]
                }
            ],
            columns: [
                {width: "7%", data: "name"},
                {width: "7%", data: "type",render:function (data) {
                        if(data == 0){
                            return'通用'
                        }else {
                            return '定制'
                        }
                    }},
                {width: "8%", data: "updateUser"},
                {width: "12%", data: "updateTime",render:function (data) {
                        if (data == null) {
                            return '';
                        } else {
                            var date = new Date(data);
                            var y = date.getFullYear();// 年
                            var MM = date.getMonth() + 1;// 月
                            MM = MM < 10 ? ('0' + MM) : MM;
                            var c = date.getDate();// 日
                            c = c < 10 ? ('0' + c) : c;
                            var h = date.getHours();// 时
                            h = h < 10 ? ('0' + h) : h;
                            var m = date.getMinutes();// 分
                            m = m < 10 ? ('0' + m) : m;
                            var s = date.getSeconds();// 秒
                            s = s < 10 ? ('0' + s) : s;
                            return y + '-' + MM + '-' + c + ' ' + h + ':' + m + ':' + s;
                        }
                    }},
                {width: "7%", data: "comPer"},
                {width: "7%", data: "warnPer"},
                {width: "12%", data: "alertPer"},
                {
                    width: "13%", data: "devIndex", render: function (data, type, full, meta) {
                        return "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);'  onclick='viewTransactionDetail(\"" + full.fsIndex + "\",2)' >修改</button>" +
                            "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);'  onclick='viewTransactionDetail(\"" + full.fsIndex + "\",3)' >删除</button>";
                    }
                }
            ],
            destroy: true,
            data: tableData,
            lengthChange: false,
            searching: true,
            dom: 'rtip',
            scrollY: tableCellHeight,
            // scrollX: true,
            lengthMenu: [length],
            language: {
                url: '/pqs9900/json/chine.json'
            },
            fnDrawCallback: function (oTable) {
                $("#deviceTable_info").addClass("fl");
                $("#deviceTable_paginate").addClass("fr");
                $("#deviceTable_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
                var table = $("#deviceTable").dataTable();
                $('#dataTable-btn').click(function (e) {
                    if ($("#changePage").val() && $("#changePage").val() > 0) {
                        var redirectpage = $("#changePage").val() - 1;
                    } else {
                        var redirectpage = 0;
                    }
                    table.fnPageChange(redirectpage);
                });
            }
        });
    }

    //搜索表中的数据
    function searchResult(value) {
        dataTable.search(value).draw();
    }

    function viewTransactionDetail(id, type) {
        passwordLocalIndentity(username,resetName,function () {
            if (type === 2) {
                //修改
                layer.open({
                    type: 2,
                    title: '修改流量策略',
                    maxmin: false,
                    area: ['25%', '80%'],
                    content: 'flowstrategyopen?type=2&fsIndex=' + id,
                    scrollbar: false,
                })
            } else if (type === 3) {
                layer.confirm('确定删除该策略吗?', function () {
                    //删除流量策略
                    let indexi;
                    $.ajax({
                        type: "get",
                        url: "/pqs9900/flowstrategy/delStrategy",
                        dataType: "json",
                        data: {fsIndex: id},
                        beforeSend: function () {
                            indexi = ityzl_SHOW_LOAD_LAYER();
                        },
                        success: function (data) {
                            if (data.code === 200) {
                                //初始化表格
                                layer.msg(data.message, {icon: 1, time: 2000})
                                refresh()
                            } else {
                                layer.msg(data.message, {icon: 2, time: 2000})
                            }
                            ityzl_CLOSE_LOAD_LAYER(indexi);
                        }
                    })
                })

            }
        })
    }

    //子页面刷新父页面
    window.reflah = function () {
        refresh()
    }
</script>
</body>
</html>
