<h3 class="text-center" style="margin:30px 0px 30px;">Количество сделок и предложений</h3>

<div class="row">
	<div class="col-lg-8 col-sm-6">
		<div class="chart-container" style="position: relative; height:40vh; width:100%; margin-bottom:20px;">
			<canvas id="DealsCount10age"></canvas>
		</div>
		<script>
		var ctxdealscount10age = document.getElementById("DealsCount10age").getContext('2d');
		var DealsCount10age = new Chart(ctxdealscount10age, {
			type: 'bar',
			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: 'Предложения',
					data: [ 
						<?php echo deals_year_count('o',  '4'); ?>,
						<?php echo deals_year_count('o',  '3'); ?>,
						<?php echo deals_year_count('o',  '2'); ?>,
						<?php echo deals_year_count('o',  '1'); ?>,
						<?php echo deals_year_count('o',  '0'); ?>
						],
					backgroundColor: "rgba(212,219,89, 0.25)",
					borderColor: "rgba(212,219,89, 1.0)",
					borderWidth: 2
				},{
					label: 'Завершенные сделки',
					data: [ 
						<?php echo deals_year_count('c',  '4'); ?>,
						<?php echo deals_year_count('c',  '3'); ?>,
						<?php echo deals_year_count('c',  '2'); ?>,
						<?php echo deals_year_count('c',  '1'); ?>,
						<?php echo deals_year_count('c',  '0'); ?>
						],
					backgroundColor: "rgba(122,178,98, 0.25)",
					borderColor: "rgba(122,178,98, 1.0)",
					borderWidth: 2
				},{
					label: 'Активные сделки',
					data: [ 
						<?php echo deals_year_count('a',  '4'); ?>,
						<?php echo deals_year_count('a',  '3'); ?>,
						<?php echo deals_year_count('a',  '2'); ?>,
						<?php echo deals_year_count('a',  '1'); ?>,
						<?php echo deals_year_count('a',  '0'); ?>
						],
					backgroundColor: "rgba(255,191,0, 0.25)",
					borderColor: "rgba(255,191,0, 1.0)",
					borderWidth: 2
				}],
			},
			options: {
				scales: {
					yAxes: [{
						ticks: {
							beginAtZero:true
						}
					}],
					y: {
					stacked: true
				  }
				},
				responsive: true,
				maintainAspectRatio: false,
				legend: {
					position: 'bottom'
				}
			}
		});
		</script>
	</div>
	<div class="col-lg-4 col-sm-6">
		<div class="chart-container" style="position: relative; height:37vh; width:100%; margin-bottom:20px;">
			<canvas id="ChartDealsPrice"></canvas>
		</div>
		<script>
		var ctx = document.getElementById("ChartDealsPrice").getContext('2d');
		var ChartDealsPrice = new Chart(ctx, {
			type: 'horizontalBar',
			data: {
				labels: [
							"> 500 тыс.",
							"от 250 до 500 тыс.",
							"от 100 до 250 тыс.",
							"от 25 до 100 тыс.",
							"от 5 до 25 тыс.",
							"< 5 тыс."
						],
				datasets: [{
					label: 'Кол-во сделок',
					data: [ 
						"<?php echo $statAllDealPrice500001; ?>",
						"<?php echo $statAllDealPrice500000; ?>",
						"<?php echo $statAllDealPrice250000; ?>",
						"<?php echo $statAllDealPrice100000; ?>",
						"<?php echo $statAllDealPrice25000; ?>",
						"<?php echo $statAllDealPrice5000; ?>",
						],
					backgroundColor:[
									"#d861ca",
									"rgba(219, 90, 89, 0.25)",
									"rgba(255,191,0, 0.25)",
									"rgba(122,178,98, 0.25)",
									"rgba(89,149,219, 0.25)",
									"rgba(212,219,89, 0.25)",
									],
					borderColor: 	[
									"#d861ca",
									"rgba(219, 90, 89, 1.0)",
									"rgba(255,191,0, 1.0)",
									"rgba(122,178,98, 1.0)",
									"rgba(89,149,219, 1.0)",
									"rgba(212,219,89, 1.0)",
									],
					borderWidth: 2
				}],
			},
			options: {
				scales: {
					yAxes: [{
						ticks: {
							beginAtZero:true
						}
					}]
				},
				responsive: true,
				maintainAspectRatio: false,
				legend: {
					display: false
				}
			}
		});
		</script>
	</div>
</div>
<h3 class="text-center" style="margin:30px 0px 30px;">Средняя величина сделки</h3>
<div class="chart-container" style="position: relative; height:40vh; width:100%; margin-top:50px;">
	<canvas id="Deals10ageAv2"></canvas>
</div>
<script>
var ctxdeals10ageav2 = document.getElementById("Deals10ageAv2").getContext('2d');
var Deals10ageAv2 = new Chart(ctxdeals10ageav2, {
	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 deals_year_average2('deal', '4'); ?>, <?php echo deals_year_average2('deal', '3'); ?>, <?php echo deals_year_average2('deal', '2'); ?>, <?php echo deals_year_average2('deal', '1'); ?>, <?php echo deals_year_average2('deal', '0'); ?>],
			backgroundColor: [ 'rgba(212,219,89, 0.25)' ],
			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>

<h3 class="text-center" style="margin:50px 0px 30px;">Статистика сделок, имеющих метки</h3>

<div class="table-responsive">
	<table id="all-subjects" class="table table-hover all-subjects">
		<thead> 
			<tr> 
				<th>Метка сделки</th> 
				<th width="10%" style="text-align:center!important;">Количество</th> 
				<th width="15%">Общая прибыль</th>
				<th width="15%">Ср. прибыль</th>
				<th width="15%">Общий расход</th>
				<th width="15%">Ср. расход</th>
			</tr> 
		</thead> 
		<tbody>
			<?php
			$dox = new DOMDocument;
			$dox->load($baseD);
			$xpathx = new DOMXPath($dox);
			$alltagsdeal = $xpathx->query('/deals/deal');
			$sourses_arr = array();
			$i = 0;
			foreach ($alltagsdeal as $deal) {
				if(is_object($deal->getElementsByTagName("subject")->item(0))){
					$listSourceName = $deal->getElementsByTagName("subject")->item(0)->nodeValue;
					$countSource = count($xmldeal->xpath('/deals/deal[subject="'.$listSourceName.'"]'));
					$sourses_arr[$listSourceName] =  $countSource;
					$i++;
				}
			}
			$sources = $sourses_arr;
			foreach ($sources as $key => $value){
				if(count_deals_paid($key) > 0){
					echo '
						<tr>
							<td>'.$key.'</td>
							<td style="text-align:center!important;">'.$value.'</td>
							<td>'.conv_price( count_deals_paid($key) , 'long' , '<i class="fa fa-sm fa-rub"></i>').'</td>
							<td class="clr_low"><b>'.conv_price( count_deals_paid($key) / $value , 'long' , '<i class="fa fa-sm fa-rub"></i>').'</b></td>
							<td>'.conv_price( count_deals_cost($key) , 'long' , '<i class="fa fa-sm fa-rub"></i>').'</td>
							<td class="clr_high"><b>'; if(count_deals_cost($key) > 0){echo conv_price( count_deals_cost($key) / $value , 'long' , '<i class="fa fa-sm fa-rub"></i>');} else { echo '0 <i class="fa fa-sm fa-rub"></i>';} echo '</b></td>
						</tr>';
				}
			}
			?>
		
		

		</tbody>
	</table>
</div>
<?php require_once(ROOT . 'templates/table-pager.php'); ?>