<%@ 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;

      /* *************************** 函数定义 ******************************* */
      function pageInit() {
        retrieve.init();
        master.init();
      }

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

      /* *************************** 模块初始化 ***************************** */
      retrieve = {
        $element: $('.m-retrieve-area'),
        $searchNm: $('#inp_search'),
        $addBtn: $('#btn_add'),
        $multiDelBtn: $('#btn_multiDel'),

        init: function () {
          this.$searchNm.ligerTextBox({width: 240,isSearch:true, search: function () {
            master.reloadGrid();
          }});

          this.$element.show();

          this.$element.attrScan();
          window.form = this.$element;
        },
        delAdapterOrg: function (code) {
          var dataModel = $.DataModel.init();
          dataModel.updateRemote('${contextRoot}/adapterorg/delAdapterOrg',{
            data:{code: code},
            success:function(data){
              if (data.successFlg) {
                $.Notice.open({type: 'success', msg: '操作成功！'});
                master.reloadGrid();
              } else {
                $.Notice.open({type: 'success', msg: '删除失败！'});
                return;
              }
            }
          });
        },
        bindEvents: function () {
          $.subscribe('adapterOrg:adapterInfo:del',function(event,ids) {
            if(!ids){
              var rows = master.grid.getSelectedRows();
              if(rows.length==0){
                $.Notice.open({type: 'info', msg: '请选择要删除的数据行！'});
                return;
              }
              for(var i=0;i<rows.length;i++){
                ids += ',' + rows[i].id;
              }
              ids = ids.length>0 ? ids.substring(1, ids.length) : ids;
            }
            retrieve.delAdapterOrg(ids);
          });
        }
      };

      master = {
        adapterInfoDialog:null,
        grid: null,
        init: function () {
          var searchNm = $("#inp_search").val();

          this.grid = $("#div_adapter_info_grid").ligerGrid({
            url: '${contextRoot}/adapterorg/searchAdapterOrg',
            parms: {
              searchNm: searchNm
            },
            columns: [
              { display: '代码', name: 'code',width: '15%', isAllowHide: false },
              { display: '名称',name: 'name', width: '30%',isAllowHide: false },
              //{ display: '类别代码', name: 'type', hide:true, width: '10%'},
              { display: '类别', name: 'typeValue', width: '15%'},
              //{ display: '映射机构代码', name: 'org', width: '25%', hide:true},
              { display: '映射机构', name: 'orgValue', width: '20%',resizable: true},
              //{ display: '父级标准代码', name: 'parent', width: '10%',minColumnWidth: 20, hide:true},
              { display: '父级标准', name: 'parentValue', width: '20%',minColumnWidth: 20},
//              { display: '操作', name: 'operator', width: '15%', render: function (row) {
//                var html = '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","adapter:adapterInfo:manager", row.code,'manager')+'">维护 /</a>' +
//                           '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","adapter:adapterInfo:open", row.code,'modify')+'">修改 /</a>' +
//                           '<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","adapter:adapterInfo:del", row.code,'del')+'">删除</a>';
//                return html;
//              }}
            ],
            enabledEdit: true,
            validate : true,
            unSetValidateAttr:false,
            checkbox : true,
            onDblClickRow : function (row){
              $.publish('adapter:adapterInfo:open',[row.code,'modify']);
            }
          });
          this.bindEvents();
        },
        reloadGrid: function () {
          var values = retrieve.$element.Fields.getValues();
          reloadGrid.call(this, '${contextRoot}/adapterorg/searchAdapterOrg', values);
        },
        bindEvents: function () {
          $.subscribe('adapter:adapterInfo:manager',function(event,code,mode){
            location.href = '${contextRoot}/orgdataset/initial?adapterOrg='+code;
          });
          $.subscribe('adapter:adapterInfo:open',function(event,code,mode) {

            var title = '';
            //只有new 跟 modify两种模式会到这个函数
            if(mode == 'modify'){
              title = '修改采集标准信息';
            }
            else{
              title = '新增采集标准信息';
            }
            master.adapterInfoDialog = $.ligerDialog.open({
              height:550,
              width: 400,
              title : title,
              url: '${contextRoot}/adapterorg/template/adapterOrgInfo',
              urlParms: {
                code:code,
                mode:mode
              },
              isHidden: false,
              opener: true
            });
          });
          $.subscribe('adapter:adapterInfo:del',function(event,code) {
            if(!code){
              var rows = master.grid.getSelectedRows();
              if(rows.length==0){
                $.Notice.warn( '请选择要删除的数据行！');
                return;
              }
              for(var i=0;i<rows.length;i++){
                code += ',' + rows[i].code;
              }
              code = code.length>0 ? code.substring(1, code.length) : code;
            }
            
            $.Notice.confirm('确认删除所选数据？', function (r) {
              if(r){
                var dataModel = $.DataModel.init();
                dataModel.updateRemote('${contextRoot}/adapterorg/delAdapterOrg',{
                  data:{code:code},
                  success:function(data){
                    $.Notice.success( '操作成功！');
                    master.reloadGrid();
                  }
                });
              }
            })
          });
        },
      };

      /* ******************Dialog页面回调接口****************************** */
      win.reloadMasterGrid = function () {
        master.reloadGrid();
      };
      win.closeDialog = function (msg) {
        master.adapterInfoDialog.close();
        if(msg)
          $.Notice.success(msg);
      }
      /* *************************** 页面功能 **************************** */
      pageInit();
    });
  })(jQuery, window);

</script>