<!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/wxpage/css/weui.css"/>
    <link rel="stylesheet" href="assets/wxpage/css/weuix.css"/>
    <style>
        .layui-m-layercont {height: 100%;}
        .page-iframe {width: 100%;height:100%;border: 0;}
    </style>
</head>
<body>
<div id="app" class="weui-cells weui-cells_form" style="margin-top:0;">
    <form id="uploadFileForm" enctype="multipart/form-data" style="display:none;">
        <input type="file" id="uploadFile" name="file" onchange="fileUpload()">
    </form>
    <div>
        <div class="form-item" th:each="dynamicFormFormShowFieldDTO, state: ${dynamicFormFormShowFieldDTOList}">
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'string'}">
                <!-- 文本start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${dynamicFormFormShowFieldDTO.fieldDefault}" th:placeholder="${'请输入'+ dynamicFormFormShowFieldDTO.fieldExplain}">
                    </div>
                </div>
                <!-- 文本end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'datetime'}">
                <!-- 时间戳start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${dynamicFormFormShowFieldDTO.fieldDefault}" th:placeholder="${'请选择'+ dynamicFormFormShowFieldDTO.fieldExplain}" readonly>
                    </div>
                </div>
                <!-- 时间戳end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'date'}">
                <!-- 日期start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${dynamicFormFormShowFieldDTO.fieldDefault}" th:placeholder="${'请选择'+ dynamicFormFormShowFieldDTO.fieldExplain}" readonly>
                    </div>
                </div>
                <!-- 日期end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'number'}">
                <!-- 整形start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="number" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${dynamicFormFormShowFieldDTO.fieldDefault}" th:placeholder="${'请输入'+ dynamicFormFormShowFieldDTO.fieldExplain}" pattern="[0-9]*" >
                    </div>
                </div>
                <!-- 整形end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'double'}">
                <!-- 双精度start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="number" class="weui-input" step="0.01" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${dynamicFormFormShowFieldDTO.fieldDefault}" th:placeholder="${'请输入'+ dynamicFormFormShowFieldDTO.fieldExplain}" pattern="^[-//+]?//d+(//.//d*)?|//.//d+$">
                    </div>
                </div>
                <!-- 双精度end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'textarea'}">
                <!-- 文本域start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <textarea class="weui-textarea" rows="4" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:placeholder="${'请输入'+ dynamicFormFormShowFieldDTO.fieldExplain}" placeholder="请输入文本域"></textarea>
                    </div>
                </div>
                <!-- 文本域end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'richText'}">
                <!-- 富文本start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <div th:id="${dynamicFormFormShowFieldDTO.fieldName}"></div>
                    </div>
                </div>
                <!-- 富文本end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'select'}">
                <!-- 下拉列表start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:placeholder="${'请选择'+ dynamicFormFormShowFieldDTO.fieldExplain}">
                    </div>
                </div>
                <!-- 下拉列表end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'radio'}">
                <!-- 单选start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cells weui-cells_radio" th:each="radioMap: ${radioMapList}" th:if="${radioMap.name eq dynamicFormFormShowFieldDTO.fieldName}">
                    <label class="weui-cell weui-check__label" th:for="${dynamicFormFormShowFieldDTO.fieldName + '_' + status.index}" th:each="radio, status: ${radioMap.list}">
                        <div class="weui-cell__bd">
                            <p th:text="${radio.dictionaryName}"></p>
                        </div>
                        <div class="weui-cell__ft">
                            <input class="weui-check" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:id="${dynamicFormFormShowFieldDTO.fieldName + '_' + status.index}" th:value="${radio.dictionaryId}" type="radio">
                            <span class="weui-icon-checked"></span>
                        </div>
                    </label>
                </div>
                <!-- 单选end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'checkbox'}">
                <!-- 复选start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cells weui-cells_checkbox" th:each="checkboxMap: ${checkboxMapList}" th:if="${checkboxMap.name eq dynamicFormFormShowFieldDTO.fieldName}">
                    <label class="weui-cell weui-check__label" th:for="${dynamicFormFormShowFieldDTO.fieldName + '_' + status.index}" th:each="checkbox, status: ${checkboxMap.list}">
                        <div class="weui-cell__hd">
                            <input type="checkbox" class="weui-check" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:id="${dynamicFormFormShowFieldDTO.fieldName + '_' + status.index}" th:value="${checkbox.dictionaryId}">
                            <i class="weui-icon-checked"></i>
                        </div>
                        <div class="weui-cell__bd">
                            <p th:text="${checkbox.dictionaryName}"></p>
                        </div>
                    </label>
                </div>
                <!-- 复选end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'file' || dynamicFormFormShowFieldDTO.fieldType eq 'image' || dynamicFormFormShowFieldDTO.fieldType eq 'video' || dynamicFormFormShowFieldDTO.fieldType eq 'audio'}">
                <!-- 文件上传start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cells" th:id="${dynamicFormFormShowFieldDTO.fieldName +'Box'}"></div>
                <div class="upload-file-button-box">
                    <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}">
                    <a href="javascript:void(0);" th:id="${dynamicFormFormShowFieldDTO.fieldName +'UploadFile'}" th:attr="data-name=${dynamicFormFormShowFieldDTO.fieldName}" class="weui-btn weui-btn_mini bg-blue">上传文件</a>
                </div>
                <!-- 文件上传end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'selectUser'}">
                <!-- 人员选择start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${dynamicFormFormShowFieldDTO.fieldDefault}">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName +'SelectUser'}" th:attr="data-name=${dynamicFormFormShowFieldDTO.fieldName}" th:placeholder="${'请选择'+ dynamicFormFormShowFieldDTO.fieldExplain}" readonly>
                    </div>
                </div>
                <!-- 人员选择end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'selectDepartment'}">
                <!-- 部门选择start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${dynamicFormFormShowFieldDTO.fieldDefault}">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName +'SelectDepartment'}" th:attr="data-name=${dynamicFormFormShowFieldDTO.fieldName}" th:placeholder="${'请选择'+ dynamicFormFormShowFieldDTO.fieldExplain}" readonly>
                    </div>
                </div>
                <!-- 部门选择end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'leftJoin' or dynamicFormFormShowFieldDTO.fieldType eq 'innerJoin' or dynamicFormFormShowFieldDTO.fieldType eq 'rightJoin'}">
                <!-- 联表start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:placeholder="${'请选择'+ dynamicFormFormShowFieldDTO.fieldExplain}">
                    </div>
                </div>
                <!-- 联表end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'currentUser'}">
                <!-- 当前用户start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${userIdAndValue}">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName +'CurrentUser'}" th:value="${userNameValue}" readonly>
                    </div>
                </div>
                <!-- 当前用户end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'currentDepartment'}">
                <!-- 当前部门start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${departmentIdAndNamesValue}">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName +'CurrentDepartment'}" th:value="${departmentNamesValue}" readonly>
                    </div>
                </div>
                <!-- 当前部门end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'currentRole'}">
                <!-- 当前角色start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${roleIdAndNamesValue}">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName +'CurrentRole'}" th:value="${roleNamesValue}" readonly>
                    </div>
                </div>
                <!-- 当前角色end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'currentGroup'}">
                <!-- 当前组start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${positionIdAndNamesValue}">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName +'CurrentGroup'}" th:value="${positionNamesValue}" readonly>
                    </div>
                </div>
                <!-- 当前组end -->
            </div>
            <div th:if="${dynamicFormFormShowFieldDTO.fieldType eq 'currentPosition'}">
                <!-- 当前职位start -->
                <div class="weui-cells__title" th:text="${dynamicFormFormShowFieldDTO.fieldExplain}"></div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="hidden" th:id="${dynamicFormFormShowFieldDTO.fieldName}" th:name="${dynamicFormFormShowFieldDTO.fieldName}" th:value="${groupIdAndNamesValue}">
                        <input type="text" class="weui-input" th:id="${dynamicFormFormShowFieldDTO.fieldName +'CurrentPosition'}" th:value="${groupNamesValue}" readonly>
                    </div>
                </div>
                <!-- 当前职位end -->
            </div>
        </div>
    </div>
    <div class="form-button">
        <a href="javascript:void(0);" class="weui-btn weui-btn_primary" id="submitForm">提交</a>
    </div>
</div>
<div id="commitSuccess" class="weui-msg" style="display: none;">
    <div class="weui-msg__icon-area"><i class="weui-icon-success weui-icon_msg"></i></div>
    <div class="weui-msg__text-area">
        <h2 class="weui-msg__title">提交成功</h2>
    </div>
</div>
<div id="commitError" class="weui-msg" style="display: none;">
    <div class="weui-msg__icon-area"><i class="weui-icon-warn  weui-icon_msg"></i></div>
    <div class="weui-msg__text-area">
        <h2 class="weui-msg__title">提交失败</h2>
        <p class="weui-msg__desc" id="errorMessage"></p>
    </div>
    <div class="weui-msg__opr-area">
        <p class="weui-btn-area">
            <a href="javascript:void(0);" class="weui-btn weui-btn_primary" id="commitErrorBack">返回继续</a>
        </p>
    </div>
</div>
<input type="hidden" id="tableName" th:value="${tableName}"/>
<input type="hidden" id="token" th:value="${token}"/>
<input type="hidden" id="uuidValue" th:value="${uuidValue}"/>
<script type="text/javascript" src="assets/js/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="assets/js/vendor/layer/layer.js"></script>
<script type="text/javascript" src="assets/wxpage/js/zepto.min.js"></script>
<script type="text/javascript" src="assets/wxpage/js/zepto.weui.js"></script>
<script type="text/javascript" src="assets/wxpage/js/php.js"></script>
<script type="text/javascript" src="assets/js/restajax.js"></script>
<script type="text/javascript" src="assets/wxpage/js/dialog.js"></script>
<script type="text/javascript" src="assets/js/vendor/wangEditor/wangEditor.min.js"></script>
<script type="text/javascript" th:inline="javascript">
    var token = $('#token').val();
    var currentUploadFileId = '';
    var wangEditor = window.wangEditor;
    var wangEditorArray = [];
    var formFieldList = [[${dynamicFormFormShowFieldDTOList}]];
    var selectMapList = [[${selectMapList}]];

    function initRichEditor(id, text) {
        // 富文本
        var editor = new wangEditor('#'+ id);
        editor.customConfig.menus = ['head', 'bold', 'fontSize', 'italic', 'justify', 'quote'];
        editor.customConfig.zIndex = 10;
        editor.customConfig.uploadImgMaxSize = 5 * 1024 * 1024;
        editor.customConfig.uploadImgMaxLength = 1;
        editor.customConfig.uploadFileName = 'image';
        editor.customConfig.uploadImgServer = 'app/file/wangeditorimage';
        editor.customConfig.uploadImgHeaders = {token: token};
        editor.customConfig.uploadImgHooks = {
            fail: function (xhr, editor, result) {
                top.dialog.msg('系统错误，图片上传失败');
            },
            error: function (xhr, editor) {
                top.dialog.msg('网络异常');
            },
            timeout: function (xhr, editor) {
                top.dialog.msg('网络请求超时');
            }
        };
        editor.create();
        editor.txt.html(text);
        var wangEditorObj = {};
        wangEditorObj[id] = editor;
        wangEditorArray.push(wangEditorObj);
    }

    function initSelect(id, name, data) {
        // 下拉选择
        $('#'+ id).select({
            title: name,
            items: data
        });
    }

    function initDateTime(id, title, min, max, defaultValue) {
        // 时间戳
        $('#'+ id).datetimePicker({
            title: title,
            min: min,
            max: max,
            value: defaultValue,
            onChange: function (picker, values, displayValues) {
            }
        });
    }

    function initDate(id, title, startYear, endYear, defaultValue) {
        // 日期
        $('#'+ id).datetimePicker({
            title: title,
            years: range(startYear, endYear),
            value: defaultValue,
            times: function () {
                return [];
            },
            parse: function (str) {
                return str.split("-");
            },
            onChange: function (picker, values, displayValues) {
            }
        });
    }

    // 文件上传 start
    function fileUpload() {
        if($('#uploadFile').val() == '') {
            return;
        }
        var formData = new FormData(document.getElementById("uploadFileForm"));
        top.restAjax.postFile(top.restAjax.path('app/file/uploadfile', []), formData, {
            headers: {
                token: token
            }
        }, function(code, data) {
            var selectIds = $('#'+ currentUploadFileId).val();
            if(selectIds != '') {
                selectIds += ',';
            }
            selectIds += data.data;
            initUploadFileList(currentUploadFileId, selectIds);
            $('#uploadFile').val('');
        }, function(code, data) {
            top.dialog.msg(data.msg);
        });
    }
    $(document).on('click', '.remove-file', function() {
        var id = this.dataset.id;
        var name = this.dataset.filename;
        var selectIds = $('#'+ name).val();
        selectIds = selectIds.replace(id, '');
        selectIds = selectIds.replace(/\,+/g, ',');
        if(selectIds.indexOf(',') == 0) {
            selectIds = selectIds.substring(1);
        }
        if(selectIds.lastIndexOf(',') == selectIds.length - 1) {
            selectIds = selectIds.substring(0, selectIds.length - 1);
        }
        initUploadFileList(name, selectIds);
    });
    // 初始化上传文件列表
    function initUploadFileList(name, selectIds) {
        var selectIdArray = selectIds.split(',');
        var fileItems = '';
        for(var i = 0, item; item = selectIdArray[i++];) {
            fileItems += '<div class="weui-cell file-upload"><div class="weui-cell__bd"><p>文件'+ i +'</p></div><div class="weui-cell__ft remove-file" style="font-size: unset;" data-id="'+ item +'" data-filename="'+ name +'">删除</div></div>';
        }
        $('#'+ name +'Box').html(fileItems);
        $('#'+ name).val(selectIds);
    }
    // 文件上传end

    $(function() {
        top.restAjax.get(top.restAjax.path('app/dynamicdata/getdynamicdata/{tableName}/{uuidValue}', [$('#tableName').val(), $('#uuidValue').val()]), {}, {
            headers: {
                token: token
            }
        }, function(code, data) {
            for(var i = 0, item = formFieldList[i]; item = formFieldList[i++];) {
                if(item.fieldType === 'radio') {
                    $('input[name='+ item.fieldName +'][value='+ data[item.fieldName] +']').attr('checked', true);
                } else if(item.fieldType === 'checkbox') {
                    var valueArray = data[item.fieldName].split(',');
                    for(var j = 0, value; value = valueArray[j++];) {
                        $('input[name='+ item.fieldName +'][value='+ value +']').attr('checked', true);
                    }
                } else if(item.fieldType === 'date') {
                    initDate(item.fieldName, item.fieldExplain, 1960, 2060, data[item.fieldName]);
                } else if(item.fieldType === 'datetime') {
                    initDateTime(item.fieldName, item.fieldExplain, '1960-12-12 00:00:00', '2060-12-12 23:59:59', data[item.fieldName]);
                } else if(item.fieldType === 'select') {
                    var dataArray = [];
                    for(var j = 0, selectMap; selectMap = selectMapList[j++];) {
                        if(selectMap.name === item.fieldName) {
                            var dataArray = [];
                            for(var k = 0, map; map = selectMap.list[k++];) {
                                dataArray.push({
                                    title: map.dictionaryName,
                                    value: map.dictionaryId
                                });
                                if(map.dictionaryId == data[item.fieldName]) {
                                    $('#'+ args.fieldName).val(map.dictionaryName);
                                    $('#'+ args.fieldName)[0].dataset.values = map.dictionaryId;
                                }
                            }
                            initSelect(item.fieldName, item.fieldExplain, dataArray);
                            break;
                        }
                    }
                } else if(item.fieldType === 'richText') {
                    initRichEditor(item.fieldName, data[item.fieldName]);
                } else if(item.fieldType === 'file') {
                    $(document).on('click', '#'+ item.fieldName +'UploadFile', function() {
                        currentUploadFileId = this.dataset.name;
                        $('#uploadFile').click();
                    });
                    initUploadFileList(item.fieldName, data[item.fieldName]);
                } else if(item.fieldType === 'selectUser') {
                    $(document).on('click', '#'+ item.fieldName +'SelectUser', function() {
                        var name = this.dataset.name;
                        var selectedUsers = [];
                        var selectDepartmentUser = $('#'+ name).val().split(',');
                        for(var selectDepartmentUserIndex = 0, item = selectDepartmentUser[selectDepartmentUserIndex]; item = selectDepartmentUser[selectDepartmentUserIndex++];) {
                            var userInfo = item.split('|');
                            selectedUsers.push({
                                userId: userInfo[0],
                                departmentId: userInfo[1]
                            });
                        }
                        top.dialog.dialogData.selectedUsers = selectedUsers;
                        top.dialog.open({
                            url: 'approute/tree/treeuserrelease',
                            title: '选择组织部门人员',
                            width: '100%',
                            height: '100%',
                            onClose: function() {
                                var selectedUsers = top.dialog.dialogData.selectedDepartmentUsers;
                                if(selectedUsers != null && selectedUsers.length > 0) {
                                    var selectedUsersVal = '';
                                    var showSelectedUsersVal = '';
                                    for(var j = 0, selectUser = selectedUsers[j]; selectUser = selectedUsers[j++];) {
                                        if(selectedUsersVal.length > 0) {
                                            selectedUsersVal += ',';
                                        }
                                        if(showSelectedUsersVal.length > 0) {
                                            showSelectedUsersVal += ',';
                                        }
                                        selectedUsersVal += (selectUser.userId +'|'+ selectUser.departmentId +'|'+ selectUser.userName +'|'+ selectUser.userTitle);
                                        showSelectedUsersVal += selectUser.userName;
                                    }
                                    $('#'+ name).val(selectedUsersVal);
                                    $('#'+ name +'SelectUser').val(showSelectedUsersVal);
                                } else {
                                    $('#'+ name).val('');
                                    $('#'+ name +'SelectUser').val('');
                                }
                            }
                        });
                    });
                    var selectedUsersVal = data[item.fieldName];
                    if(selectedUsersVal != '') {
                        var users = selectedUsersVal.split(',');
                        var showSelectedUsersVal = '';
                        for(var j = 0, user; user = users[j++];) {
                            if(showSelectedUsersVal != '') {
                                showSelectedUsersVal += ',';
                            }
                            showSelectedUsersVal += user.split('|')[2];
                        }
                        $('#'+ item.fieldName).val(selectedUsersVal);
                        $('#'+ item.fieldName +'SelectUser').val(showSelectedUsersVal);
                    }
                } else if(item.fieldType === 'selectDepartment') {
                    $(document).on('click', '#'+ item.fieldName +'SelectDepartment', function() {
                        var name = this.dataset.name;
                        var selectedNodes = [];
                        var selectDepartment = $('#'+ name).val().split(',');
                        for(var selectDepartmentIndex = 0, item = selectDepartment[selectDepartmentIndex]; item = selectDepartment[selectDepartmentIndex++];) {
                            var departmentInfo = item.split('|');
                            selectedNodes.push({
                                id: departmentInfo[0],
                                name: departmentInfo[1],
                                title: departmentInfo[1]
                            });
                        }
                        top.dialog.tree({
                            title: '选择部门',
                            apiUri: 'app/department/listztreedepartment',
                            width: '100%',
                            height: '100%',
                            dataFilter: function(treeId, parentNode, childNodes) {
                                return childNodes;
                            },
                            check: {
                                enable: true,
                                selectType: 'checkbox',
                                checkboxType: {Y: 'ps', N: 'ps'},
                                radioType: 'level',
                            },
                            selectedNodes: selectedNodes,
                            onClose: function() {
                                var selectNodes = top.dialog.dialogTreeData.selectedNodes;
                                if(typeof(selectNodes) != 'undefined' && selectNodes != null) {
                                    if(selectNodes.length > 0) {
                                        var selectedDepartmentVal = '';
                                        var showSelectedDepartmentsVal = '';
                                        for(var j = 0, selectNode = selectNodes[j]; selectNode = selectNodes[j++];) {
                                            if(selectedDepartmentVal.length > 0) {
                                                selectedDepartmentVal += ',';
                                            }
                                            if(showSelectedDepartmentsVal.length > 0) {
                                                showSelectedDepartmentsVal += ',';
                                            }
                                            selectedDepartmentVal += (selectNode.id +'|'+ selectNode.name);
                                            showSelectedDepartmentsVal += selectNode.name;
                                        }
                                        $('#'+ name).val(selectedDepartmentVal);
                                        $('#'+ name +'SelectDepartment').val(showSelectedDepartmentsVal);
                                    } else {
                                        $('#'+ name).val('');
                                        $('#'+ name +'SelectDepartment').val('');
                                    }
                                }
                            }
                        });
                    });
                    var selectedDepartmentVal = data[item.fieldName];
                    if(showSelectedDepartmentsVal != '') {
                        var departments = selectedDepartmentVal.split(',');
                        var showSelectedUsersVal = '';
                        for(var j = 0, department; department = departments[j++];) {
                            if(showSelectedUsersVal != '') {
                                showSelectedUsersVal += ',';
                            }
                            showSelectedUsersVal += selectedDepartmentVal.split('|')[1];
                        }
                        $('#'+ item.fieldName).val(selectedUsersVal);
                        $('#'+ item.fieldName +'SelectDepartment').val(showSelectedDepartmentsVal);
                    }
                } else if(item.fieldType === 'leftJoin' || item.fieldType === 'innerJoin' || item.fieldType === 'rightJoin') {
                    top.restAjax.get(top.restAjax.path('app/dynamicdata/listdynamicdata/{tableName}', [item.joinTable]), {}, {
                        headers: {
                            token: token
                        },
                        fieldName: item.fieldName,
                        fieldExplain: item.fieldExplain,
                        joinTable: item.joinTable,
                        joinTableField: item.joinTableField,
                        joinTableFormShow: item.joinTableFormShow,
                        checkItem: data[item.fieldName]
                    }, function(code, data, args) {
                        var dataArray = [];
                        for(var j = 0, joinTable; joinTable = data[j++];) {
                            dataArray.push({
                                title: joinTable[args.joinTableFormShow],
                                value: joinTable[args.joinTableField],
                            });
                            if(args.checkItem == joinTable[args.joinTableField]) {
                                $('#'+ args.fieldName).val(joinTable[args.joinTableFormShow]);
                                $('#'+ args.fieldName)[0].dataset.values = args.checkItem;
                            }
                        }
                        initSelect(args.fieldName, args.fieldExplain, dataArray);
                    }, function(code, data) {
                        top.dialog.msg(data.msg);
                    });
                } else {
                    $('#'+ item.fieldName).val(data[item.fieldName]);
                }
            }
        }, function(code, data) {
            setTimeout(function() {
                $('#errorMessage').text(data.msg);
                $('#app').hide();
                $('#commitError').show();
            }, 500);
        }, function() {
            $.showLoading('正在加载...');
        }, function() {
            $.hideLoading();
        });
    });

    $(document).on('click', '#submitForm', function() {
        var formData = {};
        // 富文本
        for(var i = 0, editor; editor = wangEditorArray[i++];) {
            for(var j in editor) {
                formData[j] = editor[j].txt.html();
            }
        }
        for(var i = 0, item; item = formFieldList[i++];) {
            if(item.fieldType === 'radio') {
                var radioValue = '';
                $('input[name='+ item.fieldName +']:checked').each(function() {
                    radioValue += $(this).val();
                });
                if(radioValue === '') {
                    $.toptip(item.fieldExplain +'不能为空', 'error');
                    return;
                }
                formData[item.fieldName] = radioValue;
            } else if(item.fieldType === 'checkbox') {
                var checkboxValue = '';
                $('input[name='+ item.fieldName +']:checked').each(function() {
                    checkboxValue += $(this).val();
                });
                if(checkboxValue === '') {
                    $.toptip(item.fieldExplain +'不能为空', 'error');
                    return;
                }
                formData[item.fieldName] = checkboxValue;
            } else if(item.fieldType === 'datetime') {
                var datetimeValue = $('#'+ item.fieldName).val();
                if(datetimeValue === '') {
                    $.toptip(item.fieldExplain +'不能为空', 'error');
                    return;
                }
                formData[item.fieldName] = datetimeValue +':00';
            } else if(item.fieldType === 'select') {
                var selectValue = $('#'+ item.fieldName)[0].dataset.values;
                if(selectValue === '') {
                    $.toptip(item.fieldExplain +'不能为空', 'error');
                    return;
                }
                formData[item.fieldName] = selectValue;
            } else if(item.fieldType === 'leftJoin' || item.fieldType === 'innerJoin' || item.fieldType === 'rightJoin') {
                var joinValue = $('#'+ item.fieldName)[0].dataset.values;
                if(joinValue === '') {
                    $.toptip(item.fieldExplain +'不能为空', 'error');
                    return;
                }
                formData[item.fieldName] = joinValue;
            } else if(item.fieldType != 'richText') {
                var datetimeValue = $('#'+ item.fieldName).val();
                if(datetimeValue === '') {
                    $.toptip(item.fieldExplain +'不能为空', 'error');
                    return;
                }
                formData[item.fieldName] = $('#'+ item.fieldName).val();
            }
        }
        $.confirm("您确定要更新吗?", "确认更新?", function() {
            top.restAjax.put(top.restAjax.path('app/dynamicdata/updatedynamicdata/{tableName}/{uuidValue}', [$('#tableName').val(), $('#uuidValue').val()]), formData, {
                headers: {
                    token: token
                }
            }, function(code, data) {
                $('#app').hide();
                $('#commitSuccess').show();
            }, function(code, data) {
                $('#errorMessage').text(data.msg);
                $('#app').hide();
                $('#commitError').show();
            }, function() {
                $.showLoading('正在提交...');
            }, function() {
                $.hideLoading();
            });
        }, function() {
        });
    })
    // 错误返回
    $(document).on('click', '#commitErrorBack', function() {
        $('#commitError').hide();
        $('#app').show();
    })
</script>
</body>

</html>
