<%@ 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.dict = {
      _url: $("#hd_url").val(),
      $type_form: $("#div_dict_info_form"),
      $div_see_dict_info_form:$("#div_see_dict_info_form"),
      init: function () {
        var u = standard.dict;
        var id =  $("#dict_id").val();
        var type = $("#type").val();
        var version =  $("#std_version").val();
        $("#sel_parentId").ligerComboBox({url : u._url+"/standardCenter/getDicts?stdVersion="+version ,
          cancelable:false,
         });

        if(type==""){//查看字典信息
          u.$div_see_dict_info_form.show();
        }else{
          u.$type_form.show();
        }

        if(type=="modify" || type==""){//修改/查看
          $.ajax({
            url: u._url + "/standardCenter/getForDictId",
            type: "get",
            dataType: "json",
            data: {dictId: 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,
                    define: data.define,
                    parentId:data.parentId==null?"":data.parentId,
                    description:data.description
                  });
                }else{
                  u.$type_form.attrScan();
                  u.$type_form.Fields.fillValues({
                    parentId:data.parentId==null?"":data.parentId,
                  });
                  $("#see_code").text(data.code);
                  $("#see_name").text(data.name);
                  $("#see_define").text(data.define==null?"":data.define);
                  $("#see_parentId").text($("#sel_parentId").ligerComboBox().getText());
                  $("#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_id").val();
        var standardId = $("#standard_id").val();
        standard.dict.$type_form.attrScan();
        var dataJson = eval("[" + standard.dict.$type_form.Fields.toJsonString() + "]");
        dataJson[0]["standardId"] = Number(standardId);
        //TODO 下面写保存标准的后台逻辑，dataJson[0]为传到后台的数据
        if($("#type").val()=="modify"){//修改
          _url = $("#hd_url").val() + "/standardCenter/updateDict";
          dataJson[0]["id"] = Number(id);
        }else{//新增
          _url = $("#hd_url").val() + "/standardCenter/addDict";
        }
        $.ajax({
          url: _url,
          dataType: "json",
          data:{dict: JSON.stringify(dataJson[0]),stdVersion:$("#std_version").val()},
          success: function (data) {
            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.dict.save();//保存
        });
        $("#btn_close").click(function () {//关闭
          parent.master.entryInfoDialog.close();
        });
        $("#btn_confirm").click(function () {//关闭
          parent.master.entryInfoDialog.close();
        });
      }
    };

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