<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加数据 - {{ $table }}</title>
    <link rel="stylesheet" href="{{ asset('CmsProUi/component/pear/css/pear.css') }}">
    <link rel="stylesheet" href="{{ asset('CmsProUi/font-awesome/4.7.0/css/font-awesome.min.css') }}">
    <link rel="stylesheet" href="{{ asset('Admin/css/admin.css') }}">
    <link rel="stylesheet" href="{{ asset('Admin/css/variables.css') }}">
    <link rel="stylesheet" href="{{ asset('Admin/css/reset.css') }}">
</head>
<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body" style="padding:20px;">
            <form class="layui-form" lay-filter="addRowForm">
                @foreach($columns as $col)
                @if($col['Field'] === 'id' || $col['Extra'] === 'auto_increment')
                @continue
                @endif
                <div class="layui-form-item">
                    <label class="layui-form-label">{{ $col['Comment'] ?: $col['Field'] }}</label>
                    <div class="layui-input-block">
                        @if(in_array($col['Type'], ['text', 'mediumtext', 'longtext']))
                        <textarea name="{{ $col['Field'] }}" id="{{ $col['Field'] }}" placeholder="请输入{{ $col['Comment'] ?: $col['Field'] }}" class="layui-textarea"></textarea>
                        @else
                        <input type="text" name="{{ $col['Field'] }}" id="{{ $col['Field'] }}" placeholder="请输入{{ $col['Comment'] ?: $col['Field'] }}" class="layui-input">
                        @endif
                    </div>
                </div>
                @endforeach
                <div class="layui-form-item" style="text-align:center;margin-top:20px;">
                    <button class="layui-btn" lay-submit lay-filter="submitAdd">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </form>
        </div>
    </div>

    <script src="{{ asset('CmsProUi/component/layui/layui.js') }}"></script>
    <script src="{{ asset('CmsProUi/component/pear/pear.js') }}"></script>
    <script>
        layui.use(['form', 'jquery'], function() {
            var form = layui.form;
            var $ = layui.jquery;

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                },
                statusCode: {
                    401: function() {
                        layer.msg('登录已过期，请重新登录', {icon: 2, time: 1500}, function() {
                            window.location.href = '/admin/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search);
                        });
                    }
                }
            });

            var tableName = '{{ $table }}';
            var API = '/api/admin/database/' + tableName;

            form.on('submit(submitAdd)', function(data) {
                $.post(API + '/table-add', data.field, function(res) {
                    if (res.code === 0) {
                        layer.msg(res.message, {icon:1});
                        setTimeout(function() {
                            var idx = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(idx);
                        }, 500);
                    } else {
                        layer.msg(res.message, {icon:2});
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>
