<?php
require_once('global.php');
require_once(ABSOLUTE_PATH.'/class/ChampionHelper.php');
require_once(ABSOLUTE_PATH.'/class/Paginator.php');
require_once(ABSOLUTE_PATH.'/class/SalesManager.php');

$template_body_class.= 'background-global';

$db = db::getInstance();
$ch = ChampionHelper::getInstance();
$sam = SalesManager::getInstance();

$meta_title = 'Sales &ndash; LoLSkill';
$meta_description = 'Check when your favorite champions and skins were on sale for the last time, so you can estimate when their next sale will take place.';
$template_og['url'] = DYNAMIC_PATH.'/sales';
$template_og['title'] = 'Sales &ndash; LoLSkill';
$template_og['description'] = 'Check when your favorite champions and skins were on sale for the last time, so you can estimate when their next sale will take place.';

// Content Selection
$contentMap = [
	'skins' => 'Skin Sales',
	'champions' => 'Champion Sales'
];
$currentContent = ((isset($_GET['content']))&&(isset($contentMap[$_GET['content']]))) ? $_GET['content'] : 'skins';
$contentSelect = '			<nav class="nav nav-content flex-wrap align-items-stretch">';
foreach($contentMap as $key => $value){
	$active = ($currentContent==$key) ? ' active' : '';
	$contentSelect.= '				<a class="nav-link'.$active.'" href="sales/'.$key.'">'.$value.'</a>';
}
$contentSelect.= '		</nav>';

$filterChampion = ((isset($_GET['filterChampion']))&&($_GET['filterChampion']!='')) ? $_GET['filterChampion'] : null;
$filterLegacy = ((isset($_GET['filterLegacy']))&&(intval($_GET['filterLegacy'])===1));

if(!$ch->isValidChampionId($filterChampion)){
	$filterChampion = null;
}

switch($currentContent){
	case 'skins':
	default:
		$type = SalesManager::TYPE_SKINS;
		break;
	case 'champions':
		$type = SalesManager::TYPE_CHAMPIONS;
		break;
}

$filter = [];
if($filterChampion!=null){
	$filter['champion'] = $filterChampion;
}
if($filterLegacy===false){
	$filter['legacy'] = $filterLegacy;
}

$page = ((isset($_GET['p']))&&($_GET['p']!='')) ? intval($_GET['p']) : 1;
$limit = 30;
$offset = ($page * $limit) - $limit;
$total = $sam->getTotal($type, $filter);

$paginator = new Paginator();
$paginator->setTotalPages(ceil($total / $limit));
$paginator->setCurrentPage($page);
$paginator->setPaginationVariance(5);
$paginator->setLink('sales/'.$currentContent.'/page-%p?filterChampion='.$filterChampion.'&filterLegacy='.+$filterLegacy);
$paginator->setShowFirst(true);
$paginator->setShowPrevious(true);
$paginator->setShowNext(true);
$paginator->setShowLast(true);
$pagination = $paginator->paginate();

$template_content = $am->getLeaderboard();
$template_content.= '<section id="sales">
	<div class="container">';
$template_content.= $contentSelect;
$template_content.= '	</div>
	<div class="container filter">
		<form id="form-filter-sales" class="form-inline" action="sales/'.$currentContent.'" method="get">
			<div class="container">
				<div class="row w-100">
					<div class="col-12 col-sm-6 col-md-4 mb-1 mb-sm-0">
						<select id="filterChampion" name="filterChampion" class="form-control mb-3 mr-sm-3 mb-sm-0">
							<option value="">All Champions</option>';
foreach($ch->getChampions() as $champion){
	$template_content.= '							<option value="'.$champion.'"'.(($filterChampion==$champion) ? ' selected' : '').'>'.$ch->getDisplayName($champion).'</option>';
}
$template_content.= '						</select>
					</div>
					<div class="col-12 col-sm-6 col-md-4 mb-1 mb-sm-3 mb-md-0 d-flex align-items-center justify-content-start justify-content-sm-center">
						<div class="form-check form-check-inline">
							<label class="form-check-label">
								<input id="filterLegacy" name="filterLegacy" class="form-check-input" type="checkbox" id="inlineCheckbox1" value="1"'.($filterLegacy ? ' checked' : '').'> Include Legacy Skins
							</label>
						</div>
					</div>
					<div class="col-12 col-md-4">
						<button type="submit" class="btn btn-outline-primary">Apply</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div class="container">';

$data = $sam->getData($type, $offset, $limit, $filter);
$template_content.= $pagination;
$template_content.= $sam->getPrintableTable($type, $data);
$template_content.= $pagination;

$template_content.= '	</div>
</section>';
$template_content.= $am->getLeaderboard();

include_once(ABSOLUTE_PATH.'/template.php');
?>