<%--
  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.attr = {
      _url: $("#hd_url").val(),
      _type:null,
      $form: $("#div_adapterplan_info_form"),
      validator: null,
      init: function () {
        var u = adapterplan.attr;
        var type = $.Util.getUrlQueryString('type');
        adapterplan.attr._type = type;
        this.event(type);
        if(type=="modify"){//修改
          $("#div_stdId").addClass("m-form-readonly");
          $("#div_stdVersion").addClass("m-form-readonly");
          $("#div_adapterStdId").addClass("m-form-readonly");
          $("#div_adapterPublisherOrgCode").addClass("m-form-readonly");
          $("#div_adapterStdVersion").addClass("m-form-readonly");

          $("#sel_stdId").removeClass("required");
          $("#sel_stdVersion").removeClass("required");
          $("#sel_stdPublisherOrgCode").removeClass("required");
          $("#sel_publisherOrgCode").removeClass("required");
          $("#sel_adapterStdId").removeClass("required");
          $("#sel_adapterStdVersion").removeClass("required");
          $(".div-essential").removeClass("essential");

          var schemeId = Number($.Util.getUrlQueryString('id').replace("scheme",""));
          $("#scheme_id").val(schemeId+"");
          $.ajax({
            url: u._url + "/adapterCenter/getForSchemeId",
            type: "get",
            dataType: "json",
            data: {schemeId: schemeId},
            success: function (data) {
              if(data.successFlg){
                u.$form.attrScan();
                u.$form.Fields.fillValues({
                  name: data.name,
                  description: data.description,
                  stdId: data.stdId,
                  stdVersion: data.stdVersion,
                  stdPublisherOrgCode:data.stdPublisherOrgCode,
                  adapterStdId: data.adapterStdId,
                  adapterStdVersion: data.adapterStdVersion,
                  adapterPublisherOrgCode:data.adapterPublisherOrgCode,
                  id:data.id,
                  parentId:data.parentId,
                  type:data.type,
                  status:data.status
                });
                liger.get('sel_stdId').selectValue(data.stdId);
                liger.get('sel_stdVersion').selectValue(data.stdVersion);
                liger.get('sel_adapterStdId').selectValue(data.adapterStdId);
                liger.get('sel_adapterStdVersion').selectValue(data.adapterStdVersion);
              }else{
                $.ligerDialog.error(data.message);
              }

            },
            error: function (jqXHR, textStatus, errorThrown) {
              $.ligerDialog.error("获取适配方案信息失败！")
            }
          })
        }

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

          }
        });
      },
        save: function () {
          if(!this.validator.validate()){
            return;
          }
          
          var _url = "";
          adapterplan.attr.$form.attrScan();
          var dataJson =  eval("[" + adapterplan.attr.$form.Fields.toJsonString() + "]");
          if(adapterplan.attr._type=="modify"){//修改
            _url = adapterplan.attr._url + "/adapterCenter/updateScheme";
          }else{//新增
            _url = adapterplan.attr._url + "/adapterCenter/addScheme";
          }
          $.ajax({
            url: _url,
            dataType: "json",
            data:{scheme: JSON.stringify(dataJson[0])},
            success: function (data) {
              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) {
              $.ligerDialog.error("保存失败！")
            }
          })
        },
        event: function (type) {
          $("#btn_save").click(function () {
            adapterplan.attr.save();//保存适配
          });
          $("#btn_close").click(function () {
            parent.adapterplan.list.dialog_adapter_detail.close();
          });
          //目标标准名称
//          $("#sel_stdId").ligerComboBox({url : adapterplan.attr._url+"/standardCenter/getStandards" ,
//            urlParms:{
//               condition: encodeURIComponent(JSON.stringify({
//                 "type":0
//               }))
//             },
//            cancelable:false,
//            onSuccess:function(data){
//              if(type !="modify" && $("#sel_stdId").val()=="" && data!=null&&data.length>0)
//              {
//                this.selectValue(data[0].code);
//              }
//              return false;
//            }});

          //目标机构名称
          $("#sel_stdPublisherOrgCode").ligerComboBox({url : adapterplan.attr._url+"/standardCenter/getPublishers" ,
            cancelable:false,
            onSuccess:function(data){
              if(type !="modify" && data!=null&&data.length>0)
              {
                this.selectValue(data[0].code);
              }
              return false;
            }});

          $("#sel_stdId").ligerComboBox({cancelable:false});
          $("#sel_stdVersion").ligerComboBox({cancelable:false});

          //目标机构名称change事件
          $("#sel_stdPublisherOrgCode").change(function(){
            var publisherOrgCode = $("#sel_stdPublisherOrgCode").ligerGetComboBoxManager().getValue();
            if(!Util.isStrEmpty(publisherOrgCode)){
              //目标标准名称
              $("#sel_stdId").ligerComboBox({url : adapterplan.attr._url+"/standardCenter/getStandards" ,//?condition={\"type\":1,\"publisherOrgCode\":\""+publisherOrgCode+"\"}
                urlParms:{
                  condition: encodeURIComponent(JSON.stringify({
                    "type":1,
                    "publisherOrgCode":publisherOrgCode
                  }))
                },
                cancelable:false,
                onSuccess:function(data){
                  if(type !="modify" && $("#sel_stdId").val()=="" &&data!=null&&data.length>0)
                  {
                    this.selectValue(data[0].code);
                  }
                  return false;
                }});
            }
          });

          //目标标准名称change事件
          $("#sel_stdId").change(function(){
            var standardId = $("#sel_stdId").ligerGetComboBoxManager().getValue();
            if(!Util.isStrEmpty(standardId)){
              //平台标准版本
              $("#sel_stdVersion").ligerComboBox({url : adapterplan.attr._url+"/standardCenter/getVersions" ,//?standardId="+standardId+"&condition="+JSON.stringify({"publishStatus":1})
                urlParms:{
                  standardId:standardId,
                  condition: encodeURIComponent(JSON.stringify({"publishStatus":1}))
                },
                cancelable:false,
                onSuccess:function(data){
                  if(type !="modify" && $("#sel_stdVersion").val()=="" && data!=null&&data.length>0)
                  {
                    this.selectValue(data[0].code);
                  }
                  return false;
                }});
            }
          });

          //目标机构名称
          $("#sel_publisherOrgCode").ligerComboBox({url : adapterplan.attr._url+"/standardCenter/getPublishers" ,
            cancelable:false,
            onSuccess:function(data){
              if(type !="modify" && data!=null&&data.length>0)
              {
                this.selectValue(data[0].code);
              }
              return false;
            }});
          $("#sel_adapterStdId").ligerComboBox({cancelable:false});
          $("#sel_adapterStdVersion").ligerComboBox({cancelable:false});

          //源机构名称change事件
          $("#sel_publisherOrgCode").change(function(){
            var publisherOrgCode = $("#sel_publisherOrgCode").ligerGetComboBoxManager().getValue();
            debugger
            if(!Util.isStrEmpty(publisherOrgCode)){
              //源标准名称
              $("#sel_adapterStdId").ligerComboBox({url : adapterplan.attr._url+"/standardCenter/getStandards" ,//?condition={\"type\":1,\"publisherOrgCode\":\""+publisherOrgCode+"\"}
                urlParms:{
                  condition: encodeURIComponent(JSON.stringify({
                    "type":1,
                    "publisherOrgCode":publisherOrgCode
                  }))
                },
                cancelable:false,
                onSuccess:function(data){
                  if(type !="modify" && $("#sel_adapterStdId").val()=="" &&data!=null&&data.length>0)
                  {
                    this.selectValue(data[0].code);
                  }
                  return false;
                }});
            }
          });

          //源标准名称change事件
          $("#sel_adapterStdId").change(function(){
            var standardId = $("#sel_adapterStdId").ligerGetComboBoxManager().getValue();
            //源标准版本
            if(!Util.isStrEmpty(standardId)){
              $("#sel_adapterStdVersion").ligerComboBox({url : adapterplan.attr._url+"/standardCenter/getVersions" ,//?standardId="+standardId +"&condition="+JSON.stringify({"publishStatus":1})
                urlParms:{
                  standardId:standardId,
                  condition:encodeURIComponent(JSON.stringify({"publishStatus":1}))
                },
                cancelable:false,
                onSuccess:function(data){
                  if(type !="modify" && $("#sel_adapterStdVersion").val()=="" &&data!=null&&data.length>0)
                  {
                    this.selectValue(data[0].code);
                  }

                  return false;

                }});
            }

          })

        }
      };

      $(function(){
        adapterplan.attr.init();
      })

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