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

    /* *************************** 模块初始化 ***************************** */
    var appService = {
        grid: null,
        dialog: null,
        init: function () {
            var me = this;
            $('.m-retrieve-area').show();
            $("#txtName").ligerSearch({
                onClick:function(value){
                    me.reloadGrid();
                }});
            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/app/getAppServiceList',
                parms: {
                    name: $('#txtName').val(),
                    valid:$('#txtValid_val').val(),
                    appId:'${appId}'
                },
                columns: [
                    {display: '服务名称', id: 'id', name: 'name', width: '20%'},
                    {display: '版本', name: 'version', width: '10%'},
                    {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: 'description', width: '30%'},
                    {
                        display: '操作', name: 'operator', width: '30%', render: function (row) {

                        var html = '<div class="m-inline-buttons" style="width:350px;">';
                        html += "<a class=\"m-btn\" style=\"padding-right:10px\" onclick=\"appService.dialogDetail('"+row.id+"')\">查看详情</a>";
                        html += "<a class=\"m-btn-edit\" onclick=\"appService.editorDialog('"+row.id+"','"+row.appId+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"appService.delete('"+row.id+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow: function (row) {
                    me.editorDialog(row.id,row.appId);
                }
            });
            me.bindEvents();

        },
        bindEvents: function () {
            var me = this;

            $('#div_new_record').click(function () {
                var appId= "${appId}";
                me.editorDialog(null,appId);
            });

            $('#div_back').click(function () {
                me.returnAppManage();
            });

            $("#txtValid").ligerComboBox({data : [{"value":"全部","code":""},{"value":"有效","code":"1"},{"value":"无效","code":"0"}],

                cancelable:false,
                onSelected: function (value)
                {
                    me.reloadGrid();
                },
                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}/app/deleteAppService",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                appService.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:$('#txtValid_val').val(),
                    appId:'${appId}'
                }
            });
            this.grid.reload();
        },
        //编辑弹窗
        editorDialog: function (id,appId) {
            var me = this;
            var title = "新增服务";
            var params = null;
            if (id != undefined && id != null) {
                title = "编辑服务";
                params = {"id": id};
            }

            me.dialog = $.ligerDialog.open({
                height: 800,
                width: 1200,
                title: title,
                url: '${contextRoot}/app/editorAppService?appId='+appId,
                //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: 800,
                width: 1200,
                title: title,
                url: '${contextRoot}/app/appServiceDetail',
                //load: true,
                urlParms: params
            });
        },
        returnAppManage: function () {
            indexPage.loadPage('${contextRoot}/app/initial'),"","";
        },
        anthorize: function (id) {

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


    };

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