<!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="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="{{domain}}/resource/admin/mini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>

    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">接口名称</label>
            <div class="layui-input-block">
                <input type="text" name="apiname" lay-verify="required" lay-reqtext="接口名不能为空" placeholder="请输入新的接口名"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">接口路径</label>
            <div class="layui-input-block">
                <input type="text" name="apipath" lay-verify="required" lay-reqtext="父级对象不能为空" placeholder="接口所在的对象位置"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">接口类型</label>
            <div class="layui-input-block">
                <select name="apitype" lay-filter="apitype" lay-verify="required">
                    <option value="api">API 程序</option>
                    <option value="view">VIEW 程序</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">安装模板</label>
            {{^ maker}}
            <div class="layui-input-block">
                <select name="template" lay-verify="required" id="templates">
                    <option value="">模板列表未加载</option>
                </select>
            </div>
            {{/ maker}}
            {{# maker}}
            <div class="layui-input-block">
                <input type="text" name="template" style="color: #707070;" value="使用生成器模板代码" class="layui-input"
                    readonly>
            </div>
            {{/ maker}}
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入本接口的备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            // {{^ maker}}
            
            loadTemplate('api');

            function loadTemplate(value) {
                $.get('{{domain}}/admin/api/apis/template?type=' + value, function (res) {
                    if (res.data != '') {
                        // 将数据渲染至下拉框
                        $('#templates').html('');

                        for (var index in res.data) {
                            $('#templates').append('<option value="' + res.data[index].id +
                                '">' +
                                res.data[index].name + '</option>');
                        }
                        form.render('select');
                    }
                });
            }

            // 更换 API 和 VIEW
            form.on('select(apitype)', function (data) {
                loadTemplate(data.value);
            });

            // {{/ maker}}

            //监听提交
            form.on('submit(saveBtn)', function (data) {

                // 提交新增请求
                $.post('{{domain}}/admin/api/apis/addapi?from=add.html', {
                    apiname: data.field.apiname,
                    apipath: data.field.apipath,
                    apitype: data.field.apitype,
                    template: data.field.template,
                    apiabout: data.field.remark
                }, function (res) {
                    if (res.msg != '') {
                        layer.msg(res.msg + '...');
                    } else if (res.data == 'OK') {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.window.location.reload();
                    } else {
                        layer.msg('未知的程序错误...');
                    }
                }).error(function (res) {
                    layer.msg('操作异常：' + res.responseJSON.msg + '！');
                });

                return false;
            });

        });
    </script>
</body>

</html>