<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    /* *************************** 自定义模块 ***************************** */
    var configSources = {
        dialog: null,
        grid: null,
        //初始化
        init: function () {
            var me = this;
            $('.m-retrieve-area').show();
            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/datasource/getDatasource',
                columns: [
                    {display: '数据源名称', name: 'name', width: '25%'},
                    {
                        display: '机构名称',
                        name: 'orgName',
                        width: '25%',
                        dict: false,
                        dictName: "SYSTEM_ORGANIZATION"
                    },
                    {
                        display: '数据源类型',
                        name: 'type',
                        width: '25%',
                        dict: false,
                        dictName: "DATASOURCE_TYPE",
                        render: function (row) {
                            if (row.type == 1) {
                                return "Web Service";
                            } else if (row.type == 2) {
                                return "文件系统";
                            } else {
                                return "数据库";
                            }
                        }
                    },
                    {
                        display: '操作', name: 'operator', width: '25%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:80px;">';
                        html += "<a class=\"m-btn-edit\" onclick=\"configSources.editorSource('" + row.id + "')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"configSources.deleteSource('" + row.id + "')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow: function (row) {
                    me.editorSource(row.id);
                }
            });

            me.bindEvents();
        },
        //绑定按钮事件
        bindEvents: function () {
            var me = this;
            $('#div_new_record').click(function () {
                me.editorSource();
            });
        },
        //编辑弹窗
        editorSource: function (id) {
            var me = this;
            var title = "新增数据源";
            var params = null;
            if (id != undefined && id != null) {
                title = "编辑数据源";
                params = {sourceId: id};
            }

            me.dialog = $.ligerDialog.open({
                id: 'sourceDialog',
                height: 560,
                width: 600,
                title: title,
                url: '${contextRoot}/datasource/editorSource',
                //load: true,
                urlParms: params
            });
        },
        //删除数据源
        deleteSource: function (id) {
            var message = "确定要删除该数据源吗？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm) {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url: "${contextRoot}/datasource/deleteDatasource",
                        dataType: "json",
                        data: {id: id},
                        cache: false,
                        success: function (data) {
                            if (data.successFlg) {
                                $.ligerDialog.success(data.message);
                                configSources.grid.reload();
                            }
                            else {
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error: function (data) {
                            $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                        }
                    });
                }
            });

        },
        //关闭弹窗
        callbackDialog: function (message) {
            var me = this;
            if (message != undefined && message.length > 0) {
                $.ligerDialog.success(message);
            }
            me.grid.reload();
            me.dialog.close();
        }
    };

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