(function ($) {
        var urls = {
            url:{
                'host':"/icss-web",
                'start_drug': '/rule_controller/start_drug_title',
                //His推送接口
                'get_push_data':'/kl/diseaseinfo/get_data',
                'recovery_index': '/at/inquiry_info/recover',
                'get_patient_from_his': '/at/patientinfo/get_patient_from_his'
            }
        };
        var urlSearch = parseUrl();
        var isRecover = urlSearch.recover;
        if(isRecover){
            getRecover();
        }else{
            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]] = encodeURI(arr[1]);
                }
            });
            return obj;
        };
        function getPushData(){
            var url = urls.url.host + urls.url.get_push_data;
            var code = urlSearch.diagnosis;
            var hospitalCode = urlSearch.hospitalCode;
            ajaxJsonGet(url,{code:code,hospitalCode:hospitalCode},function(response){
                var data = response.data;
                if(response.status=='OK'){
                    fillPushData(data);
                }
            });
        }
        //获取历史数据
        function getRecover(){
            var patientNo = urlSearch.patientId;
            var recordId = urlSearch.recordId;
            var hospitalCode = urlSearch.hospitalId;//urls.url.host
            var url =  urls.url.host + urls.url.recovery_index;
            var pUrl = urls.url.host + urls.url.get_patient_from_his;
            ajaxJsonGet(pUrl,{hospitalCode:hospitalCode,hisCode:patientNo},function(response){
                var data = response.data;
                if(data){
                    $.get(url,{patientId:data.id,hospitalCode:hospitalCode,hisCode:recordId},function(response){
                        var data = response.data;
                        if(response.status=='OK'){
                            parseRecoverData(data);
                        }
                    });
                }else{
                    console.warn("获取患者信息为空");
                }
            });

        }

        //解析recover数据
        function parseTextRecoverData(data){
            var json = JSON.parse(data[0].dataJson);
            var items = json.structuredText.items;
            var test = items[5].symptoms&&items[5].symptoms.split("，");
            var sug = items[6].symptoms&&items[6].symptoms.split("，");
            var treat = items[7].symptoms.split("，");
            var drug = items[8].symptoms.split("，");
            var treatStr='';
            if(test.length==0){
                test = json.hisPush.test.map((it) =>{
                   return it.name;
                });
            }
            if(sug.length==0){
                sug = json.hisPush.assist.map((it) =>{
                    return it.name;
                });
            }
            for(var i=0; i<treat.length;i++){
                if(i>3){
                    treatStr += '<b class="hide">'+treat[i]+'</b>';
                }else{
                    treatStr += '<b>'+treat[i]+'</b>';
                }
            }
            $(".main-suit").html(treatStr);
            mapRecoverData(".test-item",test,true);
            mapRecoverData(".sug-item",sug,true);
            mapRecoverData(".treatment",drug,true);
            var hide = $(".main-suit .hide")[0];
            if(hide){
                $("<a class='more'>...</a>").insertBefore(hide);
                $(".more").click(function(e){
                    $(e.target).siblings(".hide").removeClass("hide");
                    $(e.target).hide();
                })
            }
        }
        function parseRecoverData(data){
            if(!data[0]){
                console.warn("获取的recover数据为空");
                return ;
            }
            var json = JSON.parse(data[0].dataJson);
            if(json.mode==2){
                parseTextRecoverData(data);
                return;
            }
            var items = json.structured.originalItems;
            var test = items[5][''];
            var sug = items[6][''];
            var treat = items[7][''];
            var drug = items[8][''];
            var treatStr='',infoUrl;
            test = test.length==0?json.hisPush.test:test;
            sug = sug.length==0?json.hisPush.assist:sug;
            //console.log(json)
            for(var i=0; i<treat.length;i++){
                infoUrl = "./case_info.html?diseaseId="+treat[i].id;   //文本模式保存的没有id
                if(i>3){
                    treatStr += '<b class="hide">'+treat[i].title+'</b><a class="info hide" href="'+infoUrl+'" target="_blank">i</a>';
                }else{
                    treatStr += '<b>'+treat[i].title+'</b><a class="info" href="'+infoUrl+'" target="_blank">i</a>';
                }
            }
            $(".main-suit").html(treatStr);
            mapRecoverData(".test-item",test);
            mapRecoverData(".sug-item",sug);
            mapRecoverData(".treatment",drug);
            var hide = $(".main-suit .hide")[0];
            if(hide){
                $("<a class='more'>...</a>").insertBefore(hide);
                $(".more").click(function(e){
                    $(e.target).siblings(".hide").removeClass("hide");
                    $(e.target).hide();
                })
            }

        }

        function mapRecoverData(dom,data,isText){
            var itemDom = "",item = data,temp;
            if(!data.length>0){
                console.warn("获取数据失败");
                return ;
            }
            for(var i=0;i<item.length;i++){
                if(isText){
                    temp = item[i];
                }else{
                    temp = item[i].title||item[i].name;
                }
                if(i>7){
                    itemDom += '<a href="##" class="hide">'+temp+'</a>';
                }else{
                    itemDom += '<a href="##">'+temp+'</a>';
                }
            }
            $(dom).html(itemDom||"无");
            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();
                })
            }
        }
        //填入推送信息
        function fillPushData(data){
            if(!data||JSON.stringify(data)=='{}'){
                console.warn("获取推送数据为空！");
                return ;
            }
            var lis = data.lis;
            var pacs = data.pacs;
            var drug = data.drug;
            var disease = data.disease;
            var infoUrl,str='';

            for(var i=0;i<disease.length;i++){
                infoUrl="./case_info.html?diseaseId="+disease[i].id;
                str += '<b>'+disease[i].name+'</b><a class="info" href="'+infoUrl+'" target="_blank">i</a>';
            }
            $(".main-suit").html(str);
            mapItem(".test-item", lis);
            mapItem(".sug-item", pacs);
            mapItem(".drug-item", drug);

        }
        //遍历数据
        function mapItem(dom,item){
            var itemDom = '';
            for(var i=0;i<item.length;i++){
                if(i>5){
                    itemDom += '<a href="##" class="hide">'+item[i]+'</a>';
                }else{
                    itemDom += '<a href="##">'+item[i]+'</a>';
                }
            }
            $(dom).html(itemDom||"无");
            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();
                })
            }
        }
        function ajaxJsonGet(url, param, callback){
            $.ajax({
                url: url,
                data:param,
                contentType: 'application/json',
                type:'get',
                cache:false,
                success:function(response){
                    callback(response);
                }
            });
        }

    })(jQuery);