
(function() {
	var request = getRequest(),
		patient = request.patiCode || '';
	var tabIndex = 0,
		dayIndex = 0;
	$('.tab-record span').click(function() {
		tabIndex = $(this).index();
		dataCanvas.labels = [];
		dataCanvas.datasets[0].data = [];
		dataCanvas.datasets[1].data = [];
		indexHover(this);
		dayIndex = 0;
		var ele = $('.day-record span').get(0)
		indexHover(ele);
		// 切换数据
		getBodyRecord(tabIndex, dayIndex);
	});
	$('.day-record span').click(function() {
		dayIndex = $(this).index();
		indexHover(this);
		// 切换数据
		getBodyRecord(tabIndex, dayIndex);
	});
	function indexHover(ele) {
		$(ele).siblings().removeClass('f-hover');
		$(ele).addClass('f-hover');
	}
	// 时间推算
	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";
	};
	// 获取最后一条数据
	var dataLast = {patient: patient};
	var lastXT = '',
		lastXY = '';
	bodyRecordApis.getBodyRecoredLast({data: dataLast}).then(function(res){
		if(res.status == 200){
        	lastXT = res.data.xt.recordDate;
        	lastXY = res.data.xy.recordDate;
        	if(lastXT || lastXY) {
        		// 初始化执行
				getBodyRecord(tabIndex, dayIndex);
        	} else {
        		showWarningMsg("没有可查阅的体征记录");
        	}
        	
    	}else{
        	showWarningMsg(res.msg)
    	}
	})
	// 绘制折线图初始定义
	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 : []
			}
		]
	};
	var defaults = {
		scaleStartValue : 0,
		scaleShowLabels : true,
    	datasetFill : false,         // 是否填充数据集 
    	animation : true,            // 是否执行动画  
    	animationSteps : 60,          // 动画的时间   
    	animationEasing : "easeOutQuart",    // 动画的特效   
    	onAnimationComplete : null    // 动画完成时的执行函数
	}
	var myLineChart = null;
//	new Chart(ctx).Line(dataCanvas, defaults);

	// 获取数据
	function getBodyRecord(tab, day) {
		$('.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),
			gi_type = (tab == 0 ? 1 : 0);// 字段不清楚暂时不处理
		$('.data-record').html(enddate.substr(0, 11) + '至' + startdate.substr(0,11));
		var recordData = {
			type: type,
			begin: enddate,
			end: startdate,
			gi_type: gi_type,
			patient: patient
		}
		// 获取体征记录
		bodyRecordApis.getBodyRecordData({data: recordData}).then(function(res){
			if(res.status == 200){
	        	var list = res.list,
	        		len = list.length;
	        	list.reverse();
	        	if(len < 1) {
	        		showWarningMsg("该居民无体征记录");return;
	        	}
	        	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;
	        			}
	        		} else {
	        			dataCanvas.datasets[1].data[i] = []
	        		}
	        	}
	        	// 绘制图
	        	if(myLineChart) {// 如果构造过，解析
	        		myLineChart.destroy();
	        	}
	        	myLineChart = new Chart(ctx).Line(dataCanvas, defaults);
	    	}else{
	        	showWarningMsg(res.msg)
	    	}
	    })
	}
})(jQuery)
