<!doctype html>
<html lang="en" 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">
    <style>
        .selectColor{
            width:93%;
            float:left;
            cursor:pointer;
        }
        .layui-colorpicker-trigger-bgcolor {
            display: none !important;
        }
        .layui-colorpicker{
            display: none !important;
        }
    </style>
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein">
    <div class="layui-card">
       <!-- <div class="layui-card-header">
            <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
              <a class="close" href="javascript:void(0);">上级列表</a><span lay-separator="">/</span>
              <a href="javascript:void(0);"><cite>新增内容</cite></a>
            </span>
        </div>-->
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form layui-form-pane" lay-filter="dataForm">
                <input type="hidden" id="templateAreaFile" name="templateAreaFile">
                <div class="layui-form-item layui-row">
                    <div class="layui-col-lg12">
                        <label class="layui-form-label">字段名称<span style="color: red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="templateAreaName" name="templateAreaName" class="layui-input" value="" placeholder="请输入字段名称例如 姓名、年龄、性别" >
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-lg12">
                        <label class="layui-form-label">字段标识<span style="color: red">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="templateAreaSource" name="templateAreaSource" class="layui-input" value="" placeholder="请输入字段标识例如 name、age、sex" >
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">填充方式<span style="color: red">*</span></label>
                    <div class="layui-input-block" >
                        <input type="radio" name="templateAreaType" value="1" title="占位区域【使用区域配置内容填充】" />
                        <input type="radio" name="templateAreaType" value="2" title="填充区域【使用外部传入内容填充】" />
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">区域颜色<span style="color: red">*</span></label>
                    <div class="layui-input-block" >
                        <input type="radio" name="templateAreaColor" value="orange" checked title="橙色" />
                        <input type="radio" name="templateAreaColor" value="red" title="红色" />
                        <input type="radio" name="templateAreaColor" value="blue" title="蓝色" />
                        <input type="radio" name="templateAreaColor" value="green" title="绿色" />
                        <input type="radio" name="templateAreaColor" value="purple" title="紫色" />
                        <input type="radio" name="templateAreaColor" value="yellow" title="黄色" />
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">区域类型<span style="color: red">*</span></label>
                    <div class="layui-input-block" >
                        <input type="radio" name="templateAreaServerLink" lay-filter="templateAreaServerLinkFilter" value="1" title="文字" />
                        <input type="radio" name="templateAreaServerLink" lay-filter="templateAreaServerLinkFilter" value="2" title="图片" />
                        <input type="radio" name="templateAreaServerLink" lay-filter="templateAreaServerLinkFilter" value="3" title="条形码" />
                        <input type="radio" name="templateAreaServerLink" lay-filter="templateAreaServerLinkFilter" value="4" title="二维码" />
                        <input type="radio" name="templateAreaServerLink" lay-filter="templateAreaServerLinkFilter" value="5" title="地图位置" />
                        <input type="radio" name="templateAreaServerLink" lay-filter="templateAreaServerLinkFilter" value="6" title="小程序二维码" />
                    </div>
                </div>
                <div id="areaFontValueDiv"></div>
                <script id="areaFontValueScript" type="text/html">
                    {{# if(d != '2') { }}
                        <div class="layui-form-item layui-row {{ d == 1 ? 'layui-form-text' : ''}}">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">{{ d == 3 ? '条形码数据' : d == 4 ? '二维码数据' : d == 5 ? '地图位置' : d == 6 ? '携带参数' : '示例文字'}}<span style="color: red">*</span></label>
                                <div class="layui-input-block">
                                    {{# if(d == '1') { }}
                                        <textarea id="templateAreaFontValue" name="templateAreaFontValue" class="layui-textarea" placeholder="请输入文字例值"></textarea>
                                    {{# } else { }}
                                        <input type="text" id="templateAreaFontValue" name="templateAreaFontValue" class="layui-input" value="" placeholder="请输入{{ d == 3 ? '条形码数据' : d == 4 ? '二维码数据' : d == 5 ? '坐标或地名' : d == 6 ? '携带参数串appid=xx&secret=xx&path=xx' : '文字例值'}}" >
                                    {{# }   }}
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-row">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">{{ d == 3 ? '条形码' : d == 4 ? '二维码' : d == 5 ? '地图' : d == 6 ? '二维码' : '文字'}}颜色<span style="color: red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="text" readonly id="templateAreaFontColor" name="templateAreaFontColor" class="layui-input dataInputRowFilter selectColor" value="rgb(0, 0, 0)" placeholder="" >
                                    <div id="test-all" style="height:38px;width:5.5%;float:left;background-color:rgb(0, 0, 0)"></div>
                                </div>
                            </div>
                        </div>
                        {{# if(d == '1') { }}
                        <div class="layui-form-item layui-row" pane>
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">文字字体<span style="color: red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="radio" name="templateAreaFontFamily"  checked value="黑体" title="黑体" />
                                    <input type="radio" name="templateAreaFontFamily"  value="宋体" title="宋体" />
                                    <input type="radio" name="templateAreaFontFamily"  value="楷体" title="楷体" />
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-row" pane>
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">文字加粗<span style="color: red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="radio" name="templateAreaFontBold" checked value="0" title="正常" />
                                    <input type="radio" name="templateAreaFontBold"  value="1" title="加粗" />
                                    <input type="radio" name="templateAreaFontBold"  value="2" title="斜体" />
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-row">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">字体大小<span style="color: red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="number" id="templateAreaFontSize" name="templateAreaFontSize" value="15" class="layui-input" placeholder="请输入字体大小" >
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-row">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">限制长度<span style="color: red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="number" id="templateAreaFontLength" name="templateAreaFontLength" value="0" class="layui-input" placeholder="请输入限制长度" >
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-row" pane>
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">文字位置<span style="color: red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="radio" name="templateAreaFontCenter" checked value="1" title="居左" />
                                    <input type="radio" name="templateAreaFontCenter"  value="2" title="居中" />
                                    <input type="radio" name="templateAreaFontCenter"  value="3" title="居右" />
                                </div>
                            </div>
                        </div>
                        {{# } }}
                    {{# } }}
                </script>
                <div id="templateAreaFileFileBox"></div>
                <script id="templateAreaFileFileDownload" type="text/html">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">示例图片<span style="color: red">*</span></label>
                    <div class="layui-input-block">

                            {{# var fileName = 'templateAreaFile'; }}
                            {{# if(d[fileName].length > 0) { }}
                            {{#    var files = d[fileName];}}
                            {{#    for(var i = 0, item = files[i]; item = files[i++];) { }}
                            <div class="upload-image-box">
                                <span class="upload-image-span">
                                    <img src="route/file/download/false/{{item.fileId}}" align="加载失败">
                                </span>
                                <a class="layui-btn layui-btn-xs layui-btn-danger text-danger remove-image" href="javascript:void(0);" lay-form-button data-id="{{item.fileId}}" data-name="{{fileName}}" lay-filter="templateAreaFileRemoveFile">
                                    <i class="fa fa-trash-o"></i>
                                </a>
                            </div>
                            {{#    } }}
                            {{# } }}
                            {{# if(d[fileName].length < 1) { }}
                            <div class="upload-image-box" style="width: auto; height: auto; padding: 5px;">
                                <a href="javascript:void(0);" lay-form-button data-explain="示例图片" data-name="templateAreaFile" lay-filter="templateAreaFileUploadFile">
                                    <i class="fa fa-plus-square-o" style="font-size: 70px;"></i>
                                </a>
                            </div>
                            {{# } }}
                    </div>
                </div>
                </script>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注说明</label>
                    <div class="layui-input-block">
                        <input type="text" id="templateAreaRemark" name="templateAreaRemark" class="layui-input" value="" placeholder="请输入备注说明" >
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <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>
<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', 'slider','laydate', 'laytpl', 'colorpicker'], function(){
        var $ = layui.$;
        var form = layui.form;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var wangEditor = window.wangEditor;
        var slider = layui.slider;
        var wangEditorObj = {};
        var viewerObj = {};
        var colorpicker = layui.colorpicker;
        var picturesTemplateId = top.restAjax.params(window.location.href).picturesTemplateId;

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

        $(document).on('click', '.selectColor', function() {
            selectColor(this, $(this).val())
        });

        function selectColor(ele, color) {
            //开启全功能
            colorpicker.render({
                elem: '#test-all'
                ,color: color
                ,format: 'rgb'
                ,predefine: true
                ,alpha: false
                ,offset: 'rb'
                ,size: 'xs'
                ,shadeClose: true
                ,done: function(color){
                    $(ele).val(color);
                    $(ele).next().css("background",color);
                }
                ,change: function(color){
                    $(ele).val(color);
                    $(ele).next().css("background",color);
                }
            });
            $('.layui-colorpicker').click();
        }

        function refreshDownloadTemplet(fileName, file) {
            var dataRander = {};
            dataRander[fileName] = file;
            laytpl(document.getElementById(fileName +'FileDownload').innerHTML).render(dataRander, function(html) {
                if($('input[name="templateAreaServerLink"]:checked').val() == "2") {
                    document.getElementById(fileName +'FileBox').innerHTML = html;
                }else{
                    document.getElementById(fileName +'FileBox').innerHTML = "";
                }
            });
        }

        form.on('radio(templateAreaServerLinkFilter)', function (data) {
            initTemplateAreaFileUploadFile();
            laytpl(document.getElementById('areaFontValueScript').innerHTML).render(data.value + "", function(html) {
                document.getElementById('areaFontValueDiv').innerHTML = html;
                form.render(null, 'dataForm');
            });
        });

        // 初始化文件列表
        function initFileList(fileName, ids, callback) {
            var dataForm = {};
            dataForm[fileName] = ids;
            form.val('dataForm', dataForm);
            if(!ids) {
                refreshDownloadTemplet(fileName, []);
                if(callback) {
                    callback(fileName, []);
                }
                return;
            }
            top.restAjax.get(top.restAjax.path('api/file/list', []), {
                ids: ids
            }, null, function(code, data) {
                refreshDownloadTemplet(fileName, data);
                if(callback) {
                    callback(fileName, data);
                }
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化区域插件文件文件上传
        function initTemplateAreaFileUploadFile() {
            var files = $('#templateAreaFile').val();
            initFileList('templateAreaFile', files);
            form.on('button(templateAreaFileUploadFile)', function(obj) {
                var name = this.dataset.name;
                var explain = this.dataset.explain;
                top.dialog.file({
                    type: 'image',
                    title: '上传'+ explain,
                    width: '400px',
                    height: '420px',
                    maxFileCount: '1',
                    onClose: function() {
                        var uploadFileArray = top.dialog.dialogData.uploadFileArray;
                        if(typeof(uploadFileArray) != 'undefined' && uploadFileArray.length > 0) {
                            var files = $('#'+ name).val();
                            for(var j = 0, file = uploadFileArray[j]; file = uploadFileArray[j++];) {
                                if(files.length > 0) {
                                    files += ',';
                                }
                                files += file.data;
                            }
                            initFileList(name, files, function() {});
                        }
                    }
                });
            });
            form.on('button(templateAreaFileRemoveFile)', function(obj) {
                var name = this.dataset.name;
                var id = this.dataset.id;
                var files = $('#'+ name).val().replace(id, '');
                files = files.replace(/\,+/g, ',');
                if(files.charAt(0) == ',') {
                    files = files.substring(1);
                }
                if(files.charAt(files.length - 1) == ',') {
                    files = files.substring(0, files.length - 1);
                }
                initFileList(name, files);
            });
        }

        // 初始化内容
        function initData() {
            initTemplateAreaFileUploadFile();
            top.restAjax.get(top.restAjax.path('api/picturestemplatearea/getcurrentuseridinfo', []), {}, null, function(code, data) {
            }, 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;
                formData.field.picturesTemplateId = picturesTemplateId;
                top.restAjax.post(top.restAjax.path('api/picturestemplatearea/savepicturestemplatearea', []), formData.field, null, function(code, data) {
                    var layerIndex = top.dialog.msg(top.dataMessage.commitSuccess, {
                        time: 0,
                        btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                        shade: 0.3,
                        yes: function(index) {
                            top.dialog.close(index);
                            window.location.reload();
                        },
                        btn2: function() {
                            closeBox();
                        }
                    });
                }, 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;
        });

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

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