<!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="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">设备号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="s_deviceid" id="deviceid" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">内置电池</label>
                            <div class="layui-input-inline">
                                <select name="NULL_solarvoltage" id="NULL_solarvoltage" lay-verify="required" lay-search="">
                                    <option value="-1">全部</option>
                                    <option value="0">无</option>
                                    <option value="1">是</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">范围</label>
                            <div class="layui-input-inline">
                                <input type="text" name="dgt_createtime" autocomplete="off" id="ID-laydate-start-date" class="layui-input" placeholder="开始日期">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="dlt_createtime" autocomplete="off" id="ID-laydate-end-date" class="layui-input" placeholder="结束日期">
                            </div>
                        </div>
						<div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-export-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <div class="layui-tab layui-tab-card" lay-filter="data-tab">
            <ul class="layui-tab-title">
                <li class="layui-this">数据表格</li>
                <li>姿态曲线</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                </div>
                <div class="layui-tab-item">
                    <div id="echarts-attitude" style="min-height:500px;padding: 10px"></div>
                </div>
            </div>
        </div>

    </div>
</div>
<script src="../js/lay-module/echarts/echartsTheme.js" charset="utf-8"></script>
<script src="../js/lay-module/echarts/echarts.js" charset="utf-8"></script>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script th:inline="javascript">
    layui.use(['form', 'table', 'laydate', 'element'], function () {
        var $ = layui.$;
        var form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            element = layui.element;
            
        var echartsAttitude = echarts.init(document.getElementById('echarts-attitude'), 'walden');
        var chartDataLoaded = false;

        var cfg_cols = [
            {field: 'deviceid', title: '设备号', width: 100},
            {field: 'createtime', title: '上报时间', templet: "<div>{{layui.util.toDateString(d.createtime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
            {field: 'roll', title: 'roll', templet: "<div>{{d.roll.toFixed(2)}}</div>"},
            {field: 'pitch', title: 'pitch', templet: "<div>{{d.pitch.toFixed(2)}}</div>"},
            {field: 'yaw', title: 'yaw', templet: "<div>{{d.yaw.toFixed(2)}}</div>"},
            {field: 'rssi', title: '信号强度'},
            {field: 'voltage', title: '电压(mV)'},
            {field: 'solarvoltage', title: '光伏电压(mV)'},
            {field: 'chargecurrency', title: '充电电流(mA)'},
            {field: 'chargewatt', title: '充电电量(Wh)', templet: "<div>{{d.chargewatt==null?'':d.chargewatt.toFixed(2)}}</div>"},
            {field: 'temperature', title: '温度(°C)'},
            {field: 'humidity', title: '湿度(%)'}
        ];
        
        form.render();

        laydate.render({
            elem: '#ID-laydate-start-date',
            type: 'datetime'
        });
        laydate.render({
            elem: '#ID-laydate-end-date',
            type: 'datetime'
        });

        table.render({
            elem: '#currentTableId',
            url: '/gnss/msg/status/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [
                cfg_cols
            ],
            limits: [10, 20, 50, 100, 150],
            limit: 10,
            page: true,
            skin: 'line'
        });

        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);

            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        form.on('submit(data-export-btn)', function (data) {
            var result =  $('#searchFrm').serialize();
            var u = "/gnss/msg/status/export?" + result;
            window.open(u, "_blank");
            return false;
        });
        
        element.on('tab(data-tab)', function(data){
            if (data.index == 1) {
                echartsAttitude.resize();
                
                if (!chartDataLoaded) {
                    var deviceId = $('#deviceid').val();
                    if (deviceId) {
                        loadChartData(deviceId);
                    } else {
                        layer.msg('请先在搜索框中输入设备号');
                    }
                }
            }
        });
        
        function loadChartData(deviceId) {
            $.ajax({
                url: '/gnss/msg/status/device_data',
                type: 'GET',
                data: {
                    deviceId: deviceId
                },
                success: function(res) {
                    if (res.code === 0 && res.data) {
                        showChart(res.data, res.originalData);
                        chartDataLoaded = true;
                    } else {
                        layer.msg('获取数据失败');
                    }
                },
                error: function() {
                    layer.msg('获取数据失败');
                }
            });
        }
        
        function showChart(smoothedData, originalData) {
            var t = [];
            var roll = [];
            var pitch = [];
            var yaw = [];
            var smoothRoll = [];
            var smoothPitch = [];
            var smoothYaw = [];
            
            for (var i = 0; i < originalData.length; i++) {
                t.push(originalData[i].createtime);
                roll.push(originalData[i].roll);
                pitch.push(originalData[i].pitch);
                yaw.push(originalData[i].yaw);
            }
            
            for (var i = 0; i < smoothedData.length; i++) {
                smoothRoll.push(smoothedData[i].roll);
                smoothPitch.push(smoothedData[i].pitch);
                smoothYaw.push(smoothedData[i].yaw);
            }
            
            var option = {
                title: {
                    text: '姿态数据曲线'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985'
                        }
                    }
                },
                legend: {
                    data: ['Roll', 'Pitch', 'Yaw', '平滑Roll', '平滑Pitch', '平滑Yaw']
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: t,
                        axisLabel: {
                            formatter: function(value) {
                                var date = new Date(value);
                                return date.getFullYear() + '-' +
                                       (date.getMonth() + 1).toString().padStart(2, '0') + '-' +
                                       date.getDate().toString().padStart(2, '0') + ' ' +
                                       date.getHours().toString().padStart(2, '0') + ':' +
                                       date.getMinutes().toString().padStart(2, '0') + ':' +
                                       date.getSeconds().toString().padStart(2, '0');
                            }
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: 'Roll',
                        type: 'line',
                        data: roll,
                        lineStyle: {
                            width: 1
                        }
                    },
                    {
                        name: 'Pitch',
                        type: 'line',
                        data: pitch,
                        lineStyle: {
                            width: 1
                        }
                    },
                    {
                        name: 'Yaw',
                        type: 'line',
                        data: yaw,
                        lineStyle: {
                            width: 1
                        }
                    },
                    {
                        name: '平滑Roll',
                        type: 'line',
                        data: smoothRoll,
                        lineStyle: {
                            width: 2
                        }
                    },
                    {
                        name: '平滑Pitch',
                        type: 'line',
                        data: smoothPitch,
                        lineStyle: {
                            width: 2
                        }
                    },
                    {
                        name: '平滑Yaw',
                        type: 'line',
                        data: smoothYaw,
                        lineStyle: {
                            width: 2
                        }
                    }
                ]
            };
            
            echartsAttitude.setOption(option);
            
            window.onresize = function() {
                echartsAttitude.resize();
            }
        }

    });
</script>

</body>
</html>