var Request = GetRequest()
var sessionId = null,
    patiCode = window.localStorage.getItem("severPatientCode"),
    patiName = null,
    consultCode = Request["consult"],
    kindType = Request["type"],//区分续方8或健康2
    page = 1,
    members = [];
var docInfo = JSON.parse(window.localStorage.getItem("severDocInfo"));

$(function(){
	recordAPI.getSessionId({consult:consultCode}).then(function(res){
		if(res.status == 200){
			var msg = JSON.parse(res.msg)[0];
			sessionId = msg.session_id;
		}
	}).then(function(){
		//获得会话参与人员
		recordAPI.getMembers(sessionId).then(function(res){
		    members = res;
		    getMessage();
		})
	})

    function getMessage(){
	    var params = {
	        page: 1,
	        pagesize: 1000,
	        consult:consultCode
	    }
	    recordAPI.getList(params).then(function(res){
	        var list = res.list.list
	        if (list && list.length > 0) {
	            var html = '',
	                length = list.length;
	            for (var j = length-1; j >= 0; j--) {
	                var reply = JSON.parse(list[j])
	                if(reply.content_type!=4 && reply.content_type!=14){//跳过教育指导
						html += formatMsg(reply);
					}
	            }
	          	plyr.setup();
	            $("#talkBox").append(html);
	        }
	    });
	}
    
    function formatMsg(reply){
	    var isSelf = (reply.sender_id == docInfo.code) ? true : false;
	    var isSystem = reply.sender_id == 'system';
	    var html = '';
	    var typeMsg = reply.content_type;
	    
	    if(typeMsg == 7 || typeMsg == 10 || typeMsg == 13){
	    	var content = reply.content;
	        html = template('sys_msg_tmp', {content: content});
	    }else if(typeMsg == 5){
	    	var con = JSON.parse(reply.content);
			if(reply.sender_id == docInfo.code){
				html = '<div class="time-tips"><span class="xt-xiaoxi">已向'+ con.doctor_name +'医生求助</span></div>';
			}
	    }else{
	    	//获取个人成员信息
		    var member;
		        for(var i=0; i<members.length; i++){
		            if(reply.sender_id == members[i].id){
		                member = members[i];
		                break;
		            }
		        }
		    var img = httpRequest.getImgUrl(member.avatar);
		    var content = '';
		        try{
		            content = JSON.parse(reply.content);
		        }catch(e){
		            if(typeMsg == 12){ //视频文件
		                var arr = reply.content.split(",");
		                content = {};
		                content.img = arr[0];
		                content.path = arr[1];
		                content.time = arr[2];
		            }else{
		                content = reply.content;
		            }
		        }
	        var obj = {
	            isSelf: isSelf,
	            time: new Date(reply.timestamp).format('yyyy-MM-dd HH:mm:ss'),
	            type: typeMsg,
	            kindType:kindType,//区分续方或健康
	            name: member.name,
	            img: img,
	            content: content
	        };
	        html = template('msg_tmp', obj);
	    }
	    return html;
	}
    
    template.helper('getSourceUrl', function(str){
        return httpRequest.getImgUrl(str);
    });
    
    $("#talkBox").on("click",".showBigPic",function(){
    	var $this = $(this)
    	layer.open({
    		type: 1,
	      	title:false,
	      	shadeClose: true,
	      	content:'<img style="max-width:100%" src="'+$this.attr('src')+'">'
    	})
    })
})
