<%@ 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>
    <title>数据完整性手动操作</title>
    <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"/>
    <link rel="stylesheet"
          href="${ctx}/css/plugin/bootstrap/bootstrap.css">
    <link rel="stylesheet" href="${ctx}/js/plugin/layui/css/layui.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatables/buttons.dataTables.min.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatimepicker/bootstrap-datetimepicker.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/jquery/jquery-ui.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/fontawesome/font-awesome.min.css">
    <link rel="stylesheet" href="${ctx}/css/basic.css">
    <style>
        html, body {
            background-color: #F9F9F9 !important;
            font-size: 12px;
            overflow: hidden;
        }

        .btn {
            font-size: 12px
        }

        .buttons-html5 {
            height: 22px;
        }

        a {
            color: #003078;
            text-decoration: underline;
        }

        a:link, a:visited, a:hover, a:active {
            text-decoration: underline;
        }

        .dt-buttons a {
            text-decoration: none;
        }

    </style>
</head>
<body>
<div class="container-fluid fr" style="margin-bottom: 5px;">
    <div class="form-inline header" style="margin-top:2px;position: relative;z-index: 99">
        <button class="btn btn-primary" id="calOverLimit" style="margin-right: 200px">重新计算监测点限值</button>

        <button type="button" class="btn themeBGColor" id="exportCsvRangTime">导出暂态波形csv数据</button>

        <label class="form-group">时间间隔：</label>
        <div class="form-group has-feedback">
            <select class="form-control" id="interval">
                <option value="周">周</option>
                <option value="月份">月份</option>
                <option value="自定义">自定义</option>
            </select>
        </div>
        <div class="form-group">
            <input type="text" class="form-control form_datetime con"
                   id="startTime" style="width: 88px">
        </div>
        &nbsp;—
        <div class="form-group">
            <input type="text" class="form-control form_datetime con"
                   id="endTime" style="width: 88px">
        </div>

        <button type="button" class="njcn btn themeBGColor" id="before">
            <i class="glyphicon glyphicon-backward"></i>
        </button>
        <div class="form-group" id="present">
            <button type="button" class="njcn btn themeBGColor">当前</button>
        </div>

        <button type="button" class="njcn btn themeBGColor" id="next">
            <span class="glyphicon glyphicon-forward" aria-hidden="true"></span>
        </button>
        <div class="form-group" style="margin-left: 10px;margin-right: 5px">
            <button type="button" id="start" class="njcn btn themeBGColor">
                开始执行
            </button>
        </div>
    </div>
</div>
<input type="hidden" id="obj_Search"
       name="obj_Search">
<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-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>

    $(function () {
        $('body').on('click', '#start', function () {
            var startTime = $('#startTime').val()
            var endTime = $('#endTime').val()
            if (startTime == null || endTime == null || startTime == '' || endTime == '') {
                layer.msg('请选择时间')
                return;
            }
            var time_arr = diff(startTime, endTime)
            var ye = time_arr[0]
            var mo = time_arr[1]
            var da = time_arr[2]

            if (ye > 0 || mo > 1 || da > 31) {
                layer.msg('数据量过大，时间范围不可超过一个月!')
                return false;
            }
            var new_points = []
            var points = getTreeCheckBoxData()
            if(points.length!=0){
                points.map(function (item) {
                    if(item.level == 4){
                        new_points.push(item)
                    }
                })
            }

            if (new_points.length == 0) {
                layer.msg('请至少选择一个监测点');
                return;
            }
            if (new_points.length > 1) {
                layer.msg('请选择唯一监测点');
                return;
            }
            var lineIndex = points[0].nodeIndex //2231


            $.ajax({
                method: "get",
                url: "/pqs9900/csv/startHistoryTask",
                data: {
                    lineNo: lineIndex,
                    beginTime: startTime,
                    endTime: endTime
                },
                beforeSend: function () {
                    i = ityzl_SHOW_LOAD_LAYER();
                },
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg("执行成功", {icon: 1, time: 2000})

                        //***********关闭loading
                        ityzl_CLOSE_LOAD_LAYER(i);
                    } else {
                        layer.msg("执行失败!", {icon: 5, time: 2000}, function () {
                            parent.layer.closeAll()
                        });
                        //***********关闭loading
                        ityzl_CLOSE_LOAD_LAYER(i);
                    }

                }
            });
        })
    })

    //获取设备书选择的监测点id
    function getTreeCheckBoxData() {
        return parent.getTreeSelected(1);
    }


    var mons = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    function isLeapYear(year) {
        var r = year / 100
        if (r === parseInt(r)) {
            r = year / 400
            return r === parseInt(r)
        }
        r = year / 4
        if (r === parseInt(r)) {
            return true
        }
        return false
    }

    function getDaysOfMonth(month, year) {
        if (month === 2 && isLeapYear(year)) {
            return 29
        }
        return mons[month]
    }

    function getMonthsOfYear(year) {
        if (isLeapYear(year)) {
            return 366
        }
        return 365
    }

    function diff(s1, s2) {
        var arr1 = s1.split('-').map(Number)
        var arr2 = s2.split('-').map(Number)
        var [year, month, day] = arr2.map((n, i) => n - arr1[i])
        if (day < 0) {
            day += getDaysOfMonth(arr2[1], arr2[0])
            month--
        }
        if (month < 0) {
            month += getMonthsOfYear(arr2[0])
            year--
        }
        return [year, month, day]
    }


    $("#calOverLimit").click(function () {
        layer.confirm('是否确认重新计算限值？',function(){
            $.ajax({
                method : "get",
                url : "/pqs9900/device/reCalOverLimit",
                success : function(data) {
                    if(data.code==200){
                        layer.msg('重新计算限值成功,数量!'+data.body,{icon:1,time:1000});
                        loadReport();
                    }else{
                        layer.msg('重新计算限值失败！',{icon:2,time:1000});
                    }
                }
            })
        })
    })


    $("#exportCsvRangTime").click(function () {
        layer.open({
            type: 2,
            title: '导出暂态波形详细数据',
            maxmin: false,
            shadeClose: true, // 点击遮罩关闭层
            area: ['30%', '70%'],
            content: '/pqs9900/business/timerange',
            scrollbar: false
        });

    })



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