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

    /* *************************** 模块初始化 ***************************** */
    var app = {
        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 appStatus = liger.get("statusName").value;
            if(appStatus ==undefined || appStatus ==null || appStatus.length<=0){
                liger.get("statusName").selectValue("");
            }

            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/app/getAppList',
                parms: {
                    name: $('#txtName').val(),
                    status:appStatus
                },
                columns: [
                    {display: '图标', name: 'icon', width: '10%',height:'50',align: 'center', render: function (rowdata, rowindex, value) {
                        return ' <div style="vertical-align:middle;"><img  style="width: 50px; height: 50px;"  src="${dfs}/'+ rowdata.icon+'" /></div>';
                    }},
                    {display: '应用名称', id: 'id', name: 'name', width: '15%'},
                    {display: '英文名', name: 'code', width: '10%'},
                    {display: '开发者', name: 'developer', width: '10%'},
                    {display: '状态', name: 'status', width: '10%',align: 'center', render: function (rowdata, rowindex, value) {
                        if(rowdata.status==1 ){
                            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>有效  </span></div>';
                        }else if(rowdata.status==0){
                            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>无效 </span></div>';
                        }
                    }},
                    {display: '应用地址', name: 'appUrl', width: '15%'},
                    {
                        display: '操作', name: 'operator', width: '35%', render: function (row) {

                        var html = '<div class="m-inline-buttons" style="width:350px;">';
                        html += "<a class=\"m-btn\" style=\"padding-right:10px\" onclick=\"app.editorDialog('"+row.id+"','disabled')\">查看详情</a>";
                        html += "<a class=\"m-btn\" style=\"padding-right:10px\" onclick=\"app.appServiceManage('"+row.id+"')\">服务管理</a>";
//                        html += "<a class=\"m-btn\"  onclick=\"app.dialogDetail('"+row.id+"')\">应用标准</a>";
                        html += "<a class=\"m-btn-edit\" onclick=\"app.editorDialog('"+row.id+"','')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"app.delete('"+row.id+"','"+row.icon+"')\"></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();
            });
            $("#statusName").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,icon){
            var message = "确定要删除该应用信息吗？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    _deleteFileAfterUpdate(icon);

                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/app/deleteApp",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                app.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(),status:$('#statusName_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}/app/editorApp',
                //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}/app/appDetail',
                //load: true,
                urlParms: params
            });
        },
        appServiceManage: function (id) {
            indexPage.openChildPage("",'${contextRoot}/app/initAppService?appId='+id);
        },
    anthorize: function (id) {

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


    };

    function _deleteFileAfterUpdate(fileId){
        if(null == fileId || fileId.length == 0){
            return;
        }
        var url = "${dfs}/" + fileId;
        $.ajax({
            url: url,
            type: 'DELETE',
            async: false,
            success: function(){
                console.log("旧文件删除成功");
            }
        });
    }

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