<%--
  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" %>
<link href="${contextRoot}/develop/lib/select2/select2.min.css" rel="stylesheet">
<script src="${contextRoot}/develop/lib/select2/select2.js"></script>

<script >
  (function ($, win) {
        var standard = {};
        var Util = $.Util;
        var dictId = "";

        //集成标准版本处理
        standard.metadata = {
          _url: $("#hd_url").val(),
          $type_form: $("#div_metadata_info_form"),
          $div_see_metadata_info_form:$("#div_see_metadata_info_form"),
          validator: null,
          init: function () {
            
            var u = standard.metadata;
            var datasetId =  $("#dataset_id").val();
            var type = $("#type").val();
            var version =  $("#std_version").val();
            var metadataId =  $("#metadata_id").val();

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

            if(type=="modify" || type==""){//修改/查看
              $.ajax({
                url: u._url + "/standardCenter/getForMetadataId",
                type: "get",
                dataType: "json",
                data: {datasetId: datasetId,stdVersion:version,metadataId:metadataId},
                success: function (data) {
                  if(data.successFlg){
                    if(type=="modify"){//修改数据元信息
                      u.$type_form.attrScan();
                      u.$type_form.Fields.fillValues({
                        code: data.code,
                        name: data.name,
                        type: data.type,
                        format: data.format,
                        columnName: data.columnName,
                        columnType:data.columnType,
                        definition: data.definition,
                        deCode: data.deCode,
                        standardId: data.standardId,
                        dictId:data.dictId==null?"":data.dictId,
                        columnLength:data.columnLength
                      });

                      dictId = data.dictId==null?"":data.dictId;
                      $("#ck_primaryKey").prop('checked', false);
                      if (data.primaryKey == 1) {
                        $("#ck_primaryKey").prop('checked', true);
                      }
                      $("#ck_nullable").prop('checked', false);
                      if (data.nullable == 1) {
                        $("#ck_nullable").prop('checked', true);
                      }
                    }else{//查看数据元信息
                      u.$type_form.attrScan();
                      u.$type_form.Fields.fillValues({
                        dictId:data.dictId==0?"":data.dictId,
                      });
                      dictId = data.dictId==null?"":data.dictId;
                      $("#see_name").text(data.name);
                      $("#see_type").text(data.type);
                      $("#see_code").text(data.code);
                      $("#see_format").text(data.format);
                      $("#see_deCode").text(data.deCode);
                      $("#see_definition").text(data.definition);
                      $("#see_columnName").text(data.columnName);
                      $("#see_columnLength").text(data.columnLength);
                      $("#see_columnType").text(data.columnType);

                      $("#see_ck_primaryKey").prop('checked', false);
                      if (data.primaryKey == 1) {
                        $("#see_ck_primaryKey").prop('checked', true);
                      }
                      $("#see_ck_nullable").prop('checked', false);
                      if (data.nullable == 1) {
                        $("#see_ck_nullable").prop('checked', true);
                      }
                    }
                    u.setDictValue(version);
                  }else{
                    $.ligerDialog.error(data.message);
                  }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                  $.ligerDialog.error("获取数据元信息失败！")
                }
              })
            }
            else{//新增
              u.setDictValue(version);
            }
            this.event();

            this.validator =  new $.jValidation.Validation(this.$type_form, {immediate: true, onSubmit: false,
              onElementValidateForAjax:function(elm){
              }
            });
          },
          setDictValue:function(version){
            var self = this;
            $.ajax({
              url:self._url+"/standardCenter/getDicts?stdVersion="+version,
              type:"post",
              dataType: "json",
              success:function (data) {
                if(data.successFlg){
                  var option = "";
                  var list = data.detailModelList;
                  for(var i=0;i<list.length;i++){
                    option += '<option value="' +list[i].code+'">'+list[i].value+'</option>';
                  }
                  $("#sel_dict").html(option);
                  $("#sel_dict").val(dictId);
                  $("#sel_dict").select2();
                  $("#see_dict").text($("#sel_dict").find("option:selected").text());

                }

              }
            });
          },
          save: function () {
            if(!this.validator.validate()){
              return;
            }
            
            var _url = "";
            var id = $("#metadata_id").val();
            var datasetId = $("#dataset_id").val();
            var standardId = $("#standard_id").val();
            standard.metadata.$type_form.attrScan();
            var dataJson = eval("[" + standard.metadata.$type_form.Fields.toJsonString() + "]");
            //TODO 下面写保存标准的后台逻辑，dataJson[0]为传到后台的数据
            dataJson[0]["datasetId"] = Number(datasetId);
            dataJson[0]["standardId"] = Number(standardId);
            dataJson[0]["primaryKey"] = $("#ck_primaryKey").prop("checked")==true?1:0;
            dataJson[0]["nullable"] = $("#ck_nullable").prop("checked")==true?1:0;
            dataJson[0]["dictId"] = $("#sel_dict").val();
            if($("#type").val()=="modify"){//修改
              _url = $("#hd_url").val() + "/standardCenter/updateMetadata";
              dataJson[0]["id"] = Number(id);
            }else{//新增
              _url = $("#hd_url").val() + "/standardCenter/addMetadata";
            }
            $.ajax({
              url: _url,
              dataType: "json",
              data:{metadata: JSON.stringify(dataJson[0]),stdVersion:$("#std_version").val()},
              success: function (data) {
                if(data.successFlg){
                  $.ligerDialog.alert(data.message, "提示", "success", function () {
                    var row = parent.master.grid.getSelectedRow();
                    var parentId = "";
                    if(row){
                      parentId = row.id;
                    }
                    parent.entryMaster.reloadGrid(parentId);
                    parent.entryMaster.entryInfoDialog.close();
                  }, null);
                }else{
                  $.ligerDialog.error(data.message);
                }

              },
              error: function (jqXHR, textStatus, errorThrown) {
                $.ligerDialog.error("保存失败！")
              }
            })
          },
          event: function () {
            $("#btn_save").click(function () {
              standard.metadata.save();//保存
            });
            $("#btn_close").click(function () {//关闭
              parent.entryMaster.entryInfoDialog.close();
            });
            $("#btn_confirm").click(function () {
              parent.entryMaster.entryInfoDialog.close();
            });
            //给数据元主键和是否空值的选择
            $("#ck_primaryKey").click(function () {
              if ($(this).prop('checked')) {
                $('#ck_nullable').prop('checked', false);
              }
            });
            $('#ck_nullable').click(function () {
              if ($(this).prop('checked') && $('#ck_primaryKey').prop('checked')) {
                $.ligerDialog.error("主键不能为空！");
                $(this).prop('checked', false);
              }
            });
          }
        };

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

  })(jQuery, window)
</script>
