<?php 
	if(isset($_GET['view']) AND $_GET['view'] != ''){
		$pageTitle 	= 'Сделка: ' . get_deal_num($_GET['view']);
	} elseif(isset($_GET['edit']) AND $_GET['edit'] != ''){
		$pageTitle 	= 'Редактирование сделки: ' . get_deal_num($_GET['edit']);
	} elseif(isset($_GET['new']) AND isset($_GET['new'])){
		$pageTitle 	= "Добавление сделки";
	} else {
		$pageTitle 	= 'Сделки и предложения';
	}
	
	if(!isset($_GET['view']) AND !isset($_GET['edit'])){$filters = '1';}
	
	require_once(ROOT . 'templates/header.php'); 
	
	require_once(ROOT . 'modules/deals/list-deals.php'); 
	
	if(isset($Type) AND $Type == 'deal-a'){
		$DealType = 'Активная сделка';
	}elseif(isset($Type) AND $Type == 'deal-c'){
		$DealType = 'Завершенная сделка';
	}else{
		$DealType = 'Коммерческое предложение';
	}
	if(isset($_POST["selectClient"])){$chartClient=$_POST["selectClient"];}else{$chartClient = '';}
	if(isset($_POST["inputPriceS"])){$chartPriceS=$_POST["inputPriceS"];}else{$chartPriceS = '';}
	if(isset($_POST["inputPriceE"])){$chartPriceE=$_POST["inputPriceE"];}else{$chartPriceE = '';}
	
	require_once(ROOT . 'templates/main-menu.php');
?>
<div class="container<?php global $designView; echo $designView; ?>">
	<div class="row">
		<?php 
		if(isset($_GET['view']) and $_GET['view'] != ''){ 
			require_once(ROOT . 'modules/deals/page-deals-view.php');
		} elseif(isset($_GET['edit']) and $_GET['edit'] != '' or isset($_GET['new'])) {
			if($GroupCheck == 'admin' or $GroupCheck == 'manager'){
				require_once(ROOT . 'modules/deals/page-deals-edit.php');
			} else { echo '<meta http-equiv="refresh" content="0;/'.$folder.'/deals">';}
		} else {
		?>
		<div class="col-lg-3 col-lg-push-9 col-md-4 col-md-push-8 col-sm-12">
			<?php require_once(ROOT . 'templates/side-block.php'); ?>
		</div>
		<div class="col-lg-9 col-lg-pull-3 col-md-8 col-md-pull-4 col-sm-12">
			<?php if( !isset( $_POST['search'] ) ) { ?>
			<h1>
				<span class="hidden-sm hidden-xs">Сделки за </span>
				<?php echo $currentYear;?> год
				<span class="list-num"><i class="fa fa-fw fa-angle-double-down" aria-hidden="true"></i> 
				<?php
				if (isset($_POST['DealFilter']) OR isset($_POST['search'])){
					echo $countDeal;
				} else {
					echo floatval(multi_count_deal('deal-a', $chartClient, $sel_year, $chartPriceS, $chartPriceE) + multi_count_deal('deal-c', $chartClient, $sel_year, $chartPriceS, $chartPriceE));
					
				}
				?>
				</span>
			</h1>
			<div class="chart-container" style="position: relative; height:40vh; width:100%;margin-bottom:30px;">
				<canvas id="myChart"></canvas>
			</div>
			<script>
			var ctx = document.getElementById("myChart").getContext('2d');
			var myChart = new Chart(ctx, {
				type: 'line',
				data: {
					labels: ["Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"],
					datasets: [{
						label: 'Расходы',
						data: [
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '01');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '02');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '03');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '04');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '05');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '06');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '07');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '08');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '09');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '10');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '11');?>,
							-<?php echo count_deals_price_summ('','costs', $chartClient, $sel_year, '12');?>
						],
						backgroundColor: [ 'rgba(219, 90, 89, 0.25)' ],
						borderColor: 'rgba(219, 90, 89, 1.0)',
						borderWidth: 2,
						tension: 0,
						pointStyle: 'circle',
						pointRadius: 3,
						pointBackgroundColor: 'rgba(219, 90, 89, 1.0)'
					},{
						label: 'Прибыль',
						data: [
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '01');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '02');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '03');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '04');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '05');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '06');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '07');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '08');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '09');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '10');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '11');?>,
							<?php echo count_deals_profit_summ('',$chartClient, $sel_year, '12');?>,
						],
						backgroundColor: [ 'rgba(122,178,98, 0.25)' ],
						borderColor: 'rgba(122,178,98, 1.0)',
						borderWidth: 2,
						tension: 0,
						pointStyle: 'circle',
						pointRadius: 3,
						pointBackgroundColor: 'rgba(122,178,98, 1.0)'
					},{
						label: 'Доходы',
						data: [
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '01');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '02');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '03');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '04');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '05');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '06');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '07');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '08');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '09');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '10');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '11');?>,
							<?php echo count_deals_price_summ('','price', $chartClient, $sel_year, '12');?>
						],
						backgroundColor: [ 'rgba(64, 73, 91, 0.0)' ],
						borderColor: 'rgba(64, 73, 91, 1.0)',
						borderWidth: 2,
						tension: 0,
						pointStyle: 'circle',
						pointRadius: 3,
						pointBackgroundColor: 'rgba(64, 73, 91, 1.0)'
					},{
						label: 'Долги',
						data: [
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '01');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '02');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '03');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '04');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '05');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '06');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '07');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '08');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '09');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '10');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '11');?>,
							-<?php echo count_deals_debt_summ('',$chartClient, $sel_year, '12');?>
						],
						backgroundColor: [ 'rgba(89, 149, 219, 0.25)' ],
						borderColor: 'rgba(89, 149, 219, 1.0)',
						borderWidth: 2,
						tension: 0,
						pointStyle: 'circle',
						pointRadius: 3,
						pointBackgroundColor: 'rgba(89, 149, 219, 1.0)'
					}],
				},
				options: {
					scales: {
						yAxes: [{
							ticks: {
								beginAtZero:true
							}
						}]
					},
					responsive: true,
					maintainAspectRatio: false,
					legend: {
						position: 'bottom'
					}
				}
			});
			</script>
			<?php }?>
			<?php if( isset( $_POST["checkYear"] ) ) {?>
				<h1>Все сделки<span class="list-num"><i class="fa fa-fw fa-angle-double-down" aria-hidden="true"></i> <?php echo $countDeal; ?></span></h1>
			<?php } else { ?>
				<h1>Активные сделки<span class="list-num"><i class="fa fa-fw fa-angle-double-down" aria-hidden="true"></i> <?php echo $countDeal; ?></span></h1>
			<?php } /*echo $_POST["inputPriceS"].$_POST["inputPriceE"];*/?>
			<?php require_once(ROOT . 'templates/search-form.php'); ?>
			
			<div class="table-responsive">
				<table id="all-deals" class="table table-hover all-deals deals-inwork">
					<thead> 
						<tr> 
							<th width="6%"></th> 
							<th>Наименование</th>
							<th width="15%" style="text-align:right!important;">Сумма</th>
							<th width="6%" title="Рентабельность"><i class="fa fa-fw fa-line-chart" aria-hidden="true"></i></th>
							<th width="15%" data-sorter="shortDate" data-date-format="yyyymmdd">Заключение</th> 
							<th width="15%" data-sorter="shortDate" data-date-format="yyyymmdd">Завершение</th>
							<th width="6%"></th>
							<th width="6%"></th>
						</tr> 
					</thead> 
					<tbody>
						<?php require_once(ROOT . 'modules/deals/list-deals-m.php'); ?>
					</tbody>
				</table>
			</div>
			<?php require_once(ROOT . 'templates/table-pager.php'); ?>
			
			<div class="content-separator"></div>
			
			<h1>Предложения за <?php echo $currentYear;?> год<span class="list-num"><i class="fa fa-fw fa-angle-double-down" aria-hidden="true"></i><?php echo $countOffer; ?></span></h1>
			<div class="table-responsive">
				<table id="all-offers-sm" class="table table-hover all-deals deals-inwork">
					<thead> 
						<tr> 
							<th width="6%"></th> 
							<th>Наименование</th>
							<th width="15%" style="text-align:right!important;">Сумма</th>
							<th width="15%">Добавление</th> 
							<th width="6%"></th>
							<th width="6%"></th>
						</tr> 
					</thead> 
					<tbody>
						<?php require_once(ROOT . 'modules/deals/list-offers-m.php'); ?>
					</tbody>
				</table>
			</div>
			<?php require_once(ROOT . 'templates/table-pager2.php'); ?>
		</div>
		<?php } ?>
	</div>
</div>
<?
	require_once(ROOT . 'templates/modal-wind.php');
	require_once(ROOT . 'templates/footer.php');
?>
	</body>
</html>