<!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">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

    <div class="layui-form layuimini-form" th:object="${device}">
        <input type="hidden" name="id" id="id" th:field="*{id}">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label required">设备编号</label>
                <div class="layui-input-block">
                    <input type="text" name="deviceid" id="deviceid" th:field="*{deviceid}" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入设备编号" value="" class="layui-input" onblur="checkDeviceId()">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label required">使用状态</label>
                <div class="layui-input-inline">
                    <select name="opmode" id="opmode" th:field="*{opmode}" lay-verify="required" lay-search="">
                        <option value="0">正常</option>
                        <option value="1">维护</option>
                        <option value="2">停用</option>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label required">日志记录</label>
                <div class="layui-input-inline">
                    <select name="loggingmode" id="loggingmode" th:field="*{loggingmode}" lay-verify="required" lay-search="">
                        <option value="0">不记录(除了自检和停止消息)</option>
                        <option value="1">记录</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label required">产品型号</label>
                <div class="layui-input-inline">
                    <select name="model" id="model" th:field="*{model}" lay-verify="required" lay-search="" lay-filter="device_model">
                        <option value="0">G505</option>
                        <option value="1">G510</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline" pane="">
                <label class="layui-form-label">内置电池</label>
                <div class="layui-input-inline">
                    <select name="has_battery" id="has_battery" th:field="*{has_battery}" lay-verify="required" lay-search="">
                        <option value="0">否</option>
                        <option value="1">是</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline" pane="">
                <label class="layui-form-label">电压系数</label>
                <div class="layui-input-inline">
                    <input type="text" name="voltage_factor" id="voltage_factor" th:field="*{voltage_factor}" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">*0.1</div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label required">设备类型</label>
                <div class="layui-input-inline">
                    <select name="devicetype" id="devicetype" th:field="*{devicetype}" lay-verify="required" lay-search="" lay-filter="device_type">
                        <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-block">
                    <input type="number" name="parentid" id="parentid" th:field="*{parentid}" placeholder="请输入关联基准站编号" value="" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" id="ecef_div">
            <div class="layui-inline">
                <label class="layui-form-label">ECEF X</label>
                <div class="layui-input-block">
                    <input type="text" name="ecefx" id="ecefx" th:field="*{ecefx}" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">Y</label>
                <div class="layui-input-block">
                    <input type="text" name="ecefy" id="ecefy" th:field="*{ecefy}" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">Z</label>
                <div class="layui-input-block">
                    <input type="text" name="ecefz" id="ecefz" th:field="*{ecefz}" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label required">基本参数组</label>
                <div class="layui-input-inline">
                    <select name="group_id" id="group_id" th:field="*{group_id}" lay-verify="required" lay-search="">
                        <option th:each="item : ${gnss_group_list}" th:text="${item.name}" th:value="${item.id}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label required">解算参数组</label>
                <div class="layui-input-inline">
                    <select name="calc_group_id" id="calc_group_id" th:field="*{calc_group_id}" lay-verify="required" lay-search="">
                        <option th:each="item : ${gnss_group_calc_list}" th:text="${item.name}" th:value="${item.id}"></option>
                    </select>
                </div>
            </div>
        </div>

        <hr>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label required">所属部门</label>
                <div class="layui-input-inline">
                    <select name="tenantname" id="tenantname" th:field="*{tenantname}" lay-verify="required" lay-search="">
                        <option th:each="item : ${tenant_list}" th:text="${item.name}" th:value="${item.name}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">项目号</label>
                <div class="layui-input-block">
                    <input type="text" name="project_id" id="project_id" th:field="*{project_id}" placeholder="请输入项目编号" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-inline" th:if="${tenant_id==0}">
                <label class="layui-form-label">项目2号</label>
                <div class="layui-input-block">
                    <input type="text" name="project2_id" id="project2_id" th:field="*{project2_id}" placeholder="请输入项目编号" value="" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label required">桩号</label>
                <div class="layui-input-block">
                    <input type="text" name="sector" id="sector" th:field="*{sector}" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label required">监测点号</label>
                <div class="layui-input-block">
                    <input type="text" name="name" id="name" th:field="*{name}" value="" class="layui-input">
                </div>
            </div>
        </div>

        <hr>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">推送至</label>
                <div class="layui-input-inline">
                    <select name="fwd_group_id" id="fwd_group_id" th:field="*{fwd_group_id}" lay-search="">
                        <option value="不推送">不推送</option>
                        <option th:each="item : ${gnss_group_fwd_list}" th:text="${item.name}" th:value="${item.name}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline" th:if="${tenant_id==0}">
                <label class="layui-form-label">推送2</label>
                <div class="layui-input-inline">
                    <select name="fwd_group_id2" id="fwd_group_id2" th:field="*{fwd_group_id2}" lay-search="">
                        <option value="不推送">不推送</option>
                        <option th:each="item : ${gnss_group_fwd_list}" th:text="${item.name}" th:value="${item.name}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">推送名称</label>
                <div class="layui-input-block">
                    <input type="text" name="fwddeviceid" id="fwddeviceid" th:field="*{fwddeviceid}" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">初始位置(mm) 东</label>
                <div class="layui-input-block">
                    <input type="text" name="ipose" id="ipose" th:field="*{ipose}" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">北</label>
                <div class="layui-input-block">
                    <input type="text" name="iposn" id="iposn" th:field="*{iposn}" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">天</label>
                <div class="layui-input-block">
                    <input type="text" name="iposd" id="iposd" th:field="*{iposd}" class="layui-input">
                </div>
            </div>
        </div>
        <hr>
        <div class="layui-form-item" th:if="${role=='SUPER_ADMIN'}">
            <label class="layui-form-label">ICCID</label>
            <div class="layui-input-block">
                <input type="text" name="iccid" id="iccid" th:field="*{iccid}" class="layui-input" placeholder="请输入19-20位的ICCID号码">
            </div>
        </div>
        <div class="layui-form-item" th:if="${role=='SUPER_ADMIN'}">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" name="remark" id="remark" th:field="*{remark}" class="layui-input">
            </div>
        </div>
        <hr th:if="${role=='SUPER_ADMIN'}">
        <div class="layui-form-item">
            <div class="layui-input-block" style="float:right" >
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="initLocBtn">取初值</button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="contBtn">续数据</button>
            </div>
        </div>
    </div>

    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form'], function () {
            var form = layui.form,
                $ = layui.$;
            var iframeIndex = parent.layer.getFrameIndex(window.name);

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                $.ajax({
                    type:"POST",
                    url:"/gnss/device/update",
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(data.field),
                    success: function (result) {
                        parent.onDeviceCfgUpdated();
                        parent.layer.close(iframeIndex);
                    },
                    error: function () {
                        console.log("ajax error");
                        parent.layer.close(iframeIndex);
                    }
                });

                return false;
            });

            form.on('submit(initLocBtn)', function (data) {
                layer.confirm('确定重新取初值？', function(index){
                    $.ajax({
                        type:"POST",
                        url:"/gnss/device/init_loc",
                        data:{
                            'deviceid':$('#deviceid').val()
                        },
                        success: function (result) {
                            $('#ipose').val(result.ipose);
                            $('#iposn').val(result.iposn);
                            $('#iposd').val(result.iposd);
                        },
                        error: function () {
                            console.log("ajax error");
                        }
                    });
                    layer.close(index);
                });

                return false;
            });

            form.on('submit(contBtn)', function (data) {
                // 弹出输入框
                layer.prompt({
                    title: '请输入接续的设备编号和日期时间，用逗号隔开',        // 弹窗标题
                    formType: 0,               // 0-文本输入框
                    maxlength: 50,             // 最大输入长度
                    value: '',                 // 初始值
                    btn: ['确定', '取消']        // 按钮组
                }, function(value, index) {    // 确定按钮回调
                    // 处理输入结果
                    if(value) {
                        $.ajax({
                            type:"POST",
                            url:"/gnss/device/cont_loc",
                            data:{
                                'deviceid':$('#deviceid').val(),
                                'createtime':value
                            },
                            success: function (result) {
                                if(result.code == 0) {
                                    $('#ipose').val(result.ipose);
                                    $('#iposn').val(result.iposn);
                                    $('#iposd').val(result.iposd);
                                }
                                else{
                                    layer.alert(result.msg);
                                }
                            },
                            error: function () {
                                console.log("ajax error");
                            }
                        });
                    } else {
                        layer.msg('输入内容不能为空！', {icon: 2});
                    }
                    // 关闭当前弹窗
                    layer.close(index);
                });
                return false;
            });

            form.on('select(device_type)', function (data) {
                setEcefEditor();
            });
            form.on('select(device_model)', function (data) {
                setEcefEditor();
            });

        });


        function setEcefEditor(){
            var $ = layui.$;
            console.log($('#devicetype').val(), $('#model').val());
            if($('#devicetype').val()==1 && $('#model').val()==1){
                $('#ecef_div').show();
            }
            else {
                $('#ecef_div').hide();
            }
        }

        function checkDeviceId(){
            var $ = layui.$;
            var value = $("#deviceid").val();
            console.log(value);
            if(value.startsWith("6") || value.startsWith("7")){
                $("#has_battery").val('1');
                $("#has_battery").attr('disabled',true);
            }
            else{
                $("#has_battery").attr('disabled',false);
            }
            layui.form.render();
        }

    </script>

</body>
</html>