<%@ 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}/tenant/dataBase/addDBInfo",

        init: function () {
            this.toDisable();
            this.bindEvents();
            this.initForm();
        },
        toDisable: function () {
            if ($("#flag").val()=='disabled') {
                $("#btnEditor").show();
                $("#btnSave").hide();
                $("input[name='name']").attr("disabled", "disabled");
                $("input[name='userName']").attr("disabled", "disabled");
                $("input[name='passward']").attr("disabled", "disabled");
                $("input[name='host']").attr("disabled", "disabled");
                $("input[name='port']").attr("disabled", "disabled");
                $("input[name='valid']").attr("disabled", "disabled");
            }
        },
        initForm: function () {
            var me = this;
           var data;
            var modelString = "${model.id}";
            if(modelString!=undefined && modelString!=null && modelString.length>0)
            {
                var valid = "${model.valid}";
                liger.get("valid").selectValue(valid);
               data={
                    id: "${model.id}",
                    name: "${model.name}",
                   userName: "${model.userName}",
                   valid: valid,
                   password: "${model.password}",
                   host: "${model.host}",
                   port: "${model.port}",
                };

                me.actionUrl = "${contextRoot}/tenant/dataBase/updateDBInfo";
            }else{
                liger.get("valid").selectValue("1");//默认有效
            }
            $("#div_info_form").ligerAutoForm({
                data:data,
                validate:{
                    name:"required",
                    userName:"required",
                    password:"required",
                    host:"required",
                    port:"required",
                    valid: {
                        required:true
                    }
                },

            });
        },
        bindEvents: function () {
            var me = this;
            $(".m-form-bottom").on("click","#btnSave",function () {
                $("#btnSave").css("pointer-events","none");
                $("#name_icon").removeClass("required");
                if($("#name_icon").val()=="") {
                    $("#name_icon").addClass("required");
                    if(!$("#div_info_form").ligerAutoForm("validate")){
                        return;
                    }
                    return;
                }
                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.dbInfo.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='name']").removeAttr("disabled");
                        $("input[name='userName']").removeAttr("disabled");
                        $("input[name='password']").removeAttr("disabled");
                        $("input[name='host']").removeAttr("disabled");
                        $("input[name='port']").removeAttr("disabled");
                        $("input[name='valid']").removeAttr("disabled");
                        $("#flag").val("");
            });
            $(".m-form-bottom").on("click","#btnCancel",function () {
                parent.dbInfo.dialog.close();
            });

            $("#valid").ligerComboBox({data : [{"value":"有效","code":"1"},{"value":"无效","code":"0"}],
                cancelable:false,
                onSuccess:function(data){
                }});

        }
    };



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

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