<%@ page import="com.yihu.hos.system.model.SystemUser" %><%--
  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.version = {
        $addform: $("#div_version_info_form"),
        $seeform: $("#div_see_version_info_form"),
        _getData:null,
        _url: $("#hd_url").val(),
        validator: null,
        baseversion_select: null,
        init: function () {
          var id = $.Util.getUrlQueryString('id').replace("standard","");
          var standardId = $.Util.getUrlQueryString('standardId')==null?"":$.Util.getUrlQueryString('standardId').replace("standard","");
          var name = $.Util.getUrlQueryString('name');
          var code = $.Util.getUrlQueryString('code');
          var type = $.Util.getUrlQueryString('type');
          $("#type").val(type);
          $("#id").val(id);
          $("#standardId").val(standardId);
          $("#txt_stdname").text(name);
          $("#txt_stdcode").text(code);
          $("#txt_seestdname").text(name);

          if(type=="0"){//新增
            this.$addform.show();
            this.getBaseVersionList(id);//获取基础版本下拉框信息
          }else if(type=="1"){//修改
            this.$addform.show();
            $("#div_baseVersion").show();
            $("#div_publisher").show();
            $("#div_abaseVersion").hide();
            this.getStandardVersionInfo(id,type);
          }else{//查看
            this.$seeform.show();
            this.getStandardVersionInfo(id,type,code);
          }
          this.event();

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

            }
          });
        },
        getStandardVersionInfo: function (versionId,type,code) {//获取标准版本信息
          var u = standard.version;
            versionId = Number(versionId.replace("version",""));
            $("#versionId").val(versionId+"");
          $.ajax({
                url: u._url + "/standardCenter/getForVersionId",
                type: "get",
                dataType: "json",
                data: {versionId: versionId},
                success: function (data) {
                    if(data.successFlg){
                        u._getData = data;
                        u.getBaseVersionList($("#standardId").val(),type);//获取基础版本下拉框信息

                        if(type == ""){//查看标准版本信息
                            $("#lbl_version").text(code);
                            $("#lbl_primaryVersion").text(u._getData.primaryVersion);
                            <%
                             SystemUser user = (SystemUser) session.getAttribute("userInfo");
                             String systemName = user.getUserName();
                            %>
                            var userName = "<%=systemName%>";
                            if($.Util.isStrEmpty(u._getData.publishUser)) {
                                $("#lbl_publishUser").text(userName);
                            }else{
                                $("#lbl_publishUser").text(u._getData.publishUser);
                            }
                            $("#lbl_seename").text(u._getData.name);
                            if($.Util.isStrEmpty(u._getData.publishTime)){
                                $("#lbl_publishTime").text("");
                            }else{
                                $("#lbl_publishTime").text(u.getDate(u._getData.publishTime));
                            }

                            if(u._getData.publishStatus==0){
                                $(".lbl_publishStatus").text("未发布");
                            }else{
                                $(".lbl_publishStatus").text("已发布");
                            }

                        }else if(type =="1"){//修改
                            u.$addform.attrScan();
                            u.$addform.Fields.fillValues({
                                name: u._getData.name,
                            });


                            if(u._getData.publishStatus==0){
                                $(".lbl_publishStatus").text("未发布");
                            }else{
                                $(".lbl_publishStatus").text("已发布");
                            }
                        }
                    }else{
                        $.ligerDialog.error(result.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;
            
            standard.version.$addform.attrScan();
            var jsonString = $.parseJSON(standard.version.$addform.Fields.toJsonString());
              if( $("#type").val()=="0"){//新增保存
                  
                  var ob = {};
                  ob.name = jsonString.name;
                  ob.baseVersion = $("#baseVersion").ligerComboBox().getValue();
                  ob.standardId = Number($("#id").val().replace("standard",""));
                  ob.version = $(".lbl-version").val();
                  ob.path = "";
                  ob.primaryVersion = "";
                  ob.publishStatus = 0;
                  ob.publishUser = "";

                  resultData = ob;
                  _url = standard.version._url+"/standardCenter/addVersion";
              }else if( $("#type").val()=="1"){//修改保存
                  var data = standard.version._getData;
                  data.name = jsonString.name;
                  resultData = data;
                  _url = standard.version._url+"/standardCenter/updateVersion";
              }
                debugger
            $.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.standard.list.getTypeList();//刷新数据
                            parent.standard.list.dialog_standard_detail.close();
                        }, null);
                    }else{
                        $.ligerDialog.error(data.message);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $("#btn_save").html("确认").css("pointer-events","");
                     $.ligerDialog.error("保存失败！")
                }
            })
        },
        getBaseVersionList: function (id,type) {//获取基础版本信息
            var u = standard.version;
            $("#baseVersion").ligerComboBox({url : standard.version._url+"/standardCenter/getVersions?standardId="+id ,
                cancelable:false,
                onSuccess:function(data){
                    debugger
                    if(type==""){//查看
                        if($.Util.isStrEmpty(u._getData.baseVersion)){//标准版本名称
                            $("#lbl_primaryVersion").text("无");
                        }else{
                            $("#baseVersion").ligerComboBox()._setValue(u._getData.baseVersion);
                            $("#lbl_primaryVersion").text($("#baseVersion").ligerComboBox().getText());
                        }
                    }else{//修改
                        if($.Util.isStrEmpty(u._getData.baseVersion)){
                            $("#lbl_baseVersion").text("无");
                        }else{
                            $("#baseVersion").ligerComboBox()._setValue(u._getData.baseVersion);
                            $("#lbl_baseVersion").text($("#baseVersion").ligerComboBox().getText());
                        }
                    }

            }});
        },
       getDate:function(time_num){
           var d = new Date(time_num);
           var temp_time = this.formatDate(d);
           return temp_time;
       },
      formatDate:function(now){
          var   year=now.getYear()+1900;
          var   month=now.getMonth()+1>9?now.getMonth()+1:"0"+parseInt(now.getMonth()+1);
          var   date=now.getDate()>9?now.getDate():"0"+now.getDate();
          var   hour=now.getHours()>9?now.getHours():"0"+now.getHours();
          var   minute=now.getMinutes()>9?now.getMinutes():"0"+now.getMinutes();
          var   second=now.getSeconds()>9?now.getSeconds():"0"+now.getSeconds();
          return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;
      },
      event: function () {
          $("#btn_save").click(function () {
            standard.version.save();//保存
          });
          $("#btn_close").click(function () {
            parent.standard.list.dialog_standard_detail.close();
          });
            $("#btn_confirm").click(function () {
                parent.standard.list.dialog_standard_detail.close();
            });
        }
      };

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