<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script type="text/javascript">

    /* *************************** 模块初始化 ***************************** */
    var editorParam = {
        //form
        actionUrl:"${contextRoot}/rule/addRule",

        init: function () {
            this.toDisable();
            this.bindEvents();
            this.initForm();
        },
        toDisable: function () {
            if ($("#flag").val() == 'disabled') {
                $("#btnEditor").show();
                $("#btnSave").hide();
                $("input[name='type']").attr("disabled", "disabled");
                $("input[name='name']").attr("disabled", "disabled");
                $("input[name='describe']").attr("disabled", "disabled");
                $("input[name='rule']").attr("disabled", "disabled");
                $("input[name='error_code']").attr("disabled", "disabled");
            }
        },
        initForm: function () {
            var me = this;
            $("#type").ligerComboBox({dict: true, dictName: "RULE_TYPE"});
            $("#error_code").ligerComboBox({dict: true, dictName: "GLOBAL_ERROR_CODE"});
            var data;
            var modelString = "${model.id}";
            if (modelString != undefined && modelString != null && modelString.length > 0) {
                data = {
                    id: "${model.id}",
                    type: "${model.type}",
                    name: "${model.name}",
                    describe: "${model.describe}",
                    rule: "${model.rule}",
                    error_code: "${model.error_code}"
                };

                me.actionUrl = "${contextRoot}/rule/updateRule";
            }
            $("#div_info_form").ligerAutoForm({
                data: data,
                validate: {
                    name:"required",
                    describe:"required",
                    rule:"required",
                    error_code:"required",
                    type:"required"
                }
            });
        },
        bindEvents: function () {
            var me = this;
            $(".m-form-bottom").on("click", "#btnSave", function () {
                if (!$("#div_info_form").ligerAutoForm("validate")) {
                    return;
                }
                var data = $("#div_info_form").ligerAutoForm("getData");
                $.ajax({ //ajax处理
                    type: "POST",
                    url: me.actionUrl,
                    dataType: "json",
                    data: data,
                    cache: false,
                    success: function (data) {
                        if (data.successFlg) {
                            parent.rule.dialogSuccess(data.message);
                        }
                        else {
                            $.ligerDialog.error(data.message);
                        }
                        $("#btnSave").css("pointer-events", "");
                    },
                    error: function (data) {
                        $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                        $("#btnSave").css("pointer-events", "");
                    }
                });
            });
            $(".m-form-bottom").on("click", "#btnEditor", function () {
                $("#btnEditor").hide();
                $("#btnSave").show();
                $("input[name='type']").removeAttr("disabled");
                $("input[name='name']").removeAttr("disabled");
                $("input[name='describe']").removeAttr("disabled");
                $("input[name='rule']").removeAttr("disabled");
                $("input[name='error_code']").removeAttr("disabled");
                $("#flag").val("");
            });
            $(".m-form-bottom").on("click", "#btnCancel", function () {
                parent.rule.dialog.close();
            });

//            $("#type").ligerComboBox({
//                data: [{"value": "完整性", "code": "1"}, {"value": "及时性", "code": "2"}, {"value": "准确性", "code": "3"}],
//                cancelable: false,
//                onSuccess: function (data) {
//                }
//            });
        }
    };


    $(function () {
        editorParam.init();

    });
</script>
<script type="text/javascript" src="${staticRoot}/lib/jquery/jqueryform.js"></script>
