<%--
  Created by IntelliJ IDEA.
  User: llh
  Date: 2016/1/7
  Time: 14:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="utf-8"%>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script >
  (function ($, win) {
    var adapterplan = {};
    var Util = $.Util;

    //集成标准版本处理
    adapterplan.version = {
          $addform: $("#div_version_info_form"),
          $seeform: $("#div_see_version_info_form"),
          $div_add_baseVersion: $("#div_add_baseVersion"),
          _getData:null,
          _url: $("#hd_url").val(),
          validator: null,
          baseversion_select: null,
          init: function () {

            var type = $.Util.getUrlQueryString('type');
            var schemeNm = $.Util.getUrlQueryString('schemeNm');
            var schemeId = Number($.Util.getUrlQueryString('schemeId').replace("scheme",""));
            var versionId = Number($.Util.getUrlQueryString('versionId').replace("version",""));
            var versionNm = $.Util.getUrlQueryString('versionNm');
            $("#type").val(type);
            $("#txt_schemeNm").text(schemeNm);//适配方案名称
            $("#lbl_schemeNm").text(schemeNm);//适配方案名称
            $("#sel_baseVersion").hide();
            //基础版本名称
            $("#sel_baseVersion").ligerComboBox({url : adapterplan.version._url+"/adapterCenter/getVersions?schemeId="+schemeId ,
              cancelable:false});

            if(type=="add"){//新增
                 $("#schemeId").val(schemeId);
                this.$addform.show();
            }else if(type=="modify"){//修改
                $("#txt_versionNm").val(versionNm);
                this.$addform.show();
                //基础版本名称
                this.$div_add_baseVersion.addClass("m-form-readonly");
                this.getAdapterVersionInfo(versionId,type);
            }else{//查看
                $("#txt_versionNm").text(versionNm);
                this.$seeform.show();
                this.getAdapterVersionInfo(versionId,type);
            }
            this.event();

            this.validator =  new $.jValidation.Validation(this.$addform, {immediate: true, onSubmit: false,
              onElementValidateForAjax:function(elm){

              }
            });
          },
          getAdapterVersionInfo: function (versionId,type) {//获取基础版本信息
               var u = adapterplan.version;
              $("#versionId").val(versionId+"");
              $.ajax({
                url: u._url + "/adapterCenter/getForVersionId",
                type: "get",
                dataType: "json",
                data: {versionId: versionId},
                success: function (data) {
                  if(data.successFlg){
                    
                    u._getData = data;

                    if (type != '') {//新增或修改
                        u.$addform.attrScan();
                        u.$addform.Fields.fillValues({
                          baseVersion:u._getData.baseVersion==null?"":u._getData.baseVersion
                        });
                    } else {//查看版本信息
                      $("#lbl_versionNm").text(u._getData.name);
                      if($.Util.isStrEmpty(u._getData.baseVersion)){
                        $("#lbl_primaryVersion").text("无");
                      }else{
                        
                        liger.get('sel_baseVersion').selectValue(u._getData.baseVersion)
                        $("#lbl_primaryVersion").text(liger.get('sel_baseVersion').getText());
                      }
                    }
                  }else{
                    $.ligerDialog.error(data.message);
                  }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                  $.ligerDialog.error("获取基础版本信息失败！")
                }
              })
      },
      save: function () {
        if(!this.validator.validate()){
          return;
        }
        $("#btn_save").html("处理中...").css("pointer-events","none");
        var _url = "";
        var resultData;
        adapterplan.version.$addform.attrScan();
        var jsonString = $.parseJSON(adapterplan.version.$addform.Fields.toJsonString());
        if( $("#type").val()=="add"){//新增保存
          var ob = {};
          ob.name = jsonString.name;
          ob.baseVersion = jsonString.baseVersion;
          ob.schemeId = Number($("#schemeId").val());
          resultData = ob;
          _url = adapterplan.version._url+"/adapterCenter/addVersion";
        }else if( $("#type").val()=="modify"){//修改保存
          var data = adapterplan.version._getData;
          data.name = jsonString.name;
          resultData = data;
          _url = adapterplan.version._url+"/adapterCenter/updateVersion";
        }

        $.ajax({
          url: _url,
          dataType: "json",
          data:{version:  JSON.stringify(resultData)},
          success: function (data) {
            $("#btn_save").html("确认").css("pointer-events","");
            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 (jqXHR, textStatus, errorThrown) {
            $("#btn_save").html("确认").css("pointer-events","");
            $.ligerDialog.error("保存失败！")
          }
        })
      },
      event: function () {
        $("#btn_save").click(function () {
          adapterplan.version.save();//保存
        });
        $("#btn_close").click(function () {
          parent.adapterplan.list.dialog_adapter_detail.close();
        });
        $("#btn_confirm").click(function () {
          parent.adapterplan.list.dialog_adapter_detail.close();
        });
      }
    };

    $(function(){
      adapterplan.version.init();
    })
  })(jQuery, window);
</script>
