(function ($) {
  var urls = {
    //His推送接口
    'get_push':'http://192.168.2.236/api/icss/emr/pushEMR',
    'get_info':'http://192.168.2.236/api/icss/emr/getIntroduceByEMR'
  };
  $.support.cors = true;
  var config = {
    width:'560px',      //推送内容显示的宽度
    num:'11',         //每行显示的最大个数
    mode:'horizontal',  //布局模式水平horizontal，垂直vertical,
    models:[5,6,7]       //需要显示的推送模块，主诉-诊断1-7
  };
  var urlSearch = parseUrl();
  handleConfig();
  //getPushData();

  function parseUrl() {
    var r = window.location.search.substr(1).split("&"),
      obj = {};
    $.each(r, function (i, v) {
      if (v) {
        var arr = v.split("=");
        obj[arr[0]] = arr[1]?decodeURI(arr[1]):'';
      }
    });
    return obj;
  };

  function handleConfig(){
    $(".container").css({'width':'560px'});
    getPushData();
    $('.'+config.mode).show();

  }
  function getPushData(){
    var url = urls.get_push;
    var myParam = {
      symptom: urlSearch.symptomJson||"",
      past: urlSearch.pastJson||"",
      other: urlSearch.otherJson||"",
      vital: urlSearch.vitalsJson||"",
      lis: JSON.parse(urlSearch.labsJson||null),
      pacs: urlSearch.pacsJson||"",
      diag: urlSearch.disJson||"",
      featureType: config.models.join(","),
      hosCode:'A001',
      age:urlSearch.age,
      sex:urlSearch.sexType
    };

    $.ajax({
      url: url,
      type:'post',
      dataType: "json",
      cache:false,
      contentType:"application/json",
      data:JSON.stringify(myParam),
      success:function(response){
        var data = response.data;
        if(response.code=='0'){
          fillPushData(data);
        }
      },
      error:function(error){
        console.log("error:"+error);
      },
      complete:function(){
        console.log("complete")
      }
    });
  }

  function getTreatInfo(type,name,detailName){
    var url = urls.get_info;
    var myParam = {
      icdCode:urlSearch.icd,
      type:type,
      name:name,
      detailName:detailName,
      hosCode:'A001',
      age:urlSearch.age,
      sex:urlSearch.sexType
    };

    $.ajax({
      url: url,
      type:'post',
      dataType: "json",
      cache:false,
      contentType:"application/json",
      data:JSON.stringify(myParam),
      success:function(response){
        var data = response.data;
        if(response.code=='0'){
          fillInfomation(data);
        }
      },
      error:function(error){
        console.log("error:"+error);
      },
      complete:function(){
        console.log("complete")
      }
    });
  }

  //填入提示信息
  function fillInfomation(data){
    var dom='';
    for(var i=0;i<data.length;i++){
      dom+='<div class="info-item clearfix"><span class="title">'+data[i].title+'：</span><p class="content">'+data[i].text+'</p></div>';
    }
    $(".box0 .item-content").html(dom);
  }

  //填入推送信息
  function fillPushData(data){
    if(!data||JSON.stringify(data)=='{}'){
      console.warn("获取推送数据为空！");
      return ;
    }
    var key='',modeClass='.'+config.mode;
    mapItem(modeClass+" .box5", data['lisList'],5);   //化验
    mapItem(modeClass+" .box6", data['pacsList'],6);   //辅捡
    data['dis']['可能诊断']&&data['dis']['可能诊断'].length>0&&mapItem(modeClass+" .box7", data['dis']['可能诊断'],7);   //初步诊断
    data['dis']['确诊']&&data['dis']['确诊'].length>0&&mapItem(modeClass+" .box8", data['dis']['确诊'],7);   //疑似诊断
    data['dis']['警惕']&&data['dis']['警惕'].length>0&&mapItem(modeClass+" .box9", data['dis']['警惕'],7);   //警惕

    $("a.info").click(function(e){
      getTreatInfo($(e.target).attr('type'),$(e.target).attr('name'),$(e.target).attr('detailName'))
    });

    //$(modeClass+" .box .item-content").html(str);         //治疗建议
  }
  //遍历数据
  function mapItem(dom,item,type){
    var itemDom = '';
    if(item){
      for(var i=0;i<item.length;i++){
        if(i>config.num-1){
          itemDom += '<p class="inner-item"><a href="##" class="hide">'+(item[i].name||item[i].uniqueName)+'</a><a class="info hide" target="_blank name="'+item[i].name+'" type='+type+' detail="'+item[i].detailName+'>i</a></p>';
        }else{
          itemDom += '<p class="inner-item"><a href="##">'+(item[i].name||item[i].uniqueName)+'</a><a class="info" target="_blank" name='+item[i].name+' type="'+type+'" detail="'+item[i].detailName+'">i</a></p>';
        }
      }
    }
    $(dom+" .item-content").html(itemDom||"无");
    $(dom).show();
    var hide = $(dom).find(".hide")[0];
    if(hide){
      $("<a class='more'>...</a>").insertBefore(hide);
      $(".more").click(function(e){
        $(e.target).siblings(".hide").removeClass("hide");
        $(e.target).hide();
      });
    }
  }
})(jQuery);