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

      /* ************************** 全局变量定义 **************************** */
      var Util = $.Util;
      var dictRetrieve = null;
      var dictMaster = null;
      var conditionArea = null;

      var entryRetrieve = null;
      var entryMater = null;
      /* *************************** 函数定义 ******************************* */
      function pageInit() {
        resizeContent();
        dictRetrieve.init();
        conditionArea.init();
        entryRetrieve.init();
      }

      function resizeContent(){
        var contentW = $('#grid_content').width();
        var leftW = $('#div_left').width();
        $('#div_right').width(contentW-leftW-20);
      }

      function reloadGrid (url, params) {
        this.grid.set({
          url: url,
          parms: params
        });
        this.grid.reload();
      }


      /* *************************** 标准字典模块初始化 ***************************** */
      conditionArea = {
        $element: $('#conditionArea'),
        $stdDictVersion : $('#stdDictVersion'),

        init : function () {
          this.initDDL(this.$stdDictVersion);
          this.$element.show();
        },
        initDDL: function (target) {
          var dataModel = $.DataModel.init();
          dataModel.fetchRemote("${contextRoot}/standardsource/getVersionList",{
            success: function(data) {
              target.ligerComboBox({
                valueField: 'code',
                textField: 'value',
                data: data.detailModelList,
                initValue:1,
                allowBlank: false,
                onSelected: function (value,text) {
                  dictMaster.init();
                }
              });
              var manager = target.ligerGetComboBoxManager();
              manager.selectItemByIndex(0);

            }});
        }
      }

      dictRetrieve = {
        $element: $('#dictRetrieve'),
        $searchNm: $('#searchNm'),
        $addBtn: $('#btn_create'),

        init: function () {

          this.$searchNm.ligerTextBox({width: 200, isSearch: true, search: function () {
            dictMaster.reloadGrid();
          }});
          this.$element.show();

          this.$element.attrScan();
          window.form = this.$element;
        }
      };

      dictMaster = {
        dictInfoDialog: null,
        grid: null,
        init: function () {
          var searchNm = $("#searchNm").val();
          var stdDictVersion = $("#stdDictVersion").ligerGetComboBoxManager().getValue();
          this.grid = $("#div_stdDict_grid").ligerGrid({
            url: '${contextRoot}/cdadict/getCdaDictList',
            parms: {
              searchNm: searchNm,
              strVersionCode : stdDictVersion
            },
            columns: [
              { display: 'id', name: 'id', hide:true },
              { display: '代码', name: 'code',width: '50%', isAllowHide: false },
              { display: '名称',name: 'name', width: '49%',isAllowHide: false },
//              { display: '操作', name: 'operator', width: '33%', render: function (row) {
//                var html = '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","stddict:dictInfo:open", row.id,'modify')+'">修改</a>' +
//                        ' / <a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}'])","stddict:dictInfoGrid:delete", row.id)+'">删除</a>';
//                return html;
//              }}
            ],
            enabledEdit: true,
            validate : true,
            unSetValidateAttr:false,
            onAfterShowData: function () {
              this.select(0);
            },
            onSelectRow: function(row){
              entryMater.init();
              entryMater.reloadGrid();
            }
          });
          this.bindEvents();
          // 自适应宽度
          this.grid.adjustToWidth();
        },
        reloadGrid: function () {
          var searchNm = $("#searchNm").val();
          var stdDictVersion = $("#stdDictVersion").ligerGetComboBoxManager().getValue();
          var values = {
            searchNm: searchNm,
            strVersionCode : stdDictVersion};
          reloadGrid.call(this, '${contextRoot}/cdadict/getCdaDictList', values);
        },
        bindEvents: function () {
          $.subscribe('stddict:dictInfo:open',function(event,id,mode){
            var title = '';
            //只有new 跟 modify两种模式会到这个函数
            if(mode == 'modify'){
              title = '修改标准字典';
            }
            else{
              title = '新增标准字典';
            }
            var stdDictVersion = $("#stdDictVersion").ligerGetComboBoxManager().getValue();
            dictMaster.dictInfoDialog = $.ligerDialog.open({
              height:462,
              width: 400,
              title : title,
              url: '${contextRoot}/cdadict/template/stdDictInfo',
              urlParms: {
                dictId: id,
                mode:mode,
                strVersionCode:stdDictVersion
              },
              isHidden: false,
              opener: true
            });
          });

          $.subscribe('stddict:dictInfoGrid:delete',function(event,id) {
            $.Notice.confirm('确认要删除所选数据？', function (r) {
              if(r){
                var dataModel = $.DataModel.init();
                var stdDictVersion = $("#stdDictVersion").ligerGetComboBoxManager().getValue();
                dataModel.updateRemote('${contextRoot}/cdadict/deleteDict',{
                  data:{dictId:id, cdaVersion:stdDictVersion},
                  success:function(data){
                    $.Notice.success( '操作成功！');
                    dictMaster.reloadGrid();
                  }
                });
              }
            })
          });
        }
      };

      entryRetrieve = {
        $element: $('#entryRetrieve'),
        $searchNm: $('#searchNmEntry'),

        init: function () {
          this.$searchNm.ligerTextBox({width: 200, isSearch: true, search: function () {
            entryMater.reloadGrid();
          }});
          this.$element.show();

          this.$element.attrScan();
          window.form = this.$element;
        }
      };

      entryMater = {
        entryInfoDialog: null,
        grid: null,
        init: function (dictId) {
          if(this.grid)
            return;
          this.grid = $("#div_relation_grid").ligerGrid({
            url: '${contextRoot}/cdadict/searchDictEntryList',
            columns: [
              { display: 'id', name: 'id', hide:true },
              { display: 'dictId', name: 'dictId', hide:true },
              { display: '代码', name: 'code',width: '50%', isAllowHide: false },
              { display: '名称',name: 'value', width: '49%',isAllowHide: false },
//              { display: '操作', name: 'operator', width: '33%', render: function (row) {
//                var html = '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}','{3}'])","entry:dictInfo:open", row.id,row.dictId,'modify')+'">修改</a>' +
//                        ' / <a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}'])","entry:dictInfoGrid:delete", row.id)+'">删除</a>';
//                return html;
//              }}
            ],
            delayLoad:true,
            enabledEdit: true,
            validate : true,
            unSetValidateAttr:false,
            checkbox:true,
            onDblClickRow : function (row){
              $.publish('entry:dictInfo:open',[row.id, row.dictId, 'modify']);
            }
          });
          this.bindEvents();
          // 自适应宽度
          this.grid.adjustToWidth();
        },
        reloadGrid: function () {
          var searchNmEntry = $("#searchNmEntry").val();
          var stdDictVersion = $("#stdDictVersion").ligerGetComboBoxManager().getValue();
          var dictId = dictMaster.grid.getSelectedRow().id;
          var values = {
            searchNmEntry: searchNmEntry,
            strVersionCode : stdDictVersion,
            dictId:dictId
          };
          reloadGrid.call(this, '${contextRoot}/cdadict/searchDictEntryList', values);
        },
        bindEvents: function () {
          $.subscribe('entry:dictInfo:open',function(event,id,dictId,mode){
            if(!dictMaster.grid.getSelectedRow()){
              $.Notice.warn('请先添加标准字典数据！');
              return;
            }
            var title = '';
            //只有new 跟 modify两种模式会到这个函数
            if(mode == 'modify'){
              title = '修改字典详情';
            }
            else{
              dictId = dictMaster.grid.getSelectedRow().id;
              title = '新增字典详情';
            }
            var stdDictVersion = $("#stdDictVersion").ligerGetComboBoxManager().getValue();
            entryMater.entryInfoDialog = $.ligerDialog.open({
              height:300,
              width: 400,
              title : title,
              url: '${contextRoot}/cdadict/template/dictEntryInfo',
              urlParms: {
                id:id,
                dictId:dictId,
                mode:mode,
                strVersionCode:stdDictVersion
              },
              isHidden: false,
              opener: true
            });
          });

          $.subscribe('entry:dictInfoGrid:delete',function(event,ids) {
            if(!ids){
              var rows = entryMater.grid.getSelectedRows();
              if(rows.length==0){
                $.Notice.warn( '请选择要删除的数据行！');
                return;
              }
              for(var i=0;i<rows.length;i++){
                ids += ',' + rows[i].id;
              }
              ids = ids.length>0 ? ids.substring(1, ids.length) : ids;
            }

            $.Notice.confirm('确认要删除所选数据？', function (r) {
              if(r){
                var dataModel = $.DataModel.init();
                var stdDictVersion = $("#stdDictVersion").ligerGetComboBoxManager().getValue();
                dataModel.updateRemote('${contextRoot}/cdadict/deleteDictEntryList',{
                  data:{id:ids,cdaVersion:stdDictVersion},
                  success:function(data){
                    $.Notice.success( '操作成功！');
                    entryMater.reloadGrid();
                  }
                });
              }
            })

          });
        },
        closeDl : function () {
          this.entryInfoDialog.close();
        }
      };
      /* ******************Dialog页面回调接口****************************** */
      win.reloadMasterGrid = function () {
        dictMaster.reloadGrid();
      };
      win.getStrVersion = function () {
        return $("#stdDictVersion").ligerGetComboBoxManager().getValue();
      };
      win.reloadEntryMasterGrid = function () {
        entryMater.reloadGrid();
      };
      win.closeDialog = function (type, msg) {
        if(type=='right')
          entryMater.closeDl();
        else
          dictMaster.dictInfoDialog.close();
        if(msg)
          $.Notice.success(msg);
      };
      /* *************************** 页面功能 **************************** */
      pageInit();
    });
  })(jQuery, window);
</script>
