<?php

define('DEFAULT_TITLE', 'Mon panier bio');

define('SUPPLIER_REGEX', '[A-Za-z]\w{0,31}');
define('EVENT_REGEX', '\d{4}\-[01]\d\-[0123]\d');
define('EVENT_FORMAT', 'Y-m-d');
define('REQUEST_REGEX', '/^https?:\/\/.+\/(?<supplier>' . SUPPLIER_REGEX . ')\/?(?<event>' . EVENT_REGEX . ')?\/?$/');
define('ACTION_REGEX', '/^[a-z]{1,16}$/i');

$baseUrl = trim((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '/');
if (($pos = strpos($baseUrl, '?')) !== false)
	$baseUrl = substr($baseUrl, 0, $pos);
$requestUrl = trim(array_key_exists('QUERY_STRING', $_SERVER) ? str_replace($_SERVER['QUERY_STRING'], '', $baseUrl) : $baseUrl, '?');

if (preg_match(REQUEST_REGEX, $requestUrl, $match)) {
	$requestSupplier = array_key_exists('supplier', $match) ? $match['supplier'] : null;
	$requestEvent = array_key_exists('event', $match) ? $match['event'] : null;

	if (!is_null($requestEvent))
		$requestUrl = rtrim(str_replace($requestEvent, '', $requestUrl), '/');
	if (!is_null($requestSupplier))
		$requestUrl = rtrim(str_replace($requestSupplier, '', $requestUrl), '/');
} else {
	$requestSupplier = null;
	$requestEvent = null;
}

function isInPast($event) {
	$now = new \DateTimeImmutable('now');
	$then = new \DateTimeImmutable($event);
	return $then->getTimestamp() < $now->getTimestamp();
}

function ago($value) {
	$now = new \DateTimeImmutable('now 00:00:00');
	$value = (clone $value)->setTime(0, 0, 0);
	$diff = $now->diff($value, false);
	if (abs($diff->y) > 0) $output = sprintf('%d an%s', $diff->y, $diff->y > 1 ? 's' : '');
	elseif (abs($diff->m) > 0) $output = sprintf('%d mois', $diff->m);
	elseif (abs($diff->d) > 1) $output = sprintf('%d jours', $diff->d);
	if (isset($output)) $output = sprintf('%s %s', ($diff->invert === 1 ? 'il y a' : 'dans'), $output);
	elseif (abs($diff->d) > 0) $output = $diff->invert ? 'hier' : 'demain';
	else $output = 'aujourd\'hui';
	return $output;
}

function generatePassword($length = 20) {
	$chars = array_merge(
		range('A', 'Z'),
		range('a', 'z'),
		range('0', '9'),
		[ '!', '?', '~', '@', '#', '$', '%', '*', ';', ':', '-', '+', '=', ',', '.', '_' ]
	);
	while ($length-- > 0)
		$value .= $chars[mt_rand(0, count($chars) - 1)];
	return $value;
}

function generateUrl($supplier = null, $event = null) {
	global $requestUrl, $inIframe;

  $queryString = $inIframe ? '?iframe' : '';

	if (is_null($supplier))
		return $requestUrl . $queryString;

	if (is_null($event))
		return sprintf('%s/%s', $requestUrl, $supplier) . $queryString;

	return sprintf('%s/%s/%s', $requestUrl, $supplier, $event) . $queryString;
}

function findNext($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000, $direction = +1) {
	$now = new \DateTime('now');
	$current = clone $start;
	$frequency = \DateInterval::createFromDateString($frequency);
	do {
		if ($direction === abs($direction)) {
			if (!$vsNow and ($maxIterations-- > 0)) {
				$current->add($frequency);
			} else {
				while (
					($current->getTimestamp() < $now->getTimestamp())
					and ($maxIterations-- > 0)
				) $current->add($frequency);
			}
		} else {
			if (!$vsNow and ($maxIterations-- > 0)) {
				$current->sub($frequency);
			} else {
				while (
					($current->getTimestamp() > $now->getTimestamp())
					and ($maxIterations-- > 0)
				) $current->sub($frequency);
			}
		}
		$nextEvent = $current->format('Y-m-d');
	} while (
		in_array($nextEvent, $excludes)
		and ($maxIterations > 0)
	);
	return $current;
}

function findPrevious($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000) {
	return findNext($start, $frequency, $excludes, $vsNow, $maxIterations, -1);
}

define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');

if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
if (!isset($config)) $config = [];

$inIframe = isset($_REQUEST['iframe']);
$action = (isset($_REQUEST['action']) and preg_match(ACTION_REGEX, $_REQUEST['action'])) ? $_REQUEST['action'] : null;

$supplier = array_key_exists('supplier', $_REQUEST) ? $_REQUEST['supplier'] : $requestSupplier;
$hasSupplier = is_string($supplier) and preg_match('/^' . SUPPLIER_REGEX . '$/', $supplier);

$excludesFormatter = new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, 'Europe/Paris');

$supplierIsNew = false;
if ($hasSupplier) {

	if (!isset($config[$supplier])) {
		$config[$supplier] = [];
		$supplierIsNew = true;
	}

	$config[$supplier] = array_merge(
		[
			'title' => '',
			'subtitle' => '<small class="%color% text-nowrap d-block d-sm-inline">%date% (%ago%)</small>',
			'description' => '',
			'choices' => [],
			'start' => 'now 00:00:00',
			'end' => '+1 year 23:59:59',
			'frequency' => '1 day',
			'password' => '',
			'excludes' => [],
		],
		$config[$supplier]
	);

	$hasPassword = !empty($config[$supplier]['password']);

	if ($action === 'config') {
		if ($hasPassword) {
			if (!isset($_SERVER['PHP_AUTH_USER'])) {
				header(sprintf('WWW-Authenticate: Basic realm="Configuration de mon panier bio pour %s"', $supplier));
				header('HTTP/1.0 401 Unauthorized');
				printf('Cette configuration est protégée par mot de passe !');
				exit;
			} elseif (
				($_SERVER['PHP_AUTH_USER'] !== $supplier)
				or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
			) {
				header('HTTP/1.0 403 Forbidden');
				printf('Cette configuration est protégée par mot de passe !');
				exit;
			}
		}

		foreach (array_keys($config[$supplier]) as $key)
			if (isset($_REQUEST[$key]))
				$config[$supplier][$key] = (!in_array($key, ['title', 'subtitle', 'description']) ? filter_var($_REQUEST[$key], FILTER_SANITIZE_STRING) : $_REQUEST[$key]);
	}

	if (empty($config[$supplier]['start']))
		$config[$supplier]['start'] = 'now 00:00:00';


	foreach (['choices', 'excludes'] as $key) {
		if (is_string($config[$supplier][$key]))
			$config[$supplier][$key] = explode(PHP_EOL, $config[$supplier][$key]);

		if (!is_array($config[$supplier][$key]))
			$config[$supplier][$key] = [];

		$config[$supplier][$key] = array_filter(
			$config[$supplier][$key],
			function ($choice) {
				return is_string($choice) and !empty(trim($choice));
			}
		);

		$config[$supplier][$key] = array_map('trim', $config[$supplier][$key]);
	}

	$config[$supplier]['excludes'] = array_filter( 
		array_map(
			function ($value) use ($excludesFormatter) {
				if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value))
					return $value;

				$timestamp = $excludesFormatter->parse($value, $offset);

				if ($timestamp !== false)
					return (new \DateTimeImmutable('@' . $timestamp, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');

				try {
					return (new \DateTimeImmutable($value, new \DateTimeZone('Europe/Paris')))->format('Y-m-d');
				} catch (\Exception $exception) {
					return null;
				}
			},
			$config[$supplier]['excludes'] 
		),
		function ($value) {
			return !is_null($value);
		}
	);
}

$isConfig = false;
if ($action === 'config') {
	$output = fopen(CONFIG_FILE, 'w+');
	if ($output) {
		if (flock($output, LOCK_EX)) {
			fwrite($output, '<?php' . PHP_EOL);
			fprintf(
				$output,
				'$config = %s;' . PHP_EOL,
				var_export($config, true)
			);
			flock($output, LOCK_UN);
		}
		fclose($output);
	}
	$isConfig = true;
}

$suppliers = array_keys($config);
sort($suppliers);

try {
	$event = array_key_exists('event', $_REQUEST) ? $_REQUEST['event'] : $requestEvent;
	$hasEvent = (
		is_string($event)
		and preg_match('/^' . EVENT_REGEX . '$/', $event)
		and ((new \DateTimeImmutable($event)) instanceof \DateTimeImmutable)
	);
} catch (\Exception $exception) {
	$hasEvent = false;
}
if (!$isConfig and !$supplierIsNew and $hasSupplier) {
	$start = new \DateTime($config[$supplier]['start']);
	if (!$hasEvent) {
		$next = findNext($start, $config[$supplier]['frequency'], $config[$supplier]['excludes'], true);
		$nextEvent = $next->format('Y-m-d');
		header('Location: ' . generateUrl($supplier, $nextEvent));
		die();
	} else {
		$current = new \DateTime($event);
		$previous = findPrevious($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
		$previousEvent = $previous->format('Y-m-d');
		if (false and !array_key_exists($previousEvent, $data[$supplier]))
			unset($previousEvent);
		$first = new \DateTime($config[$supplier]['start']);
		if (true and ($previous->getTimestamp() < $first->getTimestamp()))
			unset($previousEvent);
		$next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
		$nextEvent = $next->format('Y-m-d');
		if (false and !array_key_exists($nextEvent, $data[$supplier]))
			unset($nextEvent);
		$last = new \DateTime($config[$supplier]['end']);
		if (true and ($next->getTimestamp() > $last->getTimestamp()))
			unset($nextEvent);
	}

	switch ($action) {
	case 'insert' :
	case 'delete' :
		$item = [];

		foreach (['name', 'choice', 'action'] as $field)
			$item[$field] = filter_var($_REQUEST[$field], FILTER_SANITIZE_STRING);
		$item['timestamp'] = time();
		$hash = md5(implode([ trim($item['name']), $item['choice'], ]));
		$item['hash'] = $hash;

		$isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
		$output = fopen(DATA_FILE, 'a+');
		if (!$output) break;
		if (!flock($output, LOCK_EX)) break;
		if ($isBeginning)
			fwrite($output, '<?php' . PHP_EOL);
		fprintf(
			$output,
			'$data[%s][%s][] = %s;' . PHP_EOL,
			var_export($supplier, true),
			var_export($event, true),
			str_replace(PHP_EOL, '', var_export($item, true))
		);
		flock($output, LOCK_UN);
		fclose($output);
		header('Location: ' . generateUrl($supplier, $event));
		die();
	}

	if (!isset($data)) $data = [];
	if (file_exists(DATA_FILE)) include DATA_FILE;

	$items = [];

	$allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];

	usort($allItems, function ($a, $b) {
		$a = intval($a['timestamp']);
		$b = intval($b['timestamp']);
		if ($a === $b)
			return 0;
		return ($a < $b) ? -1 : 1;
	});

	foreach ($allItems as $item) {
		if ($item['action'] === 'insert') {
			$alreadyInserted = false;
			foreach ($items as $index => $prevItem)
				if ($prevItem['hash'] === $item['hash'])
					$alreadyInserted = true;
			if (!$alreadyInserted)
				$items[] = $item;
		} elseif ($item['action'] === 'delete') {
			foreach ($items as $index => $prevItem)
				if ($prevItem['hash'] === $item['hash'])
					unset($items[$index]);
		}
	}

	$date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
	$ago = ago(new \DateTimeImmutable($event));
	$color = isInPast($event) ? 'text-danger' : 'text-muted';

  $currentEvent = findNext(new \DateTime($config[$supplier]['start']), $config[$supplier]['frequency'], $config[$supplier]['excludes'], true);
	$currentDate = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format($currentEvent);
	$currentAgo = ago($currentEvent);

	foreach (['title', 'subtitle', 'description'] as $key) {
		while (preg_match('/%([^%]+)%/i', $config[$supplier][$key], $match))
			$config[$supplier][$key] = str_replace(
				$match[0],
				${$match[1]},
				$config[$supplier][$key]
			);
	}

	if (empty($config[$supplier]['title']))
		$config[$supplier]['title'] = $supplier;

	$stats = [];
	foreach ($items as $item)
		if (!empty($item['choice']))
			$stats[$item['choice']] += 1;

}

if ($supplierIsNew and !empty($suppliers)) {
	$closestSuppliers = array_filter(
		array_map(
			function ($other) use ($supplier) {
				return [
					'supplier' => $other,
					'score' => levenshtein($supplier, $other),
				];
			},
			$suppliers
		),
		function ($item) {
			return $item['score'] > 0;
		}
	);
	usort($closestSuppliers, function ($a, $b) {
		if ($a['score'] == $b['score']) {
			return 0;
		}
		return ($a['score'] < $b['score']) ? -1 : 1;
	});
}

$linkUrl = !$hasSupplier ? generateUrl() : (!$hasEvent ? generateUrl($supplier) : generateUrl($supplier, $event));

?><!DOCTYPE html>
<html lang="fr">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<title><?php if ($hasSupplier) : ?><?php echo strip_tags($config[$supplier]['title']); ?><?php if (!$isConfig) : ?> — <?php echo strip_tags($config[$supplier]['subtitle']); ?><?php endif; ?><?php else : ?><?php echo DEFAULT_TITLE; ?><?php endif; ?></title>
		<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
		<style type="text/css">.is-fixed { position: fixed; bottom: 0; width: 100%; box-shadow: 0 0 0.5em rgba(0, 0, 0, 0.5); }</style>
		<style type="text/css">.sortable th.dir-d::after{color:inherit;content:' \025BE'}.sortable th.dir-u::after{color:inherit;content:' \025B4'}</style>
	</head>
	<body>
    <?php if (!$inIframe) : ?>
		<header>
			<nav class="navbar navbar-dark bg-dark">
				<div class="container-fluid">
					<a class="navbar-brand" href="<?php echo $hasSupplier ? generateUrl($supplier) : generateUrl(); ?>">
						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-basket d-inline-block align-text-top" viewBox="0 0 16 16">
							<path d="M5.757 1.071a.5.5 0 0 1 .172.686L3.383 6h9.234L10.07 1.757a.5.5 0 1 1 .858-.514L13.783 6H15a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1v4.5a2.5 2.5 0 0 1-2.5 2.5h-9A2.5 2.5 0 0 1 1 13.5V9a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h1.217L5.07 1.243a.5.5 0 0 1 .686-.172zM2 9v4.5A1.5 1.5 0 0 0 3.5 15h9a1.5 1.5 0 0 0 1.5-1.5V9H2zM1 7v1h14V7H1zm3 3a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 4 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 6 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 8 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5z"/>
						</svg>
						<?php echo $hasSupplier ? $supplier : DEFAULT_TITLE; ?>
					</a>
					<span class="navbar-text text-muted">
						<a class="text-reset me-3" data-bs-toggle="modal" href="#linkModal">Lien</a>
						<?php if ($hasSupplier) : ?>
							<?php if ($isConfig) : ?>
								<a class="text-reset" href="<?php echo generateUrl($supplier); ?>">Retour</a>
							<?php else : ?>
								<a tabindex="-1" class="text-reset" href="<?php printf('%s?action=config', generateUrl($supplier)); ?>">
								<?php if ($hasPassword) : ?>
								<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
									<path d="M8 1a2 2 0 0 1 2 2v4H6V3a2 2 0 0 1 2-2zm3 6V3a3 3 0 0 0-6 0v4a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2zM5 8h6a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V9a1 1 0 0 1 1-1z"/>
								</svg>
								<?php else : ?>
								<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
									<path d="M11 1a2 2 0 0 0-2 2v4a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h5V3a3 3 0 0 1 6 0v4a.5.5 0 0 1-1 0V3a2 2 0 0 0-2-2zM3 8a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V9a1 1 0 0 0-1-1H3z"/>
								</svg>
								<?php endif; ?>
									Configuration
								</a>
							<?php endif; ?>
						<?php endif; ?>
					</span>
				</div>
			</nav>
		</header>
    <?php endif; // !$inIframe ?>
		<main>
		<?php if (!$hasSupplier) : ?>
			<section class="container-fluid pt-3">
				<div class="alert alert-danger alert-dismissible mb-3" role="alert">
					⚠ Pas de fournisseur !
					<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
				</div>
				<div class="row mb-3 g-3">
					<div class="col-12">
						<form action="<?php echo generateUrl(); ?>" method="post">
              <?php if ($inIframe) : ?><input type="hidden" name="iframe" /><?php endif; // $inIframe ?>
							<datalist id="supplierList">
								<?php foreach ($suppliers as $supplier) : ?>
								<option value="<?php echo $supplier; ?>" />
								<?php endforeach; ?>
							</datalist>
							<div class="input-group input-group-lg">
								<span class="input-group-text">
									<span class="d-none d-sm-inline"><?php echo generateUrl(); ?></span>
									<span class="d-inline d-sm-none" title="<?php echo generateUrl(); ?>">&hellip;</span>
									/
								</span>
								<input type="text" class="form-control js-closealerts" name="supplier" list="supplierList" required placeholder="MonFournisseur" tabindex="1" autofocus />
								<button class="btn btn-primary" type="submit">Aller&nbsp;&rarr;</button>
							</div>
						</form>
					</div>
					<div class="col-12">
						<details>
							<summary>En savoir plus…</summary>
              <p>La documentation se trouvera ici quand elle sera prête.</p>
              <p>En attendant, ce logiciel est <a href="https://fr.wikipedia.org/wiki/WTFPL" target="_blank">libre</a> et ses sources sont disponibles <a href="https://caboulot.org/gitea/vince/mon-panier-bio" target="_blank">ici</a>.</p>
              <p><i>utere felix</i></p>
						</details>
					</div>
				</div>
			</section>
		<?php else : ?>
			<?php if ($isConfig) : ?>
				<section class="container-fluid">
					<div class="row my-3 g-3">
						<div class="col">
							<h1>Configuration</h1>
						</div>
					</div>
				</section>
				<section class="container-fluid">
					<div class="row g-3">	
						<form action="<?php echo generateUrl($supplier); ?>" method="post">
              <?php if ($inIframe) : ?><input type="hidden" name="iframe" /><?php endif; // $inIframe ?>
							<div class="row mb-3">
								<label for="title" class="col-sm-2 col-form-label">Titre</label>
								<div class="col-sm-10">
									<input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
									<div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
								</div>
							</div>
							<div class="row mb-3">
								<label for="description" class="col-sm-2 col-form-label">Description</label>
								<div class="col-sm-10">
									<textarea class="form-control js-ckeditor" name="description" rows="20"><?php echo $config[$supplier]['description']; ?></textarea>
									<div class="form-text">La description affichée sous le titre.</div>
								</div>
							</div>
							<div class="row mb-3">
								<label for="choices" class="col-sm-2 col-form-label">Choix</label>
								<div class="col-sm-10">
									<textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
									<div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
								</div>
							</div>
							<div class="row mb-3">
								<label for="start" class="col-sm-2 col-form-label">Début</label>
								<div class="col-sm-10">
									<input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
									<div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
								</div>
							</div>
							<div class="row mb-3">
								<label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
								<div class="col-sm-10">
									<input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
									<div class="form-text">La fréquence des événements dans le format <a class="text-reset" href="https://www.php.net/manual/fr/datetime.formats.relative.php" target="_blank">décrit sur cette page</a>.</div>
								</div>
							</div>
							<div class="row mb-3">
								<label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
								<div class="col-sm-10">
									<textarea class="form-control" name="excludes" rows="5"><?php echo implode(PHP_EOL, array_map(function ($value) use ($excludesFormatter) { return $excludesFormatter->format(new \DateTimeImmutable($value, new \DateTimeZone('Europe/Paris'))); }, $config[$supplier]['excludes'])); ?></textarea>
									<div class="form-text">Les dates à exclure. Une par ligne. Ou pas. En tous cas le format c'est celui de l'<a class="text-reset" href="https://unicode-org.github.io/icu/userguide/format_parse/datetime/" target="_blank">ICU</a> : <kbd><?php echo $excludesFormatter->getPattern(); ?></kbd>. Par exemple <kbd><?php echo $excludesFormatter->format(new \DateTimeImmutable('first day of january this year', new \DateTimeZone('Europe/Paris'))); ?></kbd>, <kbd><?php echo $excludesFormatter->format(new \DateTimeImmutable('now', new \DateTimeZone('Europe/Paris'))); ?></kbd> ou <kbd><?php echo $excludesFormatter->format(new \DateTimeImmutable('last day of december this year', new \DateTimeZone('Europe/Paris'))); ?></kbd>.</div>
								</div>
							</div>

							<div class="row mb-3">
								<label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
								<div class="col-sm-10">
									<input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
									<div class="form-text">Ce mot de passe sera demandé pour accéder à la configuration la prochaine fois. Le nom d'utilisateur est le fournisseur courant (en l'occurrence <kbd><?php echo $supplier; ?></kbd>). Par exemple <kbd><?php echo generatePassword(); ?></kbd>. Et pas de mot de passe, pas de protection.</div>
								</div>
							</div>
							<div class="row">
								<div class="col px-0">
									<div class="js-fixed bg-light p-3">
										<button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
									</div>
								</div>
							</div>
						</form>
					</div>
				</section>
			<?php else /* !$isConfig */ : ?>
				<?php if ($supplierIsNew) : ?>
					<section class="container-fluid pt-3">
						<div class="alert alert-warning alert-dismissible" role="alert">
							⚠ Ce fournisseur n'existe pas encore !
							<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
						</div>
						<div class="row g-3">
							<div class="col-xs-12 col-sm-6">
								<div class="card h-100">
									<div class="card-body">
										<h2 class="card-title">Oops !</h2>
										<p class="card-text">Le nom du fournisseur « <tt><?php echo $supplier; ?></tt> » est probablement mal orthographié, c'est pour ça qu'il n'existe pas.</p>
										<p class="card-text">
											Peut-être sagissait-il de
											<?php $max = 3; foreach ($closestSuppliers as $index => $item) : ?>
											<?php if ($index < $max) : ?>
											<?php if ($index > 0) : ?>
												<?php if ($index === min($max, count($closestSuppliers) - 1)) : ?>
												ou
												<?php else : ?>
												,
												<?php endif; ?>
											<?php endif; ?>
											« <tt><a class="card-link" href="<?php echo generateUrl($item['supplier']); ?>"><?php echo $item['supplier']; ?></a></tt> »
											<?php endif; ?>
											<?php endforeach; ?>
											?
										</p>
										<a class="btn btn-primary" href="<?php echo generateUrl(); ?>">Recommencer</a>
									</div>
								</div>
							</div>
							<div class="col-xs-12 col-sm-6">
								<div class="card h-100">
									<div class="card-body">
										<h2 class="card-title">C'est normal !</h2>
										<p class="card-text">On souhaite le créer.</p>
										<p class="card_text">Une fois configuré il sera prêt à être utilisé.</p>
										<a class="btn btn-primary" href="<?php echo generateUrl($supplier) . '?action=config'; ?>">Configurer</a>
									</div>
								</div>
							</div>
						</div>
					</section>
				<?php else /* !$supplierIsNew */ : ?>
					<section class="container-fluid">
						<div class="row my-3">
							<div class="col">
								<h1>
                  <?php if (!$inIframe) : ?>
									<div class="btn-group float-end" role="group">
										<?php if (isset($previousEvent)) : ?>
											<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
												<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
													<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
												</svg>
											</a>
										<?php endif; ?>
										<?php /* ?>
										<a class="btn btn-outline-primary d-none d-sm-inline" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
											<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
												<path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z"/>
												<path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z"/>
											</svg>
										</a>
										<?php */ ?>
										<?php if (isset($nextEvent)) : ?>
											<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
												<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
													<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
												</svg>
											</a>
										<?php endif; ?>
									</div>
                  <?php endif; // !$inIframe ?>
									<?php echo $config[$supplier]['title']; ?>
									<?php echo $config[$supplier]['subtitle']; ?>
								</h1>
								<?php if (!empty($config[$supplier]['description'])) : ?>
								<p class="lead"><?php echo $config[$supplier]['description']; ?></p>
								<?php endif; ?>
							</div>
						</div>
					</section>
					<section class="container-fluid">
						<div class="row g-3">	
							<form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
                <?php if ($inIframe) : ?><input type="hidden" name="iframe" /><?php endif; // $inIframe ?>
								<div class="row my-3">
									<label for="title" class="col-sm-2 col-form-label">Nom</label>
									<div class="col-sm-10">
										<input class="form-control" type="text" name="name" required placeholder="Nom" />
									</div>
								</div>
								<?php if (!empty($config[$supplier]['choices'])) : ?>
								<div class="row mb-3">
									<label for="title" class="col-sm-2 col-form-label">Choix</label>
									<div class="col-sm-10">
										<div class="btn-group" role="group">
											<?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
											<input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" required />
											<label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
											<?php endforeach; ?>
										</div>
									</div>
								</div>
								<?php endif; ?>
								<div class="row">
									<div class="col mb-3">
										<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
										<input type="hidden" name="event" value="<?php echo $event; ?>" />
										<?php if (empty($config[$supplier]['choices'])) : ?>
										<input type="hidden" name="choice" value="" />
										<?php endif; ?>
                    <?php if (isInPast($event)) :?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                      ⚠ Êtes-vous sûr·e de vouloir commander pour <strong>le <?php echo $date; ?> (<?php echo $ago; ?>)</strong> et pas plutôt pour <strong><a href="<?php echo generateUrl($supplier, $currentEvent->format(EVENT_FORMAT)); ?>">le <?php echo $currentDate; ?> (<?php echo $currentAgo; ?>)</a></strong> ?
                      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Fermer"></button>
                    </div>
                    <?php endif; ?>
										<button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
									</div>
								</div>
							</form>
						</div>
					</section>
					<section class="container-fluid">
						<div class="row my-3">
							<?php if (!empty($items)) : ?>
								<div class="col-12">
									<div class="table-responsive">
										<table class="table table-striped table-hover align-middle sortable">
											<thead>
												<tr>
													<th scope="col">
														Nom
													</th>
													<?php if (!empty($config[$supplier]['choices'])) : ?>
													<th scope="col">
														Choix
													</th>
													<?php endif; ?>
													<th scope="col" class="no-sort">
														&nbsp;
													</th>
												</tr>
											</thead>
											<tbody>
												<?php foreach ($items as $item) : ?>
													<tr>
														<td>
															<?php echo $item['name']; ?>
														</td>
														<?php if (!empty($config[$supplier]['choices'])) : ?>
														<td>
															<?php if (!empty($item['choice'])) : ?>
															<?php echo $item['choice']; ?>
															<?php endif; ?>
														</td>
														<?php endif; ?>
														<td>	
															<form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
                                <?php if ($inIframe) : ?><input type="hidden" name="iframe" /><?php endif; // $inIframe ?>
																<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
																<input type="hidden" name="event" value="<?php echo $event; ?>" />
																<input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
																<input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
																<button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
															</form>
														</td>
													</tr>
												<?php endforeach; ?>
											</tbody>
										</table>
									</div>
								</div>
							<?php endif; ?>
              <?php if (!$inIframe) : ?>                  
							<div class="col-12">
								<div class="accordion accordion-flush">
									<div class="accordion-item">
										<div id="accordion1" class="accordion-collapse collapse">
											<div class="accordion-body">
												<ul class="list-group">
													<?php foreach ($stats as $choice => $count) : ?>
														<li class="list-group-item d-flex justify-content-between align-items-center">
															<?php echo $choice; ?>
															<span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
														</li>
													<?php endforeach; ?>
												</ul>
											</div>
										</div>
										<h2 class="accordion-header">
											<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#accordion1" aria-expanded="false">
												Commandes
												<span class="badge bg-primary rounded-pill ms-1"><?php echo count($items); ?></span>
											</button>
										</h2>
									</div>
								</div>
							</div>
              <?php endif; // !$inIframe ?>
						</div>
					</section>
				<?php endif; /* $supplierIsNew */ ?>
			<?php endif; /* $isConfig*/ ?>
		<?php endif; ?>
		</main>
		
		<div class="modal fade" id="linkModal" tabindex="-1" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<h5 class="modal-title">Lien</h5>
						<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
					</div>
					<div class="modal-body">
						<div class="container-fluid">
							<div class="row g-3">
								<div class="col-12">
									Adresse web
								</div>
								<div class="col-12 text-center">
									<a href="<?php echo $linkUrl; ?>"><tt id="linkURL"><?php echo $linkUrl; ?></tt></a>
									<button class="btn btn-outline-dark js-clipboard" type="button" role="button" data-clipboard-target="#linkURL" data-bs-toggle="tooltip" data-bs-trigger="manual">
										<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
											<path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/>
											<path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/>
										</svg>
									</button>
								</div>
                <?php if ($hasSupplier) : ?>
								<div class="col-12">
									IFrame
								</div>
								<div class="col-12 text-center">
                  <pre id="iframeCode"><?php ob_start();  ?>
<iframe src="<?php echo generateUrl($supplier); ?>">
</iframe>
                  <?php echo htmlentities(ob_get_clean()); ?></pre>
									<button class="btn btn-outline-dark js-clipboard" type="button" role="button" data-clipboard-target="#iframeCode" data-bs-toggle="tooltip" data-bs-trigger="manual">
										<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
											<path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/>
											<path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/>
										</svg>
									</button>
								</div>
                <?php endif; // $hasSupplier ?>
								<div class="col-12">
									QR Code
								</div>
								<div class="col-12">
									<div id="linkQRCode"></div>
								</div>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
					</div>
				</div>
			</div>
		</div>

		<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
		<?php if ($isConfig) : ?>
		<script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
		<script>
			document.querySelectorAll('.js-ckeditor').forEach(function (element) {
				ClassicEditor.create(element).catch(error => { console.error(error); });
			});
		</script>
		<?php else : ?>
		<script>document.addEventListener("click",function(b){function n(a,e){a.className=a.className.replace(u,"")+e}function p(a){return a.getAttribute("data-sort")||a.innerText}var u=/ dir-(u|d) /,c=/\bsortable\b/;b=b.target;if("TH"===b.nodeName)try{var q=b.parentNode,f=q.parentNode.parentNode;if(c.test(f.className)){var g,d=q.cells;for(c=0;c<d.length;c++)d[c]===b?g=c:n(d[c],"");d=" dir-d ";-1!==b.className.indexOf(" dir-d ")&&(d=" dir-u ");n(b,d);var h=f.tBodies[0],k=[].slice.call(h.rows,0),r=" dir-u "===d;k.sort(function(a,
e){var l=p((r?a:e).cells[g]),m=p((r?e:a).cells[g]);return isNaN(l-m)?l.localeCompare(m):l-m});for(var t=h.cloneNode();k.length;)t.appendChild(k.splice(0,1)[0]);f.replaceChild(t,h)}}catch(a){}});</script>
		<?php endif; ?>
		<script>
			document.addEventListener('DOMContentLoaded', function () {
				document.querySelectorAll('.js-localremember').forEach(function (form) {
					const fields = [ 'name', 'choice' ];

					form.addEventListener('submit', function (event) {
						fields.forEach(function (field) {
							window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
						});
					});

					fields.forEach(function (field) {
						if (
							(form.elements[field].value === '')
							&& (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
						) {
							form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
						}
					});
				});
				document.querySelectorAll('.js-closealerts').forEach(function (element) {
					element.addEventListener('input', function (event) {
						if (event.target.value !== '') {
							document.querySelectorAll('.alert').forEach(function (alertElement) {
								var alert = bootstrap.Alert.getOrCreateInstance(alertElement)
								alert.close();
							});
						}
					});
				});
				var qrcode = new QRCode('linkQRCode', {
					text: document.getElementById('linkURL').innerText,
					width: 300,
					height: 300,
					colorDark : '#000000',
					colorLight : '#ffffff',
					correctLevel : QRCode.CorrectLevel.H,
				});
				document.querySelector('#linkQRCode img').classList.add('img-fluid', 'mx-auto', 'd-block');
				var clipboard = new ClipboardJS('.js-clipboard');
				clipboard.on('success', function (event) {
					var tooltip = new bootstrap.Tooltip(event.trigger, {
						title: 'Copié dans le presse-papier'
					});
					tooltip.show();
				});
				document.querySelectorAll('.js-fixed').forEach(function (element) {
					const height = window.getComputedStyle(element).height;
					element.parentElement.style.height = height;
					element.classList.add('is-fixed');
				});
			}, false);
		</script>
	</body>
</html>