<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>设备总览</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .top-panel {
            border: 1px solid #eceff9;
            border-radius: 5px;
            text-align: center;
        }
        .top-panel > .layui-card-body{
            height: 60px;
        }
        .top-panel-number{
            line-height:60px;
            font-size: 30px;
            border-right:1px solid #eceff9;
        }
        .top-panel-tips{
            line-height:30px;
            font-size: 12px
        }
    </style>
</head>
<body>
<div class="layuimini-main">

    <div class="layui-row layui-col-space30">
        <div class="layui-col-xs12 layui-col-md6">
            <div class="layui-card top-panel">
                <div class="layui-card-header">设备数量</div>
                <div class="layui-card-body">
                    <div class="layui-row layui-col-space5">
                        <div class="layui-col-xs9 layui-col-md8 top-panel-number">
                            <a style="color: #1aa094" th:text="${deviceOnlineNum}">2,020</a><br>
                        </div>
                        <div class="layui-col-xs3 layui-col-md4">
                            掉线数 <a style="color: #bd3004" th:text="${deviceOfflineNum}">0</a><br>
                            装机量 <a style="color: #000000" th:text="${deviceDeployedNum}">2020</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-xs12 layui-col-md6">
            <div class="layui-card top-panel">
                <div class="layui-card-header">告警统计</div>
                <div class="layui-card-body">
                    <div class="layui-row layui-col-space5">
                        <div class="layui-col-xs9 layui-col-md8 top-panel-number">
                            <a style="color: #f6c102" th:text="${warningTotalNum}">20</a><br>
                        </div>
                        <div class="layui-col-xs3 layui-col-md4">
                            严重 <a style="color: #bd3004" th:text="${warning2Num}">5</a><br>
                            一般 <a style="color: #f6c102" th:text="${warning1Num}">20</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-row layui-col-space15">
        <div class="layui-card top-panel">
            <div id="map-container" class="layui-card-body" style="height: 85vh;weight :100vw"></div>
        </div>
    </div>

</div>


<script th:inline="javascript">
    function initialize() {
        var map = new BMapGL.Map("map-container");            // 创建Map实例
        var point = new BMapGL.Point(116.404, 39.915);
        map.centerAndZoom(point, 7);
        map.enableScrollWheelZoom(true);
        //map.setMapType(BMAP_SATELLITE_MAP);
        var centerLat = 0;
        var centerLon = 0;
        var deviceList=
            [
                [# th:each="device : ${deviceList}"]
                    {deviceid:[[${device.deviceid}]],
                        latitude:[[${device.latitude}]],
                        longitude:[[${device.longitude}]],
                        warning:[[${device.warning}]]},
                    [/]
            ];
        let batch_id = 0;
        var convertor = new BMapGL.Convertor();

        var loc_green = new BMapGL.Icon("../images/loc1_green.png", new BMapGL.Size(18, 24));
        var loc_red = new BMapGL.Icon("../images/loc1_red.png", new BMapGL.Size(18, 24));
        var loc_orange = new BMapGL.Icon("../images/loc1_orange.png", new BMapGL.Size(18, 24));

        //console.log(deviceList);
        for(var i=0; i<deviceList.length; i++) {
            centerLat += deviceList[i].latitude;
            centerLon += deviceList[i].longitude;
        }
        if(deviceList.length>0) {
            centerLat = centerLat/deviceList.length;
            centerLon = centerLon/deviceList.length;
            map.setCenter(new BMapGL.Point(centerLon,centerLat));
        }

        translateCallback = function (data){
            if(data.status === 0) {
                var i=0;
                for(; i<data.points.length; i++) {
                    if(deviceList[batch_id+i].warning == 2) {
                        map.addOverlay(new BMapGL.Marker(data.points[i], {
                            icon: loc_red
                        }));
                    }
                    else if(deviceList[batch_id+i].warning == 1) {
                        map.addOverlay(new BMapGL.Marker(data.points[i], {
                            icon: loc_orange
                        }));
                    }
                    else {
                        map.addOverlay(new BMapGL.Marker(data.points[i], {
                            icon: loc_green
                        }));
                    }

                    if(deviceList[batch_id+i].warning != 0){
                        var label = new BMapGL.Label(deviceList[batch_id+i].deviceid,
                            {
                                position: data.points[i], // 指定文本标注所在的地理位置
                                offset: new BMapGL.Size(10, 10) // 设置文本偏移量
                            });             // 创建文本标注对象
                        map.addOverlay(label);
                    }

                }

            }
            batch_id+=10;
            // 函数内定时器的回调函数会继续调用 timer()
            if(batch_id<deviceList.length) {
                setTimeout(() => {
                    timer();
                }, 500);
            }
        }

        //每秒转换10个坐标，否则会受百度并发限制
        function timer() {
            var pointArr = [];
            var count = batch_id+10;
            var totalNum = deviceList.length;
            if(count>totalNum) count=totalNum;

            for (var i = batch_id; i < count; i++) {
                point = new BMapGL.Point(deviceList[i].longitude, deviceList[i].latitude);
                pointArr.push(point);
            }

            convertor.translate(pointArr, 1, 5, translateCallback);

        }
        // 启动函数
        timer();
    }


    function loadScript() {
        var script = document.createElement("script");
        script.src = "https://api.map.baidu.com/api?v=1.0&type=webgl&ak=4ef46ba62e8fba67b53a0becca4d05da&callback=initialize";
        document.body.appendChild(script);
    }

    loadScript();

</script>

</body>
</html>