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

    /* *************************** 模块初始化 ***************************** */
    var rule = {
        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();
            }});

            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/rule/getRuleList',
                parms: {
                    name: $('#txtName').val(),
                },
                checkbox:true,
                columns: [
                    {display: '名称', name: 'name', width: '10%'},
                    {display: '分类', id: 'type', name: 'type', width: '10%'},
                    {display: '规则描述', name: 'describe', width: '20%'},
                    {display: '脚本(或接口)', name: 'rule', width: '20%'},
                    {display: '统一错误代码', id: 'error_code', name: 'error_code', width: '20%'
                    },
                    {
                        display: '操作', name: 'operator', width: '20%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:350px;">';
                        html += "<a class=\"m-btn\" style=\"padding-right:10px\" onclick=\"rule.editorDialog('"+row.id+"','disabled')\">查看详情</a>";
                        html += "<a class=\"m-btn-edit\" onclick=\"rule.editorDialog('"+row.id+"','')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"rule.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();
            });
            $('#div_delete_record').click(function(){
                var rows = rule.grid.getSelecteds();
                if(rows.length > 0){
                    var idArr = [];
                    for(var i in rows){
                        idArr.push(rows[i].id);
                    }
                    rule.deleteBatch(idArr.join(","));
                }else{
                    $.ligerDialog.error("请选择要删除的规则信息！");
                    return false;
                }
            })
            $(".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}/rule/deleteRule",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                rule.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        // 批量删除
        deleteBatch: function (idList) {
            var message = "确定要删除选中的规则信息吗？";
            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/rule/deleteRuleBatch",
                        dataType : "json",
                        data:{idList: idList},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {idList
                                $.ligerDialog.success(data.message);
                                rule.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: 400,
                width: 500,
                title: title,
                url: '${contextRoot}/rule/editorRule',
                //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}/rule/ruleDetail',
                //load: true,
                urlParms: params
            });
        },
    anthorize: function (id) {

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


    };

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