<%@ 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
        dbListUrl: "${contextRoot}/tenant/dataBase/getDataBaseValues",
        actionUrl: "${contextRoot}/tenant/addTenant",

        init: function () {
            var me = this;
            this.selectDBs("");
            this.toDisable();
            this.bindEvents();
            this.initForm();

        },
        toDisable: function () {
            if ($("#flag").val() == 'disabled') {
                $("#btnEditor").show();
                $("#btnSave").hide();
                $("input[name='cnName']").attr("disabled", "disabled");
                $("input[name='name']").attr("disabled", "disabled");
                $("input[name='loginName']").attr("disabled", "disabled");
                $("input[name='passward']").attr("disabled", "disabled");
                $("input[name='schema']").attr("disabled", "disabled");
                $("input[name='valid']").attr("disabled", "disabled");
                $("input[name='datasourceId']").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}";
                var dataSourceId = "${model.dataSourceId}";

                liger.get("valid").selectValue(valid);
                liger.get("dataSourceId").selectValue(dataSourceId);

                data = {
                    id: "${model.id}",
                    name: "${model.name}",
                    cnName: "${model.cnName}",
                    loginName: "${model.loginName}",
                    valid: valid,
                    password: "${model.password}",
                    schema: "${model.schema}",
                    dataSourceId: dataSourceId,
                };
                me.actionUrl = "${contextRoot}/tenant/updateTenant";
            } else {
                liger.get("valid").selectValue("1");//默认有效
                liger.get("dataSourceId").selectValue("1");//默认有效

            }
            $("#div_info_form").ligerAutoForm({
                data: data,
                validate: {
                    name: "required",
                    loginName: "required",
                    password: "required",
                    scheme: "required",
                    valid: {
                        required: true
                    }
                },

            });
        },
        selectDBs: function (q) {
            var me = this;
            $.ajax({ //ajax处理
                type: "GET",
                url: me.dbListUrl,
                dataType: "json",
                async: false,
                data: {"q": q},
                cache: false,
                success: function (data) {
                    if (data.successFlg) {
                        debugger
                        $("#dataSourceId").ligerComboBox({
                            data: data.detailModelList,
                            cancelable: false,
                            onSuccess: function (data) {
                                alert(1111);
                            }
                        });
                    }
                    else {
                        $.ligerDialog.error(data.message);
                    }
                },
                error: function (data) {
                    $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                }
            });
        },
        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.tenant.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='cnName']").removeAttr("disabled");
                $("input[name='loginName']").removeAttr("disabled");
                $("input[name='password']").removeAttr("disabled");
                $("input[name='schema']").removeAttr("disabled");
                $("input[name='valid']").removeAttr("disabled");
                $("input[name='datasourceId']").removeAttr("disabled");
                $("#flag").val("");
            });
            $(".m-form-bottom").on("click", "#btnCancel", function () {
                parent.tenant.dialog.close();
            });

//            $(".l-text").on("click","#dataSourceId",function () {
//                var q = $(this).val();
//               me.selectDBs(q);
//            });
            $("#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>
