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

<script type="text/javascript">

  (function ($, win) {

    /* ************************** 变量定义 ******************************** */
    var dialog = frameElement.dialog;
    //标准定制变量
    var adapterCustomize = null;
    var fromTree;
    var toTree;
    var Util = $.Util;

    /* ************************** 变量定义结束 **************************** */

    /* *************************** 函数定义 ******************************* */
    /**
     * 页面初始化
     */
    function pageInit() {
      adapterCustomize.init();
    }

    //来源树复选框点击事件
    function f_onFromTreeCheck(node, checked) {
      var fromTreeOption = {
        fromTree:fromTree,
        toTree:toTree,
        checkNodeItem:node,
        checked:checked,
        toTarget: "#tree_to",
        fromFlag: "S",
        toFlag: "A"
      };
      fromTree.f_onFromTreeCheck(fromTreeOption);
    }

    //目标树复选框点击事件
    function f_onToTreeCheck(node, checked) {
      var toTreeOption = {
        fromTree:fromTree,
        toTree:toTree,
        checkNodeItem:node,
        checked:checked,
        fromFlag: "A",
        toFlag: "S"
      };
      toTree.f_onToTreeCheck(toTreeOption);
    }
    /* ************************** 函数定义结束 **************************** */

    /* *************************** 模块初始化 ***************************** */
    adapterCustomize = {
      $treeFrom:$("#tree_from"),
      $treeTo:$("#tree_to"),
      $updateCustomizeBtn: $("#btn_save"),
      $cancelBtn: $("#btn_cancel"),
      TypeSearch: null,
      _schemeId:null,
      _adapterVersion:null,
      _stdVersion:null,
      init: function () {
        this.initForm();
        this.bindEvents();
        this.TypeSearch = $("#inp_search").ligerSearch({onClick: function (value) {
          fromTree.f_onQueryNode(fromTree,value);
        }});

      },
      initForm: function () {

        var self = this;
        //设置tree的默认属性
        var defaults = {
          idFieldName: 'id',
          parentIDFieldName: 'pid',
          isExpand: 2,
          parentIcon: null,
          childIcon: null,
          adjustToWidth: true,
          checkbox:true,
        };

        var name = $("#inp_search").val();
        var stdVersion = $.Util.getUrlQueryString('stdVersion');
        var adapterVersion = $.Util.getUrlQueryString('adapterVersion');
        var schemeId = $.Util.getUrlQueryString('schemeId').replace("scheme","");
        adapterCustomize._schemeId = schemeId;
        adapterCustomize._stdVersion = stdVersion;
        adapterCustomize._adapterVersion = adapterVersion;
        var stdDatasetData = [];
        var adapterDataSetData = [];

        $.ajax({
          url: "${contextRoot}/adapterCenter/getAdapterDataset",
          type: "get",
          dataType: "json",
          data: {adapterVersion:adapterVersion,stdVersion: stdVersion},
          success: function (data) {
            if(data.successFlg){
                var schemeDataset = data.detailModelList || [];;
                for(var i = 0;i<schemeDataset.length;i++){
                  var Id = schemeDataset[i].id;
                  if(Id.substring(0,1)=="S"){//定制标准数据集数据（左树）
                    stdDatasetData.push(schemeDataset[i]);
                  }else{//定制适配数据集数据（右树）
                    adapterDataSetData.push(schemeDataset[i]);
                  }
                }

                //来源树对象（左树）
                fromTree = self.$treeFrom.ligerTree($.extend({},defaults,{
                  data:stdDatasetData,
                  onCheck: f_onFromTreeCheck
                }));

                //目标树对象（右树）
                toTree = self.$treeTo.ligerTree($.extend({},defaults,{
                  data: adapterDataSetData,
                  onCheck: f_onToTreeCheck
                }));

            }else{
              $.ligerDialog.error(data.message);
            }



          }
        });

      },
      bindEvents: function () {
        var self = this;
        $("#inp_search").keyup(function (e) {
          if (e.keyCode == 13) {
            var value = $("#inp_search").val();
            fromTree.f_onQueryNode(fromTree,value);
          }
        });

        //修改用户的点击事件
        self.$updateCustomizeBtn.click(function () {

          var notes = toTree.getChecked();
          var note;
          var datasetIdStr = "";
          var metaDataIdStr = "";
          for (var i = 0; i < notes.length; i++) {
            note = notes[i].data;
            var Id = note.id;
            if(note.id!="S0" && Id.substring(0,2)=="AD"){//数据集ID
              datasetIdStr += Id.replace("AD","")+",";
            }else if(note.id!="S0" && Id.substring(0,2)=="AM"){//数据元ID
              metaDataIdStr += Id.replace("AM","")+",";
            }
          }
          var waittingDialog = $.ligerDialog.waitting("正在处理中，请稍候..");

          $.ajax({
            url: "${contextRoot}/adapterCenter/saveAdapterDataset",
            type: "post",
            dataType: "json",
            data: {schemeId: parseInt(self._schemeId),adapterVersion:self._adapterVersion,stdVersion:self._stdVersion,datasetIdStr:datasetIdStr,metaDataIdStr:metaDataIdStr},
            success: function (data) {
              waittingDialog.close();
              if(data.successFlg){
                $.ligerDialog.alert(data.message, "提示", "success", function () {
                  parent.adapterplan.list.getTypeList();
                  parent.adapterplan.list.dialog_adapter_detail.close()
                }, null);
              }else{
                $.ligerDialog.error(data.message);
              }

            },
            error : function () {
              waittingDialog.close();
              $.ligerDialog.error("保存失败！");
            }
          });

        });
        self.$cancelBtn.click(function () {
          dialog.close();
        });
      }

    };

    /* ************************* 模块初始化结束 ************************** */

    /* *************************** 页面初始化 **************************** */

    pageInit();
    /* ************************* 页面初始化结束 ************************** */

  })(jQuery, window);
</script>