<!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_t.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" th:if="${tenant_id==0}">
                            <label class="layui-form-label">所属部门</label>
                            <div class="layui-input-inline">
                                <select name="n_t.tenantid" id="tenantid" lay-search="" lay-filter="tenant">
                                    <option value="">全部</option>
                                    <option th:each="item : ${tenant_list}" th:text="${item.name}" th:value="${item.id}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">所属项目</label>
                            <div class="layui-input-inline">
                                <select name="sl_d.project_id" id="projectid" lay-search="" lay-filter="project">
                                    <option value="">全部</option>
                                    <option th:each="item : ${project_list}" th:text="${item.project_id}" th:value="${item.project_id}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">所属基站</label>
                            <div class="layui-input-inline">
                                <select name="s_d.parentid" id="parentid" lay-search="" lay-filter="ref_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_t.createtime" autocomplete="off" id="ID-laydate-start-date" class="layui-input" placeholder="开始日期">
							</div>
							<div class="layui-input-inline">
								<input type="text" name="dlt_t.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="edit">状态修改</a>
<!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>-->
<!--            <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: 'createtime', title: '产生时间', width:'18%', templet: "<div>{{layui.util.toDateString(d.createtime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
            {field: 'updatetime', title: '上报时间', width:'18%', templet: "<div>{{d.updatetime==null?'':layui.util.toDateString(d.updatetime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
            {field: 'b562e', title: '原始东', templet: "<div>{{d.b562e.toFixed(2)}}</div>"},
            {field: 'b562n', title: '原始北', templet: "<div>{{d.b562n.toFixed(2)}}</div>"},
            {field: 'b562d', title: '原始天', templet: "<div>{{d.b562d.toFixed(2)}}</div>"},
            {field: 'rpose', title: '滤波东', templet: "<div>{{d.rpose==null?'':d.rpose.toFixed(2)}}</div>"},
            {field: 'rposn', title: '滤波北', templet: "<div>{{d.rposn==null?'':d.rposn.toFixed(2)}}</div>"},
            {field: 'rposd', title: '滤波天', templet: "<div>{{d.rposd==null?'':d.rposd.toFixed(2)}}</div>"},
            {field: 'r9250e', title: '拟合东', templet: "<div>{{d.r9250e==null?'':d.r9250e.toFixed(2)}}</div>"},
            {field: 'r9250n', title: '拟合北', templet: "<div>{{d.r9250n==null?'':d.r9250n.toFixed(2)}}</div>"},
            {field: 'r9250d', title: '拟合天', templet: "<div>{{d.r9250d==null?'':d.r9250d.toFixed(2)}}</div>"},
            {field: 'enabled', title: '有效',templet: '#enabledTrans'},
            {field: 'stabled', title: '稳定',templet: '#stabledTrans'},
            {title: '操作', toolbar: '#currentTableBar', fixed: "right", width: 100}
        ];
        if([[${role}]] != "SUPER_ADMIN") {
            data_cols[9].hide = true;
            data_cols[10].hide = true;
            data_cols[11].hide = true;
            data_cols[13].hide = true;
            data_cols[14].hide = true;
        }
		/**
		 * 初始化表单，要加上，不然刷新部分组件可能会不加载
		 */
		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_calc',
            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){
                    if([[${role}]] != "SUPER_ADMIN"){
                        showChart(result.data);
                    }
                    else{
                        showChart2(result.data);
                    }
                }
            }
        });

        // 监听工具条事件
        table.on('tool(currentTableFilter)', function(obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.confirm('确定要修改状态吗？', {
                    btn: ['确定', '取消']
                }, function(index) {
                    $.ajax({
                        url: '/page/gnssUpdateEnabled',
                        type: 'POST',
                        data: {
                            id: data.id,
                        },
                        success: function(res) {
                            table.reload('currentTableId');
                            layer.close(index);
                        },
                        error: function(xhr) {
                            layer.msg('操作失败');
                            layer.close(index);
                        }
                    });
                });
            }
        });

        // 监听搜索操作
        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/calc/export?" + result;
            window.open(u, "_blank");
            return false;
        });

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

        // 所属部门下拉框改变，修改项目下拉框
        form.on('select(tenant)', function (data) {
            //console.log(data.value);
            $.ajax({
                type:"GET",
                url:"/gnss/device/q_project",
                data:{
                    'tenantid':data.value
                },
                success: function(result) {
                    $('#projectid').empty();
                    $('#projectid').append(new Option("全部", ""));
                    $('#parentid').empty();
                    $('#parentid').append(new Option("全部", ""));
                    $('#deviceid').empty();
                    //console.log(result);
                    $.each(result.data, function (index, item) {
                        $('#projectid')
                            .append(new Option(item.project_id, item.project_id));
                    });
                    layui.form.render("select");
                },
                error: function () {
                    console.log("ajax error");
                }
            });
        });
        // 项目下拉框改变，修改基站下拉框
        form.on('select(project)', function (data) {
            $.ajax({
                type:"GET",
                url:"/gnss/device/q_parentid",
                data:{
                    'projectid':data.value
                },
                success: function(result) {
                    $('#parentid').empty();
                    $('#parentid').append(new Option("全部", ""));
                    //console.log(result);
                    $.each(result.data, function (index, item) {
                        $('#parentid').append(new Option(item.parentid, item.parentid));
                    });
                    layui.form.render("select");
                },
                error: function () {
                    console.log("ajax error");
                }
            });
        });
        //基站下拉框改变，修改设备号下拉框
        form.on('select(ref_device)', function (data) {
            $.ajax({
                type:"GET",
                url:"/gnss/device/q_deviceid",
                data:{
                    'parentid':data.value
                },
                success: function(result) {
                    $('#deviceid').empty();
                    //console.log(result);
                    $.each(result.data, function (index, item) {
                        $('#deviceid').append(new Option(item.deviceid, item.deviceid));
                    });
                    layui.form.render("select");
                },
                error: function () {
                    console.log("ajax error");
                }
            });
        });

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

        function showChart(chartData){
            var t = [];
            var e = [];
            var n = [];
            var d = [];
            var fe = [];
            var fn = [];
            var fd = [];
            var count = 0;
            var isBaseSet = false;
            var baseE = 0;
            var baseN = 0;
            var baseD = 0;
            var baseFE = 0;
            var baseFN = 0;
            var baseFD = 0;

            for(var i=chartData.length-1; i>=0; i--){
                if(chartData[i].rpose!=null) {
                    if(isBaseSet) {
                        t[count] = chartData[i].createtime;
                        e[count] = Number(chartData[i].b562e)-baseE;
                        n[count] = Number(chartData[i].b562n)-baseN;
                        d[count] = Number(chartData[i].b562d)-baseD;
                        fe[count] = Number(chartData[i].rpose) - baseFE;
                        fn[count] = Number(chartData[i].rposn) - baseFN;
                        fd[count] = Number(chartData[i].rposd) - baseFD;
                        count++;
                    }
                    else{
                        isBaseSet = true;
                        baseE = Number(chartData[i].b562e);
                        baseN = Number(chartData[i].b562n);
                        baseD = Number(chartData[i].b562d);
                        baseFE = Number(chartData[i].rpose);
                        baseFN = Number(chartData[i].rposn);
                        baseFD = Number(chartData[i].rposd);
                    }
                }
            }

            var optionDevice = {
                title: {
                    text: '位移曲线'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985'
                        }
                    }
                },
                legend: {
                    data: ['东', '北', '天','平滑东', '平滑北', '平滑天']
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: t
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '东',
                        type: 'line',
                        data: e
                    },
                    {
                        name: '北',
                        type: 'line',
                        data: n
                    },
                    {
                        name: '天',
                        type: 'line',
                        data: d
                    },
                    {
                        name: '平滑东',
                        type: 'line',
                        data: fe
                    },
                    {
                        name: '平滑北',
                        type: 'line',
                        data: fn
                    },
                    {
                        name: '平滑天',
                        type: 'line',
                        data: fd
                    }
                ]
            };
            echartsDevice.setOption(optionDevice);

            // echarts 窗口缩放自适应
            window.onresize = function () {
                echartsDevice.resize();
            }
        }

        function showChart2(chartData){
            var t = [];
            var e = [];
            var n = [];
            var d = [];
            var fe = [];
            var fn = [];
            var fd = [];
            var fe2 = []; //拟合滤波
            var fn2 = [];
            var fu2 = [];
            var count = 0;
            var isBaseSet = false;
            var baseE = 0;
            var baseN = 0;
            var baseD = 0;
            var baseFE = 0;
            var baseFN = 0;
            var baseFD = 0;
            var baseFE2 = 0;
            var baseFN2 = 0;
            var baseFU2 = 0;

            for(var i=chartData.length-1; i>=0; i--){
                if(chartData[i].rpose!=null) {
                    if(chartData[i].r9250e==null){
                        chartData[i].r9250e=0;
                        chartData[i].r9250n=0;
                        chartData[i].r9250d=0;
                    }
                    if(isBaseSet) {
                        t[count] = chartData[i].createtime;
                        e[count] = Number(chartData[i].b562e)-baseE;
                        n[count] = Number(chartData[i].b562n)-baseN;
                        d[count] = Number(chartData[i].b562d)-baseD;
                        fe[count] = Number(chartData[i].rpose) - baseFE;
                        fn[count] = Number(chartData[i].rposn) - baseFN;
                        fd[count] = Number(chartData[i].rposd) - baseFD;
                        fe2[count] = Number(chartData[i].r9250e) - baseFE2;
                        fn2[count] = Number(chartData[i].r9250n) - baseFN2;
                        fu2[count] = Number(chartData[i].r9250d) - baseFU2;
                        count++;
                    }
                    else{
                        isBaseSet = true;
                        baseE = Number(chartData[i].b562e);
                        baseN = Number(chartData[i].b562n);
                        baseD = Number(chartData[i].b562d);
                        baseFE = Number(chartData[i].rpose);
                        baseFN = Number(chartData[i].rposn);
                        baseFD = Number(chartData[i].rposd);
                        baseFE2 = Number(chartData[i].r9250e);
                        baseFN2 = Number(chartData[i].r9250n);
                        baseFU2 = Number(chartData[i].r9250d);
                    }
                }
            }

            var optionDevice = {
                title: {
                    text: '位移曲线'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985'
                        }
                    }
                },
                legend: {
                    data: ['东', '北', '天','平滑东', '平滑北', '平滑天','拟合东', '拟合北', '拟合天']
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: t
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '东',
                        type: 'line',
                        data: e
                    },
                    {
                        name: '北',
                        type: 'line',
                        data: n
                    },
                    {
                        name: '天',
                        type: 'line',
                        data: d
                    },
                    {
                        name: '平滑东',
                        type: 'line',
                        data: fe
                    },
                    {
                        name: '平滑北',
                        type: 'line',
                        data: fn
                    },
                    {
                        name: '平滑天',
                        type: 'line',
                        data: fd
                    },
                    {
                        name: '拟合东',
                        type: 'line',
                        data: fe2
                    },
                    {
                        name: '拟合北',
                        type: 'line',
                        data: fn2
                    },
                    {
                        name: '拟合天',
                        type: 'line',
                        data: fu2
                    }
                ]
            };
            echartsDevice.setOption(optionDevice);

            // echarts 窗口缩放自适应
            window.onresize = function () {
                echartsDevice.resize();
            }
        }
    });

</script>

<script type="text/html" id="enabledTrans">
    {{#  if(d.enabled == false){ }}
    <span class="layui-badge layui-bg-gray">无效</span>
    {{# } else { }}
    <span class="layui-badge layui-bg-green">有效</span>
    {{#  } }}
</script>

<script type="text/html" id="stabledTrans">
    {{#  if(d.stabled == false){ }}
    <span>否</span>
    {{# } else if(d.stabled == true){ }}
    <span>是</span>
    {{# } else { }}
    <span></span>
    {{#  } }}
</script>

</body>
</html>