<!DOCTYPE html>
<html lang="en">
{{fscript

	"The expected arguments are: startDateInterval, endDateInterval, entity, dateAttribute, kind, days. startDate and endDate expressed since reference date"

	"Today's date minus 30 days"
	start := NSDate dateWithTimeIntervalSinceReferenceDate:(startDateInterval doubleValue).
	end := NSDate dateWithTimeIntervalSinceReferenceDate:(endDateInterval doubleValue).

	dates := MCReportDateEnumerator allDailyDatesBetween:start end:end.
	
	labels := {}.
	data := {}.
	lines1 := {}.
	lines2 := {}.
	
	dictionary := #{'labels' -> labels, 'data' -> data, 'lines1' -> lines1, 'lines2' -> lines2}.
	
	dates do:[:date |
		begin := date mcDateAsBeginningOfDay.
		endDay := date mcDateAsEndOfDay.
		
		locum_str := dateAttribute ++ ' >= $start and ' ++ dateAttribute ++ ' <= $end'.

	 	c := objectContext resultCountForEntityNamed:entity qualifierLocum:locum_str bindings:#{'start' -> begin, 'end' -> endDay}.
		
		labels add:(date mcDayOfMonth).
		data add:c.
		lines1 add:c.
		lines2 add:(date mcLongDate).
		
		].

	globals setDictionary:(dictionary JSONRepresentation).

}}
<head>
	<meta charset="utf-8">
	<link rel="stylesheet" href="{{framework}}/default.css" type="text/css" charset="utf-8">
	<link rel="stylesheet" href="{{framework}}/screen.css" type="text/css" charset="utf-8" media="screen">
	<link rel="stylesheet" href="{{framework}}/print.css" type="text/css" charset="utf-8" media="print">
	<script src="{{framework}}/raphael.js"></script>
	<script src="{{framework}}/n.linechart.js"></script>
	<script type="text/javascript">

		window.onload = function () {
			var opts = {
				data: {{dictionary}},
				width: 700,
				height: 300,
				show_area: true,
				y_labels_count: 10,
				x_labels_step: true,
				mouse_coords: 'rect',
				colors: {
					master: '#0066ff'
				}
			};
									
			var chart1 = Raphael('linechart1', 700, 300);
		
			chart1.lineChart(opts);

		};
	</script>
</head>
<body>
	<h1>{{localize ' %@ created in the last %@ days', kind, days}}</h1>
	<div id="linechart1"></div>
	
	<div id="stats">
		<p>{{localize 'Total:'}} {{fscript
			objectContext resultCountForEntityNamed:entity.
		}}</p>
		<p>{{localize 'First one created on:'}} {{fscript
			date := objectContext minDateForEntityNamed:entity dateAttributeNamed:dateAttribute.
			date mcLongDate.
		}}</p>
		<p>{{localize 'Last one created on:'}} {{fscript
			date := objectContext maxDateForEntityNamed:entity dateAttributeNamed:dateAttribute.
			date mcLongDate.
		}}</p>
	</div>

	<div id="reportfooter">
	{{localize 'Generated by %@ on %@.', objectContext.user.contact.cachedName, date}}
	</div>

</body>
</html>