<!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">
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <div class="layui-card top-panel">
            <div class="layui-card-body">
                <input type="hidden" name="id" id="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="name" id="name" lay-verify="required" lay-reqtext="不能为空" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">指令类型</label>
                        <div class="layui-input-inline">
                            <select name="type" lay-filter="type">
                                <option value="0">GNSS</option>
                                <option value="1">DTU</option>
                                <option value="2">MPU</option>
                                <option value="3">DEBUG</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">指令</label>
                    <div class="layui-input-block">
                        <textarea name="content" id="content" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

    <script type="text/html" id="currentTableBar">
        <a class="layui-btn layui-btn-normal layui-btn-xs " lay-event="use">使用</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    </script>

    <script th:inline="none">
        layui.use(['form', 'table'], function () {
            var $ = layui.$,
                form = layui.form,
                table = layui.table;
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            /**
             * 初始化表单，要加上，不然刷新部分组件可能会不加载
             */
            form.render();

            table.render({
                elem: '#currentTableId',
                url: '/gnss/cmd/list',
                cols: [[
                    {field: 'name', title: '指令名称'},
                    {field: 'type', title: '类型',templet: '#typeTrans'},
                    {field: 'content', title: '指令',width:'50%'},
                    {title: '操作', toolbar: '#currentTableBar', align: "center", minWidth: 120}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'use') {
                    parent.setCmd(data.type, data.content);
                    parent.layer.close(iframeIndex);
                }
                else if (obj.event === 'delete') {
                    layer.confirm('确定删除'+data.name+"?", function(index){
                        $.ajax({
                            type:"POST",
                            url:"/gnss/cmd/delete",
                            data:{
                                'del_id':data.id
                            },
                            success: function (data) {
                                //data是cotroller相应处理函数的返回值
                                table.reload('currentTableId');
                            },
                            error: function () {
                                console.log("ajax error");
                            }
                        });
                        layer.close(index);
                    });
                }
            });

            form.on('submit(saveBtn)', function (data) {
                $.ajax({
                    type:"POST",
                    url:"/gnss/cmd/add",
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify(data.field),
                    success: function (result) {
                        table.reload('currentTableId');
                    },
                    error: function () {
                        console.log("ajax error");
                    }
                });
                return false;
            });
        });

    </script>

    <script type="text/html" id="typeTrans">
        {{#  if(d.type == 0){ }}
        <span>GNSS</span>
        {{# } else if(d.type == 1){ }}
        <span >DTU</span>
        {{# } else if(d.type == 2){ }}
        <span >MPU</span>
        {{# } else { }}
        <span >DEBUG</span>
        {{#  } }}
    </script>
</body>
</html>