<%@ 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 adapterGrid=null;
      var searchNm;
      var treeFrom = null;
      var treeTo = null;
      var adapterDataSet=null;
      var planId=null;
      var addFlg;

      /* *************************** 函数定义 ******************************* */
      function pageInit() {
        retrieve.init();
        master.init();
      }
      function getAdapterPlan(target,id,mode){
        var title = '';
        //只有new 跟 modify两种模式会到这个函数
        if(mode == 'modify'){
          title = '修改基础信息';
        }  else if(mode == 'new'){
          title = '新增基础信息';
        }
        target.adapterInfoDialog = $.ligerDialog.open({
          height:580,
          width: 440,
          title : title,
          url: '${contextRoot}/adapter/getAdapterPlan',
          urlParms: {
            id: id,
            mode:mode
          },
          isHidden: false,
          opener: true
        });
      }

      function delAdapterPlan(id){
        var dataModel = $.DataModel.init();
        dataModel.updateRemote('${contextRoot}/adapter/delAdapterPlan', {
          data: {id: [].concat(id)},
          success: function(data) {
            if(data.successFlg){
              //todo:信息提醒不行
              //$.Notice.open({type: 'success', msg: '操作成功！'});
              //重新刷新Grid
              master.reloadGrid(searchNm);
            }else{
              //$.Notice.open({type: 'error', msg: data.errorMsg});
            }
          }});
      }

      /* *************************** 模块初始化 ***************************** */
      retrieve = {
        $element: $('.m-retrieve-area'),
        $searchBox: $('#ipt_search'),
        $addBtn: $('#btn_add'),
        $deleteBtn: $('#btn_del'),

        init: function () {
          this.$searchBox.ligerTextBox({
            width: 240, isSearch: true,
            search: function () {
              master.reloadGrid(searchNm);
            },
            onChangeValue:function(value){
              searchNm=value||"";
            }
          });
          this.$element.show();
          this.$element.attrScan();
          window.form = this.$element;
        },
        bindEvents: function () {
          //
        }
      };

      master = {
        adapterInfoDialog: null,
        adapterCustomize:null,
        init: function () {
          adapterGrid = $("#div_adapter_info_grid").ligerGrid({
            url: '${contextRoot}/adapter/searchAdapterPlan',
            parms: {
              searchNm: ""
            },
            columns: [
              { display: 'ID',name: 'id', hide: true },
              { display: '方案代码', name: 'code', width: '15%', minColumnWidth: 60 },
              { display: '方案名称', name: 'name',width: '20%' },
              { display: '初始方案代码', name: 'parentCode', width: '15%', minColumnWidth: 60 },
              { display: '初始方案名称', name: 'parentName',width: '15%' },
              { display: '类别', name: 'typeValue',width: '5%'},
              { display: '类别代码', name: 'type', hide: true},
              { display: '映射机构/区域代码', name: 'org', hide: true},
              { display: '映射机构/区域', name: 'orgValue', width: '20%'},
              { display: '标准版本', name: 'versionName',width: '10%' },
              { display: '标准版本代码', name: 'version', hide: true},
//              { display: '操作', name: 'operator', width: '15%',render: function (row) {
//                var html ='<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","adapter:adapterInfo:customize", row.id,row.version)+'">定制</a>'+
//                        '/<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","adapter:adapterInfo:adapter", row.id,'modify')+'">适配</a>'+
//                        '/<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}','{2}'])","adapter:adapterInfo:open", row.id,'modify')+'">修改</a>'+
//                        '/<a href="#" onclick="javascript:'+Util.format("$.publish('{0}',['{1}'])","adapter:adapterInfo:delete", row.id)+'">删除</a>' ;
//                return html;
//              }}
            ],
            checkbox:true,
            allowHideColumn:false,
            onDblClickRow : function (row){
              //
            }
          });
          this.bindEvents();
        },
        reloadGrid: function (searchNm) {
          adapterGrid.setOptions({parms: {searchNm: searchNm}});
          //重新查询
          adapterGrid.loadData(true);
        },
        bindEvents: function () {
          var self = this;
          //新增适配方案
          retrieve.$addBtn.click(function () {
            getAdapterPlan(self,'','new');
          });
          //批量删除适配方案
          retrieve.$deleteBtn.click(function () {
            var rows = adapterGrid.getSelectedRows();
            if (rows.length>0){
              $.ligerDialog.confirm('确定删除这些方案信息?', function (yes) {
                if (yes){
                  var id=[];
                  for(var i= 0;i<rows.length;i++){
                    id[i]=rows[i].id;
                  }
                  delAdapterPlan(id);
                }
              });
            }
          });
          //修改适配方案
          $.subscribe('adapter:adapterInfo:open',function(event,id,mode){
            getAdapterPlan(self,id,mode);
          });
          //删除适配方案
          $.subscribe('adapter:adapterInfo:delete',function(event,id){
            $.ligerDialog.confirm('确定删除该方案信息?', function (yes) {
              if (yes) {
                delAdapterPlan(id);
              }
            });
          });
          //定制适配方案
          $.subscribe('adapter:adapterInfo:customize',function(event,id,version){
            self.adapterCustomize = $.ligerDialog.open({
              height:500,
              width: 700,
              title : '标准定制',
              url: '${contextRoot}/adapter/getAdapterCustomize',
              urlParms: {
                planId: id,
                version:version
              },
              isHidden: false,
              opener: true
            });
          });
          //适配
          $.subscribe('adapter:adapterInfo:adapter',function(event,id,mode){
            location.href= '${contextRoot}/adapterDataSet/initial?adapterPlanId='+id;
//            $(".adapter").show();
          });
        }
      };

      /* ******************Dialog页面回调接口****************************** */
      win.reloadAdapterMasterGrid = function () {
        master.reloadGrid(searchNm);
      };
      /* *************************** 页面功能 **************************** */
      pageInit();

    });
  })(jQuery, window);

</script>