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

    /* *************************** 模块初始化 ***************************** */
    var tenant = {
        grid: null,
        dialog: null,
        init: function () {
            this.bindEvents();
            this.initForm();
        },
        initForm: function () {
            var me = this;
            $('.m-retrieve-area').show();
            $("#txtName").ligerSearch({
                onClick:function(value){
                    me.reloadGrid();
            }});

            var tenantStatus = liger.get("valid").value;
            if(tenantStatus ==undefined || tenantStatus ==null || tenantStatus.length<=0){
                liger.get("valid").selectValue("");
            }

            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/tenant/getTenantList',
                parms: {
                    name: $('#txtName').val(),
                    valid:tenantStatus
                },
                columns: [
                    {display: '租户名称', id: 'id', name: 'name', width: '20%'},
                    {display: '租户代码', name: 'code', width: '15%'},
                    {display: '数据库名', name: 'schema', width: '15%'},
                    {display: '状态', name: 'valid', width: '10%',align: 'center', render: function (rowdata, rowindex, value) {
                        if(rowdata.valid==1 ){
                            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>有效  </span></div>';
                        }else if(rowdata.valid==0){
                            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>无效 </span></div>';
                        }
                    }},
                    {
                        display: '操作', name: 'operator', width: '40%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:350px;">';
                        html += "<a class=\"m-btn\" style=\"padding-right:10px\" onclick=\"tenant.editorDialog('"+row.id+"','disabled')\">查看详情</a>";
                        html += "<a class=\"m-btn-edit\" onclick=\"tenant.editorDialog('"+row.id+"','')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"tenant.delete('"+row.id+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow: function (row) {
                    me.editorDialog(row.id);
                }
            });

        },
        bindEvents: function () {
            var me = this;
            var flag = false;
            $('#div_new_record').click(function () {
                me.editorDialog();
            });
            $("#valid").ligerComboBox({data : [{"value":"全部","code":""},{"value":"有效","code":"1"},{"value":"无效","code":"0"}],
                cancelable:false,
                initIsTriggerEvent: false,
                onSelected: function (value)
                {
                    if (flag) {
                        me.reloadGrid();
                    } else {
                        flag = true;
                    }
                },
                onSuccess:function(data){
                }});
            $(".l-text").css("display","inline-block");
            $(".l-text-wrapper").css("display","inline-block");

        },
        delete:function(id){
            var message = "确定要删除该应用信息吗？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/tenant/deleteTenant",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                tenant.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        //刷新列表数据
        reloadGrid: function () {
            this.grid.set({
                parms: {name: $('#txtName').val(),valid:$('#valid_val').val()}
            });
            this.grid.reload();
        },
        //编辑弹窗
        editorDialog: function (id, flag) {
            var me = this;
            var title = "新增租户";
            var params = null;
            if (id != undefined && id != null) {
                title = "编辑租户";
                params = {"id": id, "flag": flag};
            }

            me.dialog = $.ligerDialog.open({
                height: 600,
                width: 500,
                title: title,
                url: '${contextRoot}/tenant/editorTenant',
                //load: true,
                urlParms: params
            });
        },
        dialogDetail: function (id) {
            var me = this;
            var title = "应用详情";
            var params = null;
            if (id != undefined && id != null) {
                params = {"id": id};
            }

            me.dialog = $.ligerDialog.open({
                height: 500,
                width: 500,
                title: title,
                url: '${contextRoot}/tenant/tenantDetail',
                //load: true,
                urlParms: params
            });
        },
    anthorize: function (id) {

        },
        //弹窗返回
        dialogSuccess: function (message) {
            $.ligerDialog.success(message);
            tenant.reloadGrid();
            tenant.dialog.close();
        }


    };

    $(function () {
        tenant.init();
    });
</script>