<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <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.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" type="text/css" href="assets/js/vendor/viewer/viewer.min.css">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein" >
    <div class="layui-row ">
        <div class="layui-col-md4 layui-col-space2">
            <div class="layui-card">
                <div class="layui-card-body" id="configTable" style="padding: 15px;height:600px;">
                    <form class="layui-form layui-form-pane" lay-filter="dataForm">
                        <input type="hidden" name="configTableActivation" id="configTableActivation" value="0" title="不激活">
                        <div class="layui-form-item" id="actionDiv" style="display:none; text-align: center;width:100% !important;font-size: 17px">
                            <b>激活后可以修改 表单描述、备注内容。</b></br>
                            <b>不支持修改 表单编号、数据库表名、数据形式。</b>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">表单编号<span style="color: red">*</span></label>
                            <div class="layui-input-block">
                                <input type="text" id="configTableNumber" readonly  name="configTableNumber" class="layui-input" value="" placeholder="请输入表单编号"  maxlength="500">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">数据库表名<span style="color: red">*</span></label>
                            <div class="layui-input-block">
                                <input type="text" id="configTableCode" name="configTableCode" class="layui-input" value="" placeholder="请输入数据库表名"  maxlength="500">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">表单描述<span style="color: red">*</span></label>
                            <div class="layui-input-block">
                                <input type="text" id="configTableName" name="configTableName" class="layui-input" value="" placeholder="请输入表单描述"  maxlength="500">
                            </div>
                        </div>
                        <div class="layui-form-item" pane>
                            <label class="layui-form-label">数据形式<span style="color: red">*</span></label>
                            <div class="layui-input-block layui-form" >
                                <input type="radio" name="configTableMode" value="1" title="单数据(每个用户一条数据)">
                                <input type="radio" name="configTableMode" value="2" title="多数据(每个用户多条数据)">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text ">
                            <label class="layui-form-label">备注内容</label>
                            <div class="layui-input-block layui-text">
                                <textarea id="configTableRemark" name="configTableRemark" class="layui-textarea" placeholder="请输入备注内容"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item layui-layout-admin">
                            <div class="layui-input-block">
                                <div style="left: 0;margin-top: 70px;">
                                    <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">修改表单配置</button>
                                    <button type="button" class="layui-btn layui-btn-primary close">返回上级</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="layui-col-md8 layui-col-space2">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 15px;">
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                    <script type="text/html" id="headerToolBar">
                        <div class="layui-btn-group">
                            <button type="button" class="layui-btn layui-btn-sm" lay-event="saveEvent">
                                <i class="fa fa-lg fa-plus"></i> 添加字段
                            </button>
                            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="updateEvent">
                                <i class="fa fa-lg fa-edit"></i> 修改字段
                            </button>
                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="removeEvent">
                                <i class="fa fa-lg fa-trash"></i> 删除字段
                            </button>
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/js/vendor/wangEditor/wangEditor.min.js"></script>
<script src="assets/js/vendor/ckplayer/ckplayer/ckplayer.js"></script>
<script src="assets/js/vendor/viewer/viewer.min.js"></script>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'laytpl', 'table'], function(){
        var $ = layui.$;
        var form = layui.form;
        var $win = $(window);
        var laytpl = layui.laytpl;
        var table = layui.table;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var wangEditor = window.wangEditor;
        var wangEditorObj = {};
        var viewerObj = {};
        var height = $win.height() - 60;
        $("#configTable").css("height", height);
        var configTableId = top.restAjax.params(window.location.href).configTableId;

        function closeBox() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }

        var tableUrl = 'api/configtablefield/listpage';
        // 初始化表格
        function initTable() {
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path(tableUrl + "?configTableId=" + configTableId, []),
                width: admin.screen() > 1 ? '100%' : '',
                height: height,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                toolbar: '#headerToolBar',
                defaultToolbar: [],
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [
                    [
                        {type:'checkbox', fixed: 'left'},
                        {field: 'configTableFieldCode', width: 150, title: '列名称', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'configTableFieldName', width: 150, title: '字段名称', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'configTableFieldType', width: 100, title: '字段类型', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(rowData == "1") {
                                    return "文字";
                                }else  if(rowData == "2") {
                                    return "照片";
                                }else  if(rowData == "3") {
                                    return "音频";
                                }else  if(rowData == "4") {
                                    return "视频";
                                }else  if(rowData == "5") {
                                    return "链接";
                                }else  if(rowData == "6") {
                                    return "定位";
                                }else  if(rowData == "7") {
                                    return "文章";
                                }else  if(rowData == "8") {
                                    return "单选";
                                }else  if(rowData == "9") {
                                    return "多选";
                                }
                                return "-";
                            }
                        },
                        {field: 'configTableFieldNumber', width: 100, title: '数量', fixed: 'right', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return '<b>' + rowData + '</b>';
                            }
                        },
                        {field: 'configTableFieldOrder', width: 100, title: '排序', fixed: 'right', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return '<b>' + rowData + '</b>';
                            }
                        },
                        {field: 'configTableFieldMust', width: 100, title: '必填项',  fixed: 'right', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(rowData == 1) {
                                    return  '<button type="button"  class="layui-btn layui-btn-xs">必填</button>';
                                }else {
                                    return '<button type="button" class="layui-btn layui-btn-danger layui-btn-xs">非必填</button>';
                                }
                            }
                        },
                    ]
                ],
                page: true,
                parseData: function(data) {
                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }

        initTable();

        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                url: top.restAjax.path(tableUrl + "?configTableId=" + configTableId, []),
                page: {
                    curr: currentPage
                },
                height: height,
            });
        }

        // 初始化内容
        function initData() {
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/configtable/get/{configTableId}', [configTableId]), {}, null, function(code, data) {
                var dataFormData = {};
                for(var i in data) {
                    dataFormData[i] = data[i] +'';
                }
                if(dataFormData.configTableActivation == 1) {
                    $("#configTableNumber").attr("readonly", "readonly");
                    $("#configTableCode").attr("readonly", "readonly");
                    $("#configTableMode").attr("readonly", "readonly");
                    $('#actionDiv').show();
                }
                form.val('dataForm', dataFormData);
                form.render(null, 'dataForm');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(loadLayerIndex);
            });
        }
        initData();

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.put(top.restAjax.path('api/configtable/update/{configTableId}', [configTableId]), formData.field, null, function(code, data) {
                    top.layer.msg("修改表单配置成功");
                    window.location.reload();
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
            return false;
        });
        // 删除
        function removeData(ids) {
            top.dialog.msg(top.dataMessage.delete, {
                time: 0,
                btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                shade: 0.3,
                yes: function (index) {
                    top.dialog.close(index);
                    var layIndex;
                    top.restAjax.delete(top.restAjax.path('api/configtablefield/remove/{ids}', [ids]), {}, null, function (code, data) {
                        top.dialog.msg(top.dataMessage.deleteSuccess, {time: 1000});
                        reloadTable();
                    }, function (code, data) {
                        top.dialog.msg(data.msg);
                    }, function () {
                        layIndex = top.dialog.msg(top.dataMessage.deleting, {icon: 16, time: 0, shade: 0.3});
                    }, function () {
                        top.dialog.close(layIndex);
                    });
                }
            });
        }
        // 事件 - 增删改
        table.on('toolbar(dataTable)', function(obj) {
            var layEvent = obj.event;
            var checkStatus = table.checkStatus('dataTable');
            var checkDatas = checkStatus.data;
            if(layEvent === 'saveEvent') {
                if (configTableId == "noSave") {
                    top.layer.msg("请先保存左侧表单配置");
                    return;
                }
                if($("#configTableActivation").val() == "1") {
                    top.layer.msg("已激活的表单不允许再新增字段");
                    return;
                }
                top.layer.open({
                    type: 2,
                    title: false,
                    closeBtn: 0,
                    area: ['70%', '90%'],
                    shadeClose: true,
                    anim: 2,
                    content: top.restAjax.path('route/configtablefield/save?configTableId=' + configTableId, []),
                    end: function() {
                        reloadTable();
                    }
                });
            } else if(layEvent === 'updateEvent') {
                if(checkDatas.length === 0) {
                    top.dialog.msg(top.dataMessage.table.selectEdit);
                } else if(checkDatas.length > 1) {
                    top.dialog.msg(top.dataMessage.table.selectOneEdit);
                } else {
                    top.layer.open({
                        type: 2,
                        title: false,
                        closeBtn: 0,
                        area: ['70%', '90%'],
                        shadeClose: true,
                        anim: 2,
                        content: top.restAjax.path('route/configtablefield/update?configTableFieldId={configTableFieldId}', [checkDatas[0].configTableFieldId]),
                        end: function() {
                            reloadTable();
                        }
                    });
                }
            } else if(layEvent === 'removeEvent') {
                if($("#configTableActivation").val() == "1") {
                    top.layer.msg("已激活的表单不允许再删除字段");
                    return;
                }
                if(checkDatas.length === 0) {
                    top.dialog.msg(top.dataMessage.table.selectDelete);
                } else {
                    var ids = '';
                    for(var i = 0, item; item = checkDatas[i++];) {
                        if(i > 1) {
                            ids += '_';
                        }
                        ids += item['configTableFieldId'];
                    }
                    removeData(ids);
                }
            }
        });

        $('.close').on('click', function() {
            closeBox();
        });

        // 校验
        form.verify({
        });
    });
</script>
</body>
</html>