<%--
  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 standard = {};
      var Util = $.Util;

      //集成标准处理
      standard.attr = {
        _url: $("#hd_url").val(),
        _type:null,
        $form: $("#div_standard_info_form"),
        validator: null,
        init: function () {
         var u = standard.attr;
          var type = $.Util.getUrlQueryString('type');
          var typeFlg = $.Util.getUrlQueryString('typeFlg');
          var code = $.Util.getUrlQueryString('code');
          var name = $.Util.getUrlQueryString('name');
          standard.attr._type = type;

          if(type=="modify"){//修改
              if(typeFlg == 0){//集成标准，编码不可修改
                  $("#div_code").addClass("m-form-readonly");
              }
            var standardId = Number($.Util.getUrlQueryString('standardId').replace("standard",""));
            $("#standardId").val(standardId);
            $.ajax({
                  url: u._url + "/standardCenter/getForStandardId",
                  type: "get",
                  dataType: "json",
                  data: {standardId: standardId},
                  success: function (data) {
                      if(data.successFlg){
                          $("#lbl_publisher").text(data.publisher);
                          $("#lbl_publisher").attr("title",data.publisher);
                          $("#lbl_publisherOrgCode").text(data.publisherOrgCode);
                          u.$form.attrScan();
                          u.$form.Fields.fillValues({
                              code: data.code,
                              name: data.name,
                              refStandard: data.refStandard,
                              refStandardVersion: data.refStandardVersion,
                              summary: data.summary
                          });
                      }else{
                          $.ligerDialog.error(result.message);
                      }

                  },
                error: function (jqXHR, textStatus, errorThrown) {
                    $.ligerDialog.error("获取标准版本信息失败！")
                }
            })
          } else if (typeFlg == 0) {//集成标准
              $.ajax({
                  url: u._url + "/standardCenter/getSysParamList",
                  type: "get",
                  dataType: "json",
                  success: function (data) {
                      $("#txt_code").val(data.code);
                      $("#div_code").addClass("m-form-readonly");
                  },
                  error: function (jqXHR, textStatus, errorThrown) {
                      $.ligerDialog.error("获取集成标准系统参数失败！")
                  }
              })

              $.ajax({
                  url: u._url + "/standardCenter/getStandardPublisher",
                  type: "get",
                  dataType: "json",
                  success: function (data) {
                      if(data.successFlg){
                          $("#lbl_publisher").text(data.name);
                          $("#lbl_publisher").attr("title",data.name);
                          $("#lbl_publisherOrgCode").text(data.code);
                      }else{
                          $.ligerDialog.error(data.message);
                      }
                  },
                  error: function (jqXHR, textStatus, errorThrown) {
                      $.ligerDialog.error("获取机构信息失败！")
                  }
              })
          }else if(typeFlg == 1) {//应用标准
              $("#lbl_publisher").text(name);
              $("#lbl_publisherOrgCode").text(code);
          }
          this.event();

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

            }
          });
        },
        save: function () {
          if(!this.validator.validate()){
            return;
          }
            
          var _url = "";
          var id = $("#standardId").val();
          standard.attr.$form.attrScan();
          var dataJson =  eval("[" + standard.attr.$form.Fields.toJsonString() + "]");
          dataJson[0]["versionStatus"] = 0;
          dataJson[0]["publisher"] = $("#lbl_publisher").text();
          dataJson[0]["publisherOrgCode"] = $("#lbl_publisherOrgCode").text();

          if(standard.attr._type=="modify"){//修改
            _url = standard.attr._url + "/standardCenter/updateStandard";
            dataJson[0]["id"] = Number(id);
          }else{//新增
            _url = standard.attr._url + "/standardCenter/addStandard";
          }
          $.ajax({
              url: _url,
              dataType: "json",
              data:{standard: JSON.stringify(dataJson[0])},
              success: function (data) {
                  if(data.successFlg){
                      $.ligerDialog.alert(data.message, "提示", "success", function () {
                          parent.standard.list.getTypeList();//刷新数据
                          parent.standard.list.dialog_standard_detail.close();
                      }, null);
                  }else{
                      $.ligerDialog.error(data.message);
                  }
              },
              error: function (jqXHR, textStatus, errorThrown) {
                  $.ligerDialog.error("保存失败！")
              }
          })
        },
        event: function () {
          $("#btn_save").click(function () {
            standard.attr.save();//保存标准
          });
          $("#btn_close").click(function () {
            parent.standard.list.dialog_standard_detail.close();
          });
        }
      };

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

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