<?php 
$clients_count_return = clients_requests_count('return');
$clients_count_once = clients_requests_count('once');
$clients_count_nodeal = clients_requests_count('nodeal');
?>

<h3 class="text-center" style="margin:30px 0px 30px;">График изменения количества клиентов</h3>

<div class="chart-container" style="position: relative; height:40vh; width:100%; margin-bottom:50px;">
	<canvas id="ClientsCount10age"></canvas>
</div>
<script>
var ctxclientscount10age = document.getElementById("ClientsCount10age").getContext('2d');
var ClientsCount10age = new Chart(ctxclientscount10age, {
	type: 'line',
	data: {
		labels: ["<?php echo date("Y",strtotime("- 4 years")); ?>", "<?php echo date("Y",strtotime("- 3 years")); ?>", "<?php echo date("Y",strtotime("- 2 years")); ?>", "<?php echo date("Y",strtotime("- 1 years")); ?>", "<?php echo date("Y"); ?>"],
		datasets: [{
			label: 'Юр. лица',
			stack: 'Stack 0',
			data: [<?php echo clients_year_sum('UL'); ?> ],
			backgroundColor: [ 'rgba(219, 90, 89, 0.23)' ],
			borderColor: 'rgba(219, 90, 89, 1.0)',
			borderWidth: 2,
			tension: 0,
			pointStyle: 'circle',
			pointRadius: 3,
			pointBackgroundColor: 'rgba(219, 90, 89, 1.0)'
		},{
			label: 'Физ. лица',
			stack: 'Stack 1',
			data: [<?php echo clients_year_sum('FL'); ?> ],
			backgroundColor: [ 'rgba(89,149,219, 0.23)' ],
			borderColor: [ 'rgba(89,149,219, 1.0)' ],
			borderWidth: 2,
			tension: 0,
			pointStyle: 'circle',
			pointRadius: 3,
			pointBackgroundColor: 'rgba(89,149,219, 1.0)'
		},{
			label: 'Общее кол-во',
			stack: 'Stack 3',
			data: [<?php echo clients_year_sum('all'); ?> ],
			backgroundColor: [ 'rgba(212,219,89, 0.0)' ],
			borderColor: 'rgba(212,219,89, 1.0)',
			borderWidth: 2,
			tension: 0,
			pointStyle: 'circle',
			pointRadius: 3,
			pointBackgroundColor: 'rgba(212,219,89, 1.0)'
		}]
	},
	options: {
		scales: {
			yAxes: [{
				ticks: {
					beginAtZero:true
				}
			}]
		},
		responsive: true,
		maintainAspectRatio: false,
		legend: {
			position: 'bottom'
		}
	}
});
</script>

<div class="row">
	<div class="col-lg-4">
		<div class="panel panel-default">
			<div class="panel-body">
				<div class="row">
					<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
						<div class="row">
							<div class="col-lg-12"><b>Типы клиентов</b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Общее количество:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $countClientTypeUl + $countClientTypeFl; ?></b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Юридическое лицо:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $countClientTypeUl; ?></b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Физическое лицо:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $countClientTypeFl; ?></b></div>
						</div>
					</div>
					<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
						<div class="chart-container" style="position: relative; height:10vh; width:100%;">
							<canvas id="ChartClientsType"></canvas>
						</div>
						<script>
							var ctxclientstype = document.getElementById("ChartClientsType").getContext('2d');
							var ChartClientsType = new Chart(ctxclientstype, {
								type: 'doughnut',
								data: {
									datasets: [{
										data: [
											"<?php echo $countClientTypeUl; ?>",
											"<?php echo $countClientTypeFl; ?>",
										],
										backgroundColor:[
														"rgba(219, 90, 89, 0.25)",
														"rgba(89,149,219, 0.25)",
														],
										borderColor:	[
														"rgba(219, 90, 89, 1.0)",
														"rgba(89,149,219, 1.0)",
														],
										borderWidth: 2
									}],
									labels: [
										"Юр.лицо",
										"Физ.лицо"
									]
								},
								options: {
									responsive: true,
									maintainAspectRatio: false,
									legend: {
										display: false
									},
									title: {
										display: false
									},
									animation: {
										animateScale: true,
										animateRotate: true
									}
								}
							});
						</script>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="col-lg-4">
		<div class="panel panel-default">
			<div class="panel-body">
				<div class="row">
					<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
						<div class="row">
							<div class="col-lg-12"><b>Важность клиентов</b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Высокая важность:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $countClientPriorityHigh; ?></b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Средняя важность:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $countClientPriorityMedium; ?></b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Низкая важность:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $countClientPriorityLow; ?></b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Важность отсутствует:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $countClientPriorityNone; ?></b></div>
						</div>
					</div>
					<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
						<div class="chart-container" style="position: relative; height:10vh; width:100%;">
							<canvas id="ChartClientsPriority"></canvas>
						</div>
						<script>
						var ctxclientspriority = document.getElementById("ChartClientsPriority").getContext('2d');
						var ClientsPriority = new Chart(ctxclientspriority, {
							type: 'doughnut',
							data: {
								datasets: [{
									label: 'Количество',
									data: [ 
										"<?php echo $countClientPriorityHigh; ?>", 
										"<?php echo $countClientPriorityMedium; ?>", 
										"<?php echo $countClientPriorityLow; ?>", 
										"<?php echo $countClientPriorityNone; ?>" 
										],
									backgroundColor:[
													"rgba(219, 90, 89, 0.25)",
													"rgba(255,191,0, 0.25)",
													"rgba(122,178,98, 0.25)",
													"rgba(212,219,89, 0.25)",
													],
									borderColor: 	[
												"rgba(219, 90, 89, 1.0)",
												"rgba(255,191,0, 1.0)",
												"rgba(122,178,98, 1.0)",
												"rgba(212,219,89, 1.0)",
												],
									borderWidth: 2
													
								}],
								labels: ["Высокая", "Средняя", "Низкая", "Отсутствует"]
							},
							options: {
								responsive: true,
								maintainAspectRatio: false,
								legend: {
									display: false
								},
								title: {
									display: false
								},
								animation: {
									animateScale: true,
									animateRotate: true
								}
							}
						});
						</script>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="col-lg-4">
		<div class="panel panel-default">
			<div class="panel-body">
				<div class="row">
					<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
						<div class="row">
							<div class="col-lg-12"><b>Количество сделок</b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Две сделки и более:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $clients_count_return; ?></b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">Одна сделка:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $clients_count_once; ?></b></div>
						</div>
						<hr/>
						<div class="row">
							<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">нет сделок:</div>
							<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" style="text-align:right;"><b><?php echo $clients_count_nodeal; ?></b></div>
						</div>
					</div>
					<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
						<div class="chart-container" style="position: relative; height:10vh; width:100%;">
							<canvas id="ChartClientsReturn"></canvas>
						</div>
						<script>
						var ctxclientsreturn = document.getElementById("ChartClientsReturn").getContext('2d');
						var ClientsReturn = new Chart(ctxclientsreturn, {
							type: 'doughnut',
							data: {
								datasets: [{
									label: 'Количество',
									data: [ 
										"<?php echo $clients_count_return; ?>", 
										"<?php echo $clients_count_once; ?>", 
										"<?php echo $clients_count_nodeal; ?>", 
										],
									backgroundColor:[
													"rgba(122,178,98, 0.25)",
													"rgba(255,191,0, 0.25)",
													"rgba(212,219,89, 0.25)",
													],
									borderColor: 	[
												"rgba(122,178,98, 1.0)",
												"rgba(255,191,0, 1.0)",
												"rgba(212,219,89, 1.0)",
												],
									borderWidth: 2
													
								}],
								labels: ["Две и более", "Одна сделка", "Нет сделок"]
							},
							options: {
								responsive: true,
								maintainAspectRatio: false,
								legend: {
									display: false
								},
								title: {
									display: false
								},
								animation: {
									animateScale: true,
									animateRotate: true
								}
							}
						});
						</script>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<h3 class="text-center" style="margin:30px 0px 30px;">Источники обращения клиентов</h3>

<div class="row">
	<div class="col-lg-6 col-sm-6">
		<div class="table-responsive">
			<?php
			$doc = new DOMDocument;
			$doc->load(ROOT . '/crmdata/database/clients.xml');
			$xpathc = new DOMXPath($doc);
			$clients = $xpathc->query('/clients/client');
			$sourses_arr = array();
			$i = 0;
			foreach ($clients as $client) {
				$SourceName = $client->getElementsByTagName("clientfined")->item(0)->nodeValue;
				$countSource = count($xmlclient->xpath('/clients/client[clientfined="'.$SourceName.'"]'));
				$sourses_arr[$SourceName] =  $countSource;
				$i++;
			}
			$sources = $sourses_arr;
			?>
			<table id="all-subjects" class="table table-hover all-subjects">
				<thead> 
					<tr> 
						<th>Источник</th> 
						<th width="10%" style="text-align:center!important;">Количество</th> 
					</tr> 
				</thead> 
				<tbody>
					<?php
					foreach ($sources as $key => $value){
					if($key == ''){$source = 'Источник не указан';} else {$source = $key;}
					echo '
						<tr>
							<td>'.$source.'</td>
							<td style="text-align:center!important;">'.$value.'</td>
						</tr>';
					}
					?>
				</tbody>
			</table>
		</div>
	</div>
	<div class="col-lg-6 col-sm-6">
		<div class="chart-container" style="position: relative; height:30vh; width:100%; margin-bottom:20px;">
			<canvas id="ChartClientsSource"></canvas>
		</div>
		<script>
			var ctxclientssource = document.getElementById("ChartClientsSource").getContext('2d');
			var ChartClientsSource = new Chart(ctxclientssource, {
				type: 'horizontalBar',
				data: {
					datasets: [{
						data: [<?php foreach ($sources as $key => $value) {echo '"' . $value . '", ';} ?>],
						backgroundColor:[<?php foreach ($sources as $key => $value) {echo '"' . hexToRgb(get_color2($key).'40', true) . '", ';} ?>],
						borderColor: 	[<?php foreach ($sources as $key => $value) {echo '"' . hexToRgb(get_color2($key).'ff', true) . '", ';} ?>],
						borderWidth: 2
					}],
					labels: [<?php foreach ($sources as $key => $value) {echo '"' . $key . '", ';} ?>]
				},
				options: {
					responsive: true,
					maintainAspectRatio: false,
					legend: {
						display: false
					},
					title: {
						display: false
					},
					animation: {
						animateScale: true,
						animateRotate: true
					}
				}
			});
		</script>
	</div>
</div>