<!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-add">
            <input type="hidden" id="projectId" name="projectId" th:value="${projectId}">
            <input type="hidden" id="entityId" name="entityId" th:value="${entityId}">
            <input type="hidden" id="versionId" name="versionId" th:value="${versionId}">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">字段名：</label>
                <div class="col-sm-8">
                    <input id="fieldName" name="fieldName" placeholder="商品名称" 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" placeholder="productName" 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 id="fieldType" name="fieldType" class="form-control m-b">
                        <option th:each="dt : ${dataTypeList}" th:text="${dt.dataTypeName}" th:value="${dt.dataTypeCode}"></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">
                        <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">
                        <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">
                        <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" 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" ></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "model/modelField"
        $("#form-modelField-add").validate({
            onkeyup: false,
            rules:{
                fieldCode:{
                    maxlength: 50,
                    remote: {
                        url: prefix + "/checkFieldCodeUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "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-add").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});
                data.push({"name": "fieldSort", "value": '999'});
                $.operate.save(prefix + "/add", data);
            }
        }
    </script>
</body>
</html>