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

    /* *************************** 模块初始化 ***************************** */
    var dict = {
        grid:null,
        grid1:null,
        $layout: $("#div_wrapper"),//layout对象
        $dictname: $("#dictname"),//字典输入框
        $dictItemName: $("#dictItemName"),//字典项输入框
        $btnADDdict: $("#btn_dict"),//新增字典
        $btnADDDictItem: $("#btn_dict_item"),//新增字典项
        $dictGrid: $("#dict_grid"),//字典datagrid
        $dictItemGrid: $("#dictItem_grid"),//字典datagrid
        init: function () {
            this.initLayout();
            this.initButton();
            this.initGrid();
        },
        initLayout: function () {
            this.$layout.ligerLayout({
                leftWidth: 380,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false//左边区域不可以隐藏
            });
            $("#div_wrapper .l-layout-left").css({"border": "0px"});
            $("#div_wrapper .l-layout-right").css({"border": "0px"});
            $("#div_wrapper .l-layout-center").css({"border": "0px"});
            $("#div_wrapper .l-layout-top").css({"border": "0px"});
            $("#div_wrapper .l-layout-left .l-layout-header").hide();

        },
        editorResourceRestDialog: function (id) {
            var title = "新增字典信息";
            var params = null;
            if (id != undefined && id != null) {
                title = "编辑字典信息";
                params = {id: id,mode:"modify"};
            }

            this.$dialog = $.ligerDialog.open({
                height: 520,
                width: 600,
                title: title,
                url: '${contextRoot}/dict/dictDialog',
                //load: true,
                urlParms: params
            });
        },
        deleteResourceRestDetail: function (id) {
            var message = "确定要删除字典吗？";
            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/dict/deleteDict",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                dict.$dictGrid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        editorResourceRestDetailDialog: function (id) {
            var title = "新增字典项信息";
            var params = null;
            if (id != undefined && id != null) {
                title = "编辑字典项信息";
                params = {id: id,mode:"modify"};

            }
            else if(dict.grid.getSelectedRow()){
                var t=dict.grid.getSelectedRow();

                params = {dictName: t.dictName};
            }

            this.$dialog = $.ligerDialog.open({
                height: 330,
                width: 600,
                title: title,
                url: '${contextRoot}/dict/dictItemDialog',
                //load: true,
                urlParms: params
            });
        },
       deleteResourceRest: function (id,dictName) {
            var message = "确定要删除该字典项吗？";
            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/dict/deleteDictItem",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                dict.$dictItemGrid.loadServerData({dictId:dictName});
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        initButton: function () {


            this.$btnADDdict.click(function(){
                dict.editorResourceRestDialog();
            });
            this.$btnADDDictItem.click(function(){
                dict.editorResourceRestDetailDialog();
            });
        },//弹窗返回
        dialogSuccess: function (message,reloadType) {
            $.ligerDialog.success(message);
            if("1"==reloadType){
                dict.$dictGrid.reload();
            }else{
                dict.$dictItemGrid.reload();
            }
            this.$dialog.close();
        },
        initGrid: function () {
            var me=this;
            $("#dictname").ligerSearch({
                onClick:function(value){

                    me.grid.set({
                        parms: {name:$('#dictname').val()}
                    });


                    me.grid.reload();
                }});
            $("#dictItemName").ligerSearch({
                onClick:function(value){

                    me.grid1.set({
                        parms: {name:$('#dictItemName').val()}
                    });

                    me.grid1.reload();
                }});
            me.grid=dict.$dictGrid= this.$dictGrid.ligerGrid({
                url: '${contextRoot}/dict/getDictList',
                columns: [
                    {display: '字典名称', name: 'chineseName', width: '70%'},
                    {
                        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=\"dict.editorResourceRestDialog('"+row.id+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"dict.deleteResourceRestDetail('"+row.id+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow:function(data,rowid,rowdata) {
                    dict.editorResourceRestDialog(data.id);
                },
                onSelectRow:function(rowdata, rowid, rowobj) {
                    dict.$dictItemGrid.setParm("dictId",rowdata.dictName);
                    dict.$dictItemGrid.reload();
                }
            });

            me.grid1=dict.$dictItemGrid=this.$dictItemGrid.ligerGrid({
                url: '${contextRoot}/dict/getDictItemList',
                columns: [
                    {display: '值域编码',  name: 'code', width: '40%', align: "center"},
                    {display: '值域名称', name: 'value', width: '40%', align: "center"},
                    {
                        display: '操作', name: 'operator', width: '17%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:80px;">';
                        html += "<a class=\"m-btn-edit\" onclick=\"dict.editorResourceRestDetailDialog('"+row.id+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"dict.deleteResourceRest('"+row.id+"','" + row.dictName + "')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow:function(data,rowid,rowdata) {
                    dict.editorResourceRestDetailDialog(data.id);
                }
            });
        },
    };

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