<%--
  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.dictEntry = {
      _url: $("#hd_url").val(),
      $type_form: $("#div_dict_entry_info_form"),
      $div_see_dict_entry_info_form:$("#div_see_dict_entry_info_form"),
      validator: null,
      init: function () {
        var u = standard.dictEntry;
        var dictId =  $("#dict_id").val();
        var type = $("#type").val();
        var version =  $("#std_version").val();
        var dictEntryId =  $("#dict_entry_id").val();
        if(type==""){//查看字典项信息
          u.$div_see_dict_entry_info_form.show();
        }else{
          u.$type_form.show();
        }

        if(type=="modify" || type==""){//修改/查看
          $.ajax({
            url: u._url + "/standardCenter/getForDictEntryId",
            type: "get",
            dataType: "json",
            data: {dictId: dictId,stdVersion:version,dictEntryId:dictEntryId},
            success: function (data) {
              if(data.successFlg){
                if(type=="modify"){
                  u.$type_form.attrScan();
                  u.$type_form.Fields.fillValues({
                    code: data.code,
                    value: data.value,
                    description: data.description
                  });
                }else{
                  $("#see_code").text(data.code);
                  $("#see_value").text(data.value);
                  $("#see_description").text(data.description==null?"":data.description);
                }
              }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 = $("#dict_entry_id").val();
        var dictId = $("#dict_id").val();
        var standardId = $("#standard_id").val();
        standard.dictEntry.$type_form.attrScan();
        var dataJson = eval("[" + standard.dictEntry.$type_form.Fields.toJsonString() + "]");
        //TODO 下面写保存标准的后台逻辑，dataJson[0]为传到后台的数据
        dataJson[0]["dictId"] = Number(dictId);
        dataJson[0]["standardId"] = Number(standardId);
        if($("#type").val()=="modify"){//修改
          _url = $("#hd_url").val() + "/standardCenter/updateDictEntry";
          dataJson[0]["id"] = Number(id);
        }else{//新增
          _url = $("#hd_url").val() + "/standardCenter/addDictEntry";
        }
        $.ajax({
          url: _url,
          dataType: "json",
          data:{dictEntry: 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.dictEntry.save();//保存
        });
        $("#btn_close").click(function () {//关闭
          parent.entryMaster.entryInfoDialog.close();
        });
        $("#btn_confirm").click(function () {//关闭
          parent.entryMaster.entryInfoDialog.close();
        });

      }
    };

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

  })(jQuery, window)
</script>
