<%@ 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 retrieve = null;
      var master = null;
      var conditionArea = null;
      var adapterPlanId = ${adapterPlanId};
      var entryRetrieve = null;
      var entryMater = null;
      var cfgModel = 0;
      var cfg = [
        {
          left:{title:'平台数据集', cls:'', search:'/adapterDataSet/searchAdapterDataSet', goAdd:'', del:''},
          right:{title:'数据元映射', cls:'', search:'/adapterDataSet/searchAdapterMetaData', goAdd:'/adapterDataSet/template/adapterMetaDataInfo', del:'/adapterDataSet/delMetaData',delLs:'/adapterDataSet/delMetaData'}
        },
        {
          left:{title:'平台字典', cls:'', search:'/adapterDict/searchAdapterDict', goAdd:'', del:''},
          right:{title:'字典项映射', cls:'', search:'/adapterDict/searchAdapterDictEntry', goAdd:'/adapterDict/template/adapterMetaDataInfo', del:'/adapterDict/delDictEntry',delLs:'/adapterDict/delDictEntry'}
        }
      ]
      /* *************************** 函数定义 ******************************* */
      function pageInit() {
        resizeContent();
        retrieve.init();
        conditionArea.init();
        entryRetrieve.init();
        master.init();
        master.reloadGrid();
        entryMater.init();
      }

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

      function resizeContent(){
        var contentW = $('#grid_content').width();
        var leftW = $('#div_left').width();
        $('#div_right').width(contentW-leftW-20);
      }
      /* *************************** title模块初始化 ***************************** */
      conditionArea = {
        $element: $('#conditionArea'),
        $adapter_plan_name :$('#adapter_plan_name'),
        $adapter_plan_parent :$('#adapter_plan_parent'),
        $adapter_plan_type :$('#adapter_plan_type'),
        $adapter_plan_org :$('#adapter_plan_org'),
        $btn_switch_dataSet :$('#switch_dataSet'),
        $btn_switch_dict :$('#switch_dict'),
        init : function () {
          this.initAdapterOrg();
          this.bindEvents();
          this.$element.show();
        },
        initAdapterOrg: function () {
          var self = this;
          var dataModel = $.DataModel.init();

          dataModel.fetchRemote("${contextRoot}/adapter/getAdapterPlanById",{
            data:{id:adapterPlanId},
            success: function(data) {
              var model = data.obj;
              self.$adapter_plan_name.val(model.name);
              self.$adapter_plan_parent.val(model.parentName);
              self.$adapter_plan_type.val(model.typeValue);
              self.$adapter_plan_org.val(model.orgValue);
            },
            error: function () {
              alert(1)
            }
          });
        },
        bindEvents : function () {
          this.$btn_switch_dataSet.click(function () {
            if(cfgModel==0)
              return;
            cfgModel = 0;
            retrieve.setTitle();
            entryRetrieve.setTitle();
            master.reloadGrid();
          })

          this.$btn_switch_dict.click(function () {
            if(cfgModel==1)
              return;
            cfgModel = 1;
            retrieve.setTitle();
            entryRetrieve.setTitle();
            master.reloadGrid();
          })
        }
      }

      /* *************************** left模块初始化 ***************************** */
      retrieve = {
        $element: $('#retrieve'),
        $searchNm: $('#searchNm'),
        $addBtn: $('#btn_create'),
        $title :$('#left_title'),
        init: function () {
          this.$searchNm.ligerTextBox({width: 200, isSearch: true, search: function () {
            master.reloadGrid();
          }});
          this.$element.show();
        },
        setTitle: function () {
            this.$title.html(cfg[cfgModel].left.title);
        }
      };

      master = {
        infoDialog: null,
        grid: null,
        init: function () {
          if(this.grid)
            return;
          this.grid = $("#div_left_grid").ligerGrid({
            url: '${contextRoot}'+cfg[cfgModel].left.search,
            columns: this.getColumn(),
            delayLoad:true,
            enabledEdit: true,
            validate : true,
            unSetValidateAttr:false,
            onBeforeShowData: function (data) {
              if(data.detailModelList.length==0){
                entryMater.reloadGrid('');
              }
            },
            onAfterShowData: function () {
              this.select(0);
            },
            onSelectRow: function(row){
              entryMater.reloadGrid();
            }
          });
          this.bindEvents();
        },
        reloadGrid: function () {
          var searchNm = $("#searchNm").val();
          var values = {
            strKey: searchNm,
            adapterPlanId: adapterPlanId
          };
          var url = '${contextRoot}' + cfg[cfgModel].left.search;
          reloadGrid.call(this, url, values, this.getColumn());
        },
        bindEvents: function () {
          $.subscribe('grid:left:open',function(event,id,mode){

          });

          $.subscribe('grid:left:delete',function(event,id) {

          });
        },
        getColumn: function () {
          var columnCfg =[];
          var code = 'code';
          var name = 'name';
          if(cfgModel==1){
            code = 'dictCode';
            name = 'dictName';
          }
          columnCfg = [
            { display: 'id', name: 'id', hide:true },
            { display: '代码', name: code,width: '50%', isAllowHide: false },
            { display: '名称',name: name, width: '50%',isAllowHide: false }
//            { display: '操作', name: 'operator', width: '34%', render: function (row) {
//              var html = '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","grid:left:open", row.id,'modify')+'">修改</a>' +
//                      ' / <a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}'])","grid:left:delete", row.id)+'">删除</a>';
//              return html;
//            }}
          ]
          return columnCfg;
        }
      };

      /* *************************** right模块初始化 ***************************** */
      entryRetrieve = {
        $element: $('#entryRetrieve'),
        $searchNm: $('#searchNmEntry'),
        $title: $('#right_title'),
        init: function () {
          this.$searchNm.ligerTextBox({width: 200, isSearch: true, search: function () {
            entryMater.reloadGrid();
          }});
          this.$element.show();
        },
        setTitle: function () {
          this.$title.html(cfg[cfgModel].right.title);
        }
      };

      entryMater = {
        entryInfoDialog: null,
        grid: null,
        init: function (dictId) {
          if(this.grid)
            return;
          this.grid = $("#div_relation_grid").ligerGrid({
            url: '${contextRoot}'+cfg[cfgModel].right.search,
            columns: this.getColumn(),
            delayLoad:true,
            enabledEdit: true,
            validate : true,
            unSetValidateAttr:false,
            checkbox:true,
            onDblClickRow : function (row){
              $.publish('grid:right:open',[row.id, 'modify']);
            }
          });
          this.bindEvents();
        },
        reloadGrid: function (parentId) {
          var searchNmEntry = $("#searchNmEntry").val();
          if(parentId!=''){
            var row = master.grid.getSelectedRow();
            if(row){
              parentId = row.id;
            }
          }
          var values = {
            adapterPlanId :adapterPlanId,
            strKey: searchNmEntry,
            dataSetId:parentId,
            dictId:parentId
          };
          reloadGrid.call(this, '${contextRoot}'+cfg[cfgModel].right.search, values, this.getColumn());
        },
        bindEvents: function () {
          $.subscribe('grid:right:open',function(event,id,mode){
            if(!master.grid.getSelectedRow()){
              if(cfgModel==0)
                $.Notice.warn('请先添加数据集数据！');
              else
                $.Notice.warn('请先添加平台字典数据！');
              return;
            }
            var title = mode == 'modify'?'修改' :'新增';
            title += cfg[cfgModel].right.title;
            entryMater.entryInfoDialog = $.ligerDialog.open({
              height:460,
              width: 400,
              title : title,
              url: '${contextRoot}'+cfg[cfgModel].right.goAdd,
              urlParms: {
                id:id,
                mode:mode
              },
              isHidden: false,
              opener: true
            });
          });

          $.subscribe('grid:right: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();
                dataModel.updateRemote('${contextRoot}'+cfg[cfgModel].right.delLs,{
                  data:{id:ids},
                  success:function(data){
                    $.Notice.success( '操作成功！');
                    entryMater.reloadGrid();
                  }
                });
              }
            })
          });
        },
        getColumn: function () {
          var columnCfg =[];
          if(cfgModel==0){
            columnCfg = [
              { display: 'id', name: 'id', hide:true },
              { display: 'metaDataId', name: 'metaDataId', hide:true },
              { display: '标准数据元代码', name: 'metaDataCode',width: '14%', isAllowHide: false },
              { display: '标准数据元名称',name: 'metaDataName', width: '14%',isAllowHide: false },
              { display: '机构数据表代码', name: 'orgDataSetCode',width: '14%', isAllowHide: false },
              { display: '机构数据表名称',name: 'orgDataSetName', width: '14%',isAllowHide: false },
              { display: '机构数据元代码', name: 'orgMetaDataCode',width: '14%', isAllowHide: false },
              { display: '机构数据元名称',name: 'orgMetaDataName', width: '14%',isAllowHide: false },
              { display: '操作', name: 'operator', width: '16%', render: function (row) {
                var html = '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}','{3}'])","grid:right:open", row.id,'modify')+'">修改</a>' +
                        ' / <a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}'])","grid:right:delete", row.id)+'">删除</a>';
                return html;
              }}
            ]
          }
          else{
            columnCfg = [
              { display: 'id', name: 'id', hide:true },
              { display: 'dictEntryId', name: 'dictEntryId', hide:true },
              { display: '标准字典项代码', name: 'dictEntryCode',width: '14%', isAllowHide: false },
              { display: '标准字典项值',name: 'dictEntryName', width: '14%',isAllowHide: false },
              { display: '机构字典代码', name: 'orgDictCode',width: '14%', isAllowHide: false },
              { display: '机构字典名称',name: 'orgDictName', width: '14%',isAllowHide: false },
              { display: '机构字典项代码', name: 'orgDictEntryCode',width: '14%', isAllowHide: false },
              { display: '机构字典项值',name: 'orgDictEntryName', width: '14%',isAllowHide: false },
              { display: '操作', name: 'operator', width: '16%', render: function (row) {
                var html = '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}','{3}'])","grid:right:open", row.id,'modify')+'">修改</a>' +
                        ' / <a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}'])","grid:right:delete", row.id)+'">删除</a>';
                return html;
              }}
            ]
          }
          return columnCfg;
        }
      };
      /* ******************Dialog页面回调接口****************************** */
      win.reloadEntryMasterGrid = function () {
          entryMater.reloadGrid();
      };
      win.getAdapterPlanId = function () {
        return adapterPlanId;
      };
      win.getParentId = function () {
        return master.grid.getSelectedRow().id;
      };
      win.getDialogOpener = function () {
        return cfgModel;
      };
      win.closeDialog = function (msg) {
        entryMater.entryInfoDialog.close();
        if(msg)
          $.Notice.success(msg);
      }
      /* *************************** 页面功能 **************************** */
      pageInit();
    });
  })(jQuery, window);
</script>
