<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    var rsCategoryManage = {
        appInfoDialog: null,
        grid: null,
        init: function () {
            var me = this;
            $("#inp_search").ligerSearch({
                onClick:function(value){
                    me.reloadGrid();
                }
            });

            this.grid = $("#div_app_info_grid").ligerGrid({
                url: '${contextRoot}/resource/rsCategory/searchRsCategory',
                parms: {
                    page: 1,
                    rows: 100000
                },
                columns: [
                    {display: '名称', id: 'code', name: 'name', width: '45%', isAllowHide: false}, ,
                    {
                        display: '分类说明', name: 'remark', width: '45%', resizable: true
                    },
                    {
                        display: '操作', name: 'operator', width: '10%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:80px;">';
                        html += "<a class=\"m-btn-edit\" onclick=\"rsCategoryManage.editor('"+row.id+"', 'modify')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"rsCategoryManage.delete('"+row.id+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                unSetValidateAttr: false,
                scrollToPage: false,
                tree: {
                    columnId: 'code',
                    idField: 'id',
                    parentIDField: 'pid'
                },
                rownumbers: false,
                usePager: false,
                onDblClickRow:function(data,rowid,rowdata){
                    rsCategoryManage.editor(data.id,"modify");
                }
            });

            this.bindEvents();
        },
        dialogCallBack: function (message) {
            $.ligerDialog.success(message);
            rsCategoryManage.grid.reload();
            rsCategoryManage.appInfoDialog.close();
        },
        editor: function (id, mode) {
            var title = '';
            //只有new 跟 modify两种模式会到这个函数
            if (mode == 'modify') {
                title = '修改分类';
            }
            else {
                title = '新增分类';
            }

            rsCategoryManage.appInfoDialog = $.ligerDialog.open({
                height: 450,
                width: 600,
                title: title,
                url: '${contextRoot}/resource/rsCategory/rsCategoryInfo',
                urlParms: {
                    id: id,
                    mode: mode
                },
                isHidden: false,
                opener: true
            });
        },
        delete: function (id) {
            var message = "确定要删除该类别吗？";
            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm) {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url: "${contextRoot}/resource/rsCategory/deleteCategory",
                        dataType: "json",
                        data: {id: id},
                        cache: false,
                        success: function (data) {
                            if (data.successFlg) {
                                $.ligerDialog.success(data.message);
                                rsCategoryManage.grid.reload();
                            }
                            else {
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error: function (data) {
                            $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                        }
                    });
                }
            });
        },
        reloadGrid:function(){
            var me = this;
            me.grid.loadServerData({
                searchNm: $("#inp_search").val(),
                page: 1,
                rows: 100000
            });
        },
        bindEvents: function () {

            $('#btn_add').click(function () {
                rsCategoryManage.editor("", "new");
            })

        }
    }

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