<?php 
global $myUser;
if(!$myUser->connected()) throw new Exception('Vous devez être connecté pour accéder à cette fonctionnalité');
if(!$myUser->can('notification', 'read')) return;
require_once(__DIR__.SLASH.'Notification.class.php');

$myUser->loadPreferences();
$sendMail = $myUser->preference('notification_send_mail');
$userAlerts = json_decode($myUser->preference('notification_categories'),true);
$userAlerts = !$userAlerts ? array() : $userAlerts;
$categories = array();

foreach(Notification::types() as $slug => $type): 
	$type['slug'] = $slug;
	if(!isset($type['category'])) $type['category'] = 'Général';

	if(!isset($categories[slugify($type['category'])])) $categories[slugify($type['category'])] = array('label'=>$type['category'],'items'=>array());
	$categories[slugify($type['category'])]['items'][] =  $type;
endforeach;

?>
<div class="row">
	<div class="col-md-12" id="notification_preference">
		<br>
		<div onclick="notification_user_save_preference()" class="btn btn-success right"><i class="fas fa-check"></i> Enregistrer</div>
		<h3>Notifications</h3>
		<hr>
		<div class="row">
			<div class="col-md-12">
				<label for="notification_send_mail">Envoyer mes notifications à cette adresse mail : </label>
				<input id="notification_send_mail" name="notification_send_mail" class="form-control" placeholder="j.doe@email.com" value="<?php echo !empty($sendMail) ? $sendMail : $myUser->mail;; ?>" type="text"><br>
			</div>
			<div class="col-md-12 notification_categories" id="notification_categories">
				<h5>Suivre les catégories : </h5>
				<div class="row">
				  <div class="col-3">
				    <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
				    <?php 
				    	$i = 0;
				    	foreach($categories as $categorySlug=>$category): ?>
				    	<a class="nav-link <?php echo $i==0?'active':'' ?>" id="v-pills-home-tab" data-toggle="pill" href="#category-<?php echo $categorySlug ?>" role="tab" aria-controls="v-pills-home" aria-selected="true"><i class="far fa-bookmark"></i> <?php echo $category['label'] ?></a>
				   		<?php 
				   		$i++;
				   		endforeach; ?>
				    </div>
				  </div>
				  <div class="col-9">
				    <div class="tab-content" id="v-pills-tabContent">
				     <?php 
				     $i = 0;
				     foreach($categories as $categorySlug=>$category): ?>
				      <div class="tab-pane fade show <?php echo $i==0?'active':'' ?>" id="category-<?php echo $categorySlug ?>" role="tabpanel" aria-labelledby="v-pills-home-tab">
							<?php foreach($category['items'] as $type): ?>
									<div data-slug="<?php echo $type['slug']; ?>" class="list-group-item list-group-item-action flex-column align-items-start category no-select mb-2">
										<div class="d-flex w-100 justify-content-between">
											<h5 class="mb-1" style="color:<?php echo $type['color']; ?>;"><i class="<?php echo $type['icon']; ?>"></i> <?php echo $type['label']; ?></h5>
											<small>
												<input data-type="checkbox" <?php echo in_array($type['slug'], $userAlerts) ? 'checked="checked"' : '' ?> class="form-check-input">
											</small>
										</div>
										<p class="mb-1"><?php echo $type['description']; ?></p>
										<small>Cochez la case pour être averti par e-mail.</small>
									</div>
							<?php 
							$i++;
							endforeach; ?>
				      </div>
				      <?php endforeach; ?>
				    </div>
				  </div>
				</div>


			</div>
		</div>
	</div>
</div><br>
