<!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="input_deviceid" class="layui-input" style="position:absolute;z-index:2;width:85%;"  value=""   autocomplete="off">
                                <select id="deviceid" lay-search="" lay-filter="device" >
                                    <option value="">全部</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">
                            <label class="layui-form-label">粒度</label>
                            <div class="layui-input-inline">
                                <select name="freqency" id="freqency" lay-search="">
                                    <option value="0">次</option>
                                    <option value="1">小时</option>
                                    <option value="2">自动</option>
                                </select>
                            </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">&#xe615;</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-gnss" style="min-height:300px;padding: 10px"></div>
                </div>
            </div>
        </div>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="cmd">命令行</a>
        </script>
    </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.$,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;
        var searchDeviceId = false;
        var echartsDevice = echarts.init(document.getElementById('echarts-gnss'), 'walden');
        var data_cols = [
            {field: 'deviceid', title: '设备号'},
            {field: 'model', title: '模块', templet: '#model'},
            {field: 'createtime', title: '产生时间', width:'18%', templet: "<div>{{layui.util.toDateString(d.createtime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
            {field: 'x', title: '东'},
            {field: 'y', title: '北'},
            {field: 'z', title: '天'},
            {field: 'status', title: '状态', templet: '#status'}
        ];
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        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/data/list_single_data',
            toolbar: '#toolbarDemo', //开启头部工具栏
            defaultToolbar: ['filter'],
            cols: [
                data_cols
            ],
            limits: [20, 50, 100, 200, 300],
            limit: 20,
            page: true,
            skin: 'line',
            done: function (result, curr, count) {
                if(searchDeviceId){
                    // console.log(result.data);
                    showChart(result.data)
                }
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            var deviceId = $('#input_deviceid').val();
            searchDeviceId = !isNaN(deviceId);
            //执行搜索重载
            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/data/single/export?" + result;
            window.open(u, "_blank");
            return false;
        });

        form.on('select(device)', function (data) {
            //对select和input进行监控渲染
            if (data.value != "") {
                $("#input_deviceid").val(data.value);
            }
        });

        //监听Tab切换，重新resize图表，否则显示不出来
        layui.element.on('tab(data-tab)', function(data){
            if (data.index == 1) {
                echartsDevice.resize();
            }
        });

        function showChart(chartData){
            var sortedData = chartData.sort((a, b) => new Date(a.createtime) - new Date(b.createtime));
            // 处理数据
            var times = sortedData.map(item => {
                let date = new Date(item.createtime);
                return date.toLocaleTimeString();
            });
            var xValues = sortedData.map(item => item.x);
            var yValues = sortedData.map(item => item.y);
            var zValues = sortedData.map(item => item.z);

            // 图表配置
            var option = {
                title: {
                    text: 'GNSS 单次解记录',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis',
                    formatter: function(params) {
                        let result = params[0].name + '<br/>';
                        params.forEach(param => {
                            result += param.seriesName + ': ' +
                                param.value.toFixed(2) + '<br/>';
                        });
                        return result;
                    }
                },
                legend: {
                    data: ['X坐标', 'Y坐标', 'Z坐标'],
                    top: '30px'
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    data: times,
                    axisLabel: {
                        rotate: 45
                    }
                },
                yAxis: {
                    type: 'value',
                    axisLabel: {
                        formatter: '{value}'
                    }
                },
                series: [
                    {
                        name: 'X坐标',
                        type: 'line',
                        data: xValues,
                        smooth: true,
                    },
                    {
                        name: 'Y坐标',
                        type: 'line',
                        data: yValues,
                        smooth: true,
                    },
                    {
                        name: 'Z坐标',
                        type: 'line',
                        data: zValues,
                        smooth: true,
                    }
                ]
            };
            echartsDevice.setOption(option);
        }
    });

</script>
<script type="text/javascript">
    const STATUS_CONFIG_F9P = {
        0: { text: '无B562', color: 'layui-bg-gray' },
        1: { text: '浮点解', color: 'layui-bg-gray' },
        2: { text: '固定解', color: 'layui-bg-green' },
        default: { text: '其他', color: 'layui-bg-gray' }
    };
    const STATUS_CONFIG_GGA = {
        4: { text: '固定解', color: 'layui-bg-green' },
        5: { text: '浮点解', color: 'layui-bg-gray' },
        default: { text: '其他', color: 'layui-bg-gray' }
    };

    function renderStatus(model,status) {
        if(model === 0){
            const config = STATUS_CONFIG_F9P[status] || STATUS_CONFIG_F9P.default;
            return `<span class="layui-badge ${config.color}">${config.text}</span>`;
        }
        else if(model === 1){
            const config = STATUS_CONFIG_GGA[status] || STATUS_CONFIG_GGA.default;
            return `<span class="layui-badge ${config.color}">${config.text}</span>`;
        }

    }
</script>
<script type="text/html" id="status">
    {{ renderStatus(d.model,d.status) }}
</script>


<script type="text/javascript">
    const MODEL_CONFIG = {
        0: { text: 'ECEF - F9P', color: 'layui-bg-orange' },
        1: { text: 'GGA - 博通', color: 'layui-bg-green' }
    };

    function renderModel(model) {
        const config = MODEL_CONFIG[model] || MODEL_CONFIG[0];
        return `<span class="layui-badge ${config.color}">${config.text}</span>`;
    }
</script>
<script type="text/html" id="model">
    {{ renderModel(d.model) }}
</script>

</body>
</html>