
(function() {
	var request = getRequest(),
		patient = request.patiCode || '';
	var tabIndex = 0,
		dayIndex = 0,
		statusIndex = 1;
	// 绘制折线图初始定义
	var ctx = document.getElementById('canvas').getContext("2d");
	var dataCanvas = {
		labels : [],
		datasets : [
				{
					fillColor : "rgba(220,220,220,0.5)",
					strokeColor : "rgba(220,220,220,1)",
					pointColor : "rgba(220,220,220,1)",
					pointStrokeColor : "#fff",
					data : []
				},
				{
					fillColor : "rgba(151,187,205,0.5)",
					strokeColor : "rgba(151,187,205,1)",
					pointColor : "rgba(151,187,205,1)",
					pointStrokeColor : "rgba(151,187,205,1)",
					data : []
				}
			]
		},
		defaults = {
			scaleStartValue : 0,
			scaleShowLabels : true,
	    	datasetFill : false,         // 是否填充数据集 
	    	animation : true,            // 是否执行动画  
	    	animationSteps : 60,          // 动画的时间   
	    	animationEasing : "easeOutQuart",    // 动画的特效   
	    	onAnimationComplete : null    // 动画完成时的执行函数
		};
	var myLineChart = null;
//	new Chart(ctx).Line(dataCanvas, defaults);

	$('.tab-record span').click(function() {
		tabIndex = $(this).index();
		// 初始化数据
		if(tabIndex == 1) {
			statusIndex = 0;
			$('.status-record').hide();
		} else {
			var ele = $('.status-record span').get(0);
			indexHover(ele);
			statusIndex = 1;
			$('.status-record').show();
		}
		indexHover(this);
		dayIndex = 0;
		var ele = $('.day-record span').get(0);
		indexHover(ele);
		canvasDataDefault();
	});
	$('.day-record span').click(function() {
		dayIndex = $(this).index();
		if(tabIndex == 0) {
			var ele = $('.status-record span').get(0);
			indexHover(ele);
			statusIndex = 1;
		}
		indexHover(this);
		canvasDataDefault();
		
	});
	$('.status-record span').click(function() {
		statusIndex = $(this).index() + 1;
		indexHover(this);
		canvasDataDefault();

	});
	// 点击指示特效
	function indexHover(ele) {
		$(ele).siblings().removeClass('f-hover');
		$(ele).addClass('f-hover');
	};
	// 绘图数据格式化
	function canvasDataDefault() {
		dataCanvas.labels = [];
		dataCanvas.datasets[0].data = [];
		dataCanvas.datasets[1].data = [];
		// 切换数据
		getBodyRecord(tabIndex, dayIndex, statusIndex);
	}
	// 时间推算
	function getDateFromCurrentDate(fromDate, dayInterval) {
		var curDate = new Date(Date.parse(fromDate.replace(/-/g, "/")));
		curDate.setDate(curDate.getDate() + dayInterval);
		var year = curDate.getFullYear();
		var month = (curDate.getMonth() + 1) < 10 ? "0" + (curDate.getMonth() + 1) : (curDate.getMonth() + 1);
		var day = curDate.getDate() < 10 ? "0" + curDate.getDate() : curDate.getDate();
		return year + "-" + month + "-" + day + " 00:00:00";
	};
	// 血糖标准
	function getNormalLevel(index) {
		return [
			[],
			[6.1, 3.9],
			[7.8, 4.4],
			[6.1, 3.9],
			[7.8, 4.4],
			[6.1, 3.9],
			[7.8, 4.4],
			[6.1, 3.9]
		][index];
	};
	// 数据统计
	function totalStauts(zc, pg, pd) {
		$('.is-ok').html("正常：" + zc + "次");
	    $('.is-height').html("偏高：" + pg + "次");
    	$('.is-low').html("偏低：" + pd + "次");
	}
	// 获取最后一条数据
	var dataLast = {patient: patient};
	var lastXT = '',
		lastXY = '';
	bodyRecordApis.getBodyRecoredLast({data: dataLast}).then(function(res){
		if(res.status == 200){
			if(!res.data.xt || !res.data.xy) {
				showWarningMsg("该居民无体征记录");
				return;
			}
        	lastXT = res.data.xt.recordDate;
        	lastXY = res.data.xy.recordDate;
        	// 初始化执行
			getBodyRecord(tabIndex, dayIndex, statusIndex);
    	}else{
        	showWarningMsg(res.msg)
    	}
	})
	// 获取数据
	function getBodyRecord(tab, day, statu) {
		if(!lastXT || !lastXY) {
			showWarningMsg("该居民无体征记录");
			return;
		};
		var xtMax = getNormalLevel(statusIndex)[0],
			xtMin = getNormalLevel(statusIndex)[1],
			xyMax = [139, 89],
			xymin = [90, 60],
			toNormal = 0,
			toHigh = 0,
			toLow = 0;
		totalStauts(toNormal, toHigh, toLow);
		$('.danwei').html(tab == 0 ? '单位(mmol/L)' : '单位(mmHg)');
		var	dayNum = 0;
		switch(day) {
			case 0:
				dayNum = 7;break;
			case 1:
				dayNum = 30;break;
			case 2:
				dayNum = 90;break;
			case 3:
				dayNum = 365 / 2;break;
		}
		var type = (tab == 0 ? 1 : 2),
			startdate = (tab == 0 ? lastXT : lastXY),
			enddate = getDateFromCurrentDate(startdate, -dayNum);
		$('.data-record').html('<' + enddate.substr(0, 10) + '至' + startdate.substr(0,10) + '>');
		var recordData = {
			type: type,
			begin: enddate,
			end: startdate,
			gi_type: statu,
			patient: patient
		};
		// 获取体征记录
		bodyRecordApis.getBodyRecordData({data: recordData}).then(function(res){
			if(myLineChart) {// 如果构造过，解析
        		myLineChart.destroy();
        	}
			if(res.status == 200){
	        	var list = res.list,
	        		len = 0;
	        	if(list) {
	        		len = list.length;
	        	} else {
	        		showWarningMsg("当前状态下无居民体征记录");return;
	        	};
	        	list.reverse();
	        	for(var i = 0; i < len; i++) {
	        		if(len == 1) {
	        			dataCanvas.labels = [enddate.substr(5, 5), list[i].date.substr(5, 5)];
	        			dataCanvas.datasets[0].data = [null, list[i].value1];
	        		} else {
	        			dataCanvas.labels[i] = list[i].date.substr(5, 5);
	        			dataCanvas.datasets[0].data[i] = list[i].value1;
	        		}
	        		if(tabIndex == 1) {
	        			if(len == 1) {
	        				dataCanvas.datasets[1].data = [null, list[i].value2];
	        			} else {
	        				dataCanvas.datasets[1].data[i] = list[i].value2;
	        			}
	        			// 血压判断
	        			if(list[i].value1 > xyMax[0] || list[i].value2 > xyMax[1]) {
	        				++toHigh;
	        			} else if(list[i].value1 < xyMin[0] || list[i].value2 < xyMin[1]) {
	        				++toLow;
	        			} else {
	        				++toNormal;
	        			}
	        		} else {
	        			// 血糖判断
	        			if(list[i].value1 > xtMax) {
	        				++toHigh;
	        			} else if(list[i].value1 < xtMin) {
	        				++toLow;
	        			} else {
	        				++toNormal;
	        			};
	        		}
	        	}
	        	totalStauts(toNormal, toHigh, toLow);
	        	// 绘制图
	        	myLineChart = new Chart(ctx).Line(dataCanvas, defaults);
	    	}else{
	        	showWarningMsg(res.msg)
	    	}
	    })
	}
})(jQuery)
