<%@ 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.dataset = {
        _url: $("#hd_url").val(),
        $type_form: $("#div_dataset_info_form"),
        $div_see_dataset_info_form:$("#div_see_dataset_info_form"),
        init: function () {
          var u = standard.dataset;
          var id =  $("#dataset_id").val();
          var type = $("#type").val();
          var version =  $("#std_version").val();

          if(type==""){//查看数据集信息
            u.$div_see_dataset_info_form.show();
          }else{
            u.$type_form.show();
          }

          if(type=="modify" || type==""){//修改/查看
            $.ajax({
              url: u._url + "/standardCenter/getForDatasetId",
              type: "get",
              dataType: "json",
              data: {datasetId: id,stdVersion:version},
              success: function (data) {
                if(data.successFlg){
                  if(type=="modify" ){
                    u.$type_form.attrScan();
                    u.$type_form.Fields.fillValues({
                      code: data.code,
                      name: data.name,
                      summary: data.summary,
                      language:data.language,
                      catalog:data.catalog,
                      classModel:data.classModel
                    });
                  }else{
                    $("#see_code").text(data.code);
                    $("#see_name").text(data.name);
                    $("#see_summary").text(data.summary);
                    $("#see_language").text(data.language);
                    $("#see_catalog").text(data.catalog);
                    $("#see_classModel").text(data.classModel);
                  }
                }else{
                  $.ligerDialog.error(data.message);
                }

              },
              error: function (jqXHR, textStatus, errorThrown) {
                $.ligerDialog.error("获取数据集信息失败！")
              }
            })
          }
          this.event();

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

            }
          });
        },
        save: function () {
          if(!this.validator.validate()){
            return;
          }
          var _url = "";
          var id = $("#dataset_id").val();
          var standardId = $("#standard_id").val();
          standard.dataset.$type_form.attrScan();
          var dataJson = eval("[" + standard.dataset.$type_form.Fields.toJsonString() + "]");
          dataJson[0]["standardId"] = Number(standardId);
          //TODO 下面写保存标准的后台逻辑，dataJson[0]为传到后台的数据
          if($("#type").val()=="modify"){//修改
            _url = $("#hd_url").val() + "/standardCenter/updateDataset";
            dataJson[0]["id"] = Number(id);
          }else{//新增
            _url = $("#hd_url").val() + "/standardCenter/addDataset";
          }
          
          $.ajax({
            url: _url,
            dataType: "json",
            data:{dataset: JSON.stringify(dataJson[0]),stdVersion:$("#std_version").val(),standardId:$("#standard_id").val()},
            success: function (data) {
              debugger
              if(data.successFlg){
                $.ligerDialog.alert(data.message, "提示", "success", function () {
                  parent.master.reloadGrid();
                  parent.master.entryInfoDialog.close();
                }, null);
              }else{
                $.ligerDialog.error(data.message);
              }

            },
            error: function (jqXHR, textStatus, errorThrown) {
              $.ligerDialog.error("保存失败！")
            }
          })
        },
        event: function () {
          $("#btn_save").click(function () {
            standard.dataset.save();//保存
          });
          $("#btn_close").click(function () {//关闭
            parent.master.entryInfoDialog.close();
          });
          $("#btn_confirm").click(function () {//关闭
            parent.master.entryInfoDialog.close();
          });

        }
      };

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