! function() {
	var httpData = GetRequest();
	new Vue({
		el: "#app",
		data: {
			imgData: {
				accept: 'image/gif, image/jpeg, image/png, image/jpg',
			},
			isCollapse: false,
			upImgs: [],
			upImgArr: [],
			planid: httpData['planid'],
			service: {
				relationRecordImg:{},
				messageList:{}
				}
		},
		mounted: function() {
			this.getServiceItem();
		},
		methods: {
			zwkf: function() {
				top.layer.msg('此功能暂未开放', {
					icon: 1
				});
			},
			jkjy: function(patient, planId) {
				var vm = this
				top.layer.open({
					type: 2,
					//				  offset: ['100px'], //右下角弹出
					area: ['80%', '648px'],
					shade: 0.5,
					title: '健康教育',
					fixed: true, //不固定
					maxmin: true,
					closeBtn: 1,
					shift: 5,
					shadeClose: false, //点击遮罩关闭层
					content: '../../article/html/article.html?code=' + patient + '&planId=' + planId + '&isReha=true'
				});
			},
			//						健康指导
			jkzd: function(planId,patient) {
				var vm = this
				top.layer.open({
					type: 2,
					//				  offset: ['100px'], //右下角弹出
					area: ['80%', '648px'],
					shade: 0.5,
					title: '健康教育',
					fixed: true, //不固定
					maxmin: true,
					closeBtn: 1,
					shift: 5,
					shadeClose: false, //点击遮罩关闭层
					content: '../../guidance/html/index.html#/person-edit-panel?patient=' + patient + '&planId=' + planId
				});
			},
			qwapp: function() {
				top.layer.msg('请前往app处理', {
					icon: 5
				});
			},
			getServiceItem: function() {
				var vm = this;
				var loginDoctor = JSON.parse(window.localStorage.getItem('wlyyAgent'));
				var loadding = top.layer.load(0, {
					shade: false
				}); //0代表加载的风格，支持0-2
				rehaAPI.serviceItem({
					planDetailId: vm.planid
				}).then(function(res) {
					top.layer.close(loadding);
					if(res.status == 200) {
						vm.service = res.data;
						var isZK=loginDoctor.doctorType==1?true:false;
						vm.service.isZK=isZK;
						if(vm.service.isMyTask==1){//执行者										
							vm.service.isCK=true;//无法显示按钮
							if(loginDoctor.uid==vm.service.specialistDoctorCode){//说明我自己是创建者
								vm.service.isCK=false;
								vm.service.isCreate=true;
							}
						}else{
							if(loginDoctor.uid==vm.service.specialistDoctorCode){//说明我自己是创建者
								vm.service.isCreate=true;
							}
						}
					} else {
						top.layer.msg(res.msg, {
							icon: 5
						});
					}
					if(vm.service.status==1){
						var index = top.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						top.layer.title('服务记录详情', index)  //再改变当前层的标题						
					}
				})
			},
			zdly: function(data) {
				var vm = this
				var loginDoctor = JSON.parse(window.localStorage.getItem('wlyyAgent'));
				var otherCode, otherName;
				var planDetailId = data.planDetaiId;
				if(loginDoctor.doctorType == 1) {
					otherCode = data.familyDoctorCode;
					otherName = data.familyDoctorName;
				} else {
					otherCode = data.specialistDoctorCode;
					otherName = data.specialistDoctorName;
				}
				top.layer.open({
					type: 2,
					//				  offset: ['100px'], //右下角弹出
					area: ['80%', '600px'],
					shade: 0.5,
					title: '指导留言',
					fixed: true, //不固定
					maxmin: true,
					closeBtn: 1,
					shift: 5,
					shadeClose: false, //点击遮罩关闭层
					content: '../../consulting/html/consulting.html?otherCode=' + otherCode + '&otherName=' + encodeURI(otherName) + '&planDetailId=' + planDetailId,
					end: function() { // 未点击确定按钮，点击关闭按钮  
						　　　　　　
						vm.getServiceItem();　　　　
					}
				});
			},
			deleteImg: function(idx) {
				console.log(idx)
				this.upImgs.splice(idx, 1)
			},
			chakan: function() {
				this.isCollapse = !this.isCollapse;
			},
			upLoadImgToBase64: function() {
				var vm = this;
				var img1 = event.target.files[0];
				console.log(img1)
				var reader = new FileReader();
				var type = img1.type; //文件的类型，判断是否是图片  
				var size = img1.size; //文件的大小，判断图片的大小  
				if(this.imgData.accept.indexOf(type) == -1) {
					alert('请选择我们支持的图片格式！');
					return false;
				}
				if(size > 3145728) {
					alert('请选择3M以内的图片！');
					return false;
				}
				reader.readAsDataURL(img1);
				reader.onload = function(e) {
					vm.upImgs.push({
						baseUrl: e.target.result
					});
					vm.upImgArr.push(img1);
					//			             		vm.uploadImg(vm.upImgArr);
				}
			},
			qrwc: function(patient,type) {
				var vm = this;
				vm.saveRehabilitationOperateRecord(patient,type);
			},
			saveRehabilitationOperateRecord:function(patient,type){
	        	var vm=this;
	        	var userInfo = JSON.parse(window.localStorage.getItem(httpRequest.agentName)); 
	        	var _type;
	        	if(type==0){
	        		_type=5
	        	}
	        	if(type!=0){
	        		vm.uploadImg(vm.upImgArr, vm.updateNoteAndImageRehabilitationOperate);
	        		return ;//只有上传需要新增
	        	} 
	        	var params={
	        		rehabilitationDetailId:vm.planid,
					node:null,
					patientCode:patient,
					doctorCode:userInfo.uid,
					relationRecordType:_type,
					relationRecordCode:'',
					relationRecordImg:null,
					status:0
	        	}
	        	rehaAPI.saveRehabilitationOperateRecord({dataJson:JSON.stringify(params)}).then(function(res){
	        		var icon=5;
	        		if(res.status==200){
	        			icon=1;
	        			vm.uploadImg(vm.upImgArr, vm.updateNoteAndImageRehabilitationOperate);
	        		}
	        	})
	        	
	        },
			//上传图片
			uploadImg: function(data, cb) {
				var vm = this;
				var len = data.length;
				if(!len) {
					cb && cb();
				} else {
					var file = data[len - 1];
					var formData = new FormData();
					formData.append('file', file);
					rehaAPI.upload(formData).then(function(res) {
						console.log(res)
						if(res.status == 200) {
							console.log(vm.upImgs[len - 1])
							vm.upImgs[len - 1].img = res.data;
							data.pop();
							vm.uploadImg(data, cb);
						}
					})
				}
			},
			viewDetial:function(type,code){
				var vm=this;
				if(type == 3) {
					top.layer.msg('客户端暂不支持查询健康指导记录');
				} else if(type==4){
					top.layer.msg('客户端暂不支持查询随访记录');
				}else{
					top.layer.open({
						type: 2,
						//				  offset: ['100px'], //右下角弹出
						area: ['80%', '600px'],
						shade: 0.5,
						title: '指导教育',
						fixed: true, //不固定
						maxmin: true,
						closeBtn: 1,
						shift: 5,
						shadeClose: false, //点击遮罩关闭层
						content: '../../article/html/article-info.html?articleId=' + code,
						end: function() { // 未点击确定按钮，点击关闭按钮  
							vm.getServiceItem();　　　　
						}
					});
					
				}
			},
			updateNoteAndImageRehabilitationOperate: function() {
				var vm = this;
				var _image = [];
				for(var i in vm.upImgs) {
					_image.push(vm.upImgs[i].img);
				}
				var params = {
					planDetailId: vm.planid,
					node: vm.service.node,
					image: JSON.stringify(_image)
				}
				var loadding = top.layer.load(0, {
					shade: false
				}); //0代表加载的风格，支持0-2
				rehaAPI.updateNoteAndImageRehabilitationOperate(params).then(function(res) {
					top.layer.close(loadding);
					console.log(res)
					if(res.status == 200) {
						vm.getServiceItem();
					}
				})
			},
			getContent: function(value,type) {
				var res=value;
				if(type==2 || type==9){
					res = "<img src='"+httpRequest.getImgUrl(res)+"'>";
				}else if(type==3){
					res = '【语音】';
				}else if(type==19){
					res = '【聊天记录】';
				}else if(type==12){
					res = '【视频】';
				}
				return res;
			}
		},
		filters: {
			formatDate: function(value, format) {
				if(!value) return;
				var fmt = format || "yyyy-MM-dd hh:mm";
				var date = new Date(value);
				if(/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
				}
				let o = {
					'M+': date.getMonth() + 1,
					'd+': date.getDate(),
					'h+': date.getHours(),
					'm+': date.getMinutes(),
					's+': date.getSeconds()
				};
				for(let k in o) {
					if(new RegExp(`(${k})`).test(fmt)) {
						let str = o[k] + '';
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
					}
				}
				return fmt;
			},
			getImgUrl: function(value) {
				var url = httpRequest.getImgUrl(value);
				return url;
			}
		}
	})
	$("body").on('click','img',function(){  
        var _this = $(this);//将当前的img元素作为_this传入函数  
        imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);  
    }); 
    function imgShow(outerdiv, innerdiv, bigimg, _this){  
        var src = _this.attr("src");//获取当前点击的pimg元素中的src属性  
        $(bigimg).attr("src", src);//设置#bigimg元素的src属性  
            /*获取当前点击图片的真实大小，并显示弹出层及大图*/  
        $("<img/>").attr("src", src).load(function(){  
            var windowW = $(window).width();//获取当前窗口宽度  
            var windowH = $(window).height();//获取当前窗口高度  
            var realWidth = this.width;//获取图片真实宽度  
            var realHeight = this.height;//获取图片真实高度  
            var imgWidth, imgHeight;  
            var scale = 0.8;//缩放尺寸，当图片真实宽度和高度大于窗口宽度和高度时进行缩放  
              
            if(realHeight>windowH*scale) {//判断图片高度  
                imgHeight = windowH*scale;//如大于窗口高度，图片高度进行缩放  
                imgWidth = imgHeight/realHeight*realWidth;//等比例缩放宽度  
                if(imgWidth>windowW*scale) {//如宽度扔大于窗口宽度  
                    imgWidth = windowW*scale;//再对宽度进行缩放  
                }  
            } else if(realWidth>windowW*scale) {//如图片高度合适，判断图片宽度  
                imgWidth = windowW*scale;//如大于窗口宽度，图片宽度进行缩放  
                            imgHeight = imgWidth/realWidth*realHeight;//等比例缩放高度  
            } else {//如果图片真实高度和宽度都符合要求，高宽不变  
                imgWidth = realWidth;  
                imgHeight = realHeight;  
            }  
                    $(bigimg).css("width",imgWidth);//以最终的宽度对图片缩放  
              
            var w = (windowW-imgWidth)/2;//计算图片与窗口左边距  
            var h = (windowH-imgHeight)/2;//计算图片与窗口上边距  
            $(innerdiv).css({"top":h, "left":w});//设置#innerdiv的top和left属性  
            $(outerdiv).fadeIn("fast");//淡入显示#outerdiv及.pimg  
        });  
        $(outerdiv).click(function(){//再次点击淡出消失弹出层  
            $(this).fadeOut("fast");  
        });  
    }
}();