<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改数据字段')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-modelField-edit" th:object="${modelField}">
            <input id="id" name="id" th:field="*{id}" type="hidden">
            <input type="hidden" id="projectId" name="projectId" th:value="*{projectId}">
            <input type="hidden" id="entityId" name="entityId" th:value="*{entityId}">

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">字段名：</label>
                <div class="col-sm-8">
                    <input id="fieldName" name="fieldName" th:field="*{fieldName}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">逻辑名：</label>
                <div class="col-sm-8">
                    <input id="fieldCode" name="fieldCode" th:field="*{fieldCode}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">类型：</label>
                <div class="col-sm-8">
                    <select name="fieldType" class="form-control m-b">
                        <option th:each="dt : ${dataTypeList}" th:text="${dt.dataTypeName}" th:value="${dt.dataTypeCode}" th:field="*{fieldType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">主键：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="fieldPk" th:checked="${modelField.fieldPk == 'Y' ? true : false}">
                        <span></span>
                    </label>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">非空：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="notNull" th:checked="${modelField.notNull == 'Y' ? true : false}">
                        <span></span>
                    </label>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">自增：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="autoIncrement" th:checked="${modelField.autoIncrement == 'Y' ? true : false}">
                        <span></span>
                    </label>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">默认值：</label>
                <div class="col-sm-8">
                    <input id="defaultValue" name="defaultValue" th:field="*{defaultValue}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea id="remark" name="remark" class="form-control">[[*{remark}]]</textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "model/modelField";
        $("#form-modelField-edit").validate({
            onkeyup: false,
            rules:{
                fieldCode:{
                    maxlength: 50,
                    remote: {
                        url: prefix + "/checkFieldCodeUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "id":$("#id").val(),
                            "projectId":$("#projectId").val(),
                            "entityId":$("#entityId").val(),
                            "fieldCode": function() {
                                return $.common.trim($("#fieldCode").val());
                            }
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
                fieldName:{
                    maxlength: 25
                },
                defaultValue:{
                    maxlength: 8
                },
                remark:{
                    maxlength: 100
                },
            },
            messages: {
                "fieldCode": {
                    remote: "字段已经存在"
                },
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var data = $("#form-modelField-edit").serializeArray();
                var fieldPk = $("input[id='fieldPk']").is(':checked') == true ? "Y" : "N";
                var notNull = $("input[id='notNull']").is(':checked') == true ? "Y" : "N";
                var autoIncrement = $("input[id='autoIncrement']").is(':checked') == true ? "Y" : "N";

                data.push({"name": "fieldPk", "value": fieldPk});
                data.push({"name": "notNull", "value": notNull});
                data.push({"name": "autoIncrement", "value": autoIncrement});
                $.operate.save(prefix + "/edit", data);
            }
        }
    </script>
</body>
</html>