<?php
session_start();
if((!isset ($_SESSION['login']) == true) and (!isset ($_SESSION['password']) == true))
{
  unset($_SESSION['login']);
  unset($_SESSION['senha']);
  header('location:/controle/?error=1');
  }

$email = $_SESSION['login'];
  
date_default_timezone_set('America/Sao_Paulo');
$error = $_GET['error'];

include("conecta/conecta.php");

$sql = $conecta->query("SELECT nome_site, favicon_site FROM config_site WHERE id = '1'");
while ($consulta = $sql->fetch_assoc())
{$nome_site = $consulta['nome_site'];
 $favicon_site = $consulta['favicon_site'];}

$sql1 = $conecta->query("SELECT id, nome FROM usuarios WHERE email = '$email'");
while ($consulta1 = $sql1->fetch_assoc())
{$id_user = utf8_encode($consulta1['id']);
 $nome_user = $consulta1['nome'];}

$menu = 5;

?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title><?php echo $nome_site; ?> - Painel de Controle</title>
	<link rel="icon" type="image/x-icon" href="images/cow.png" />
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="fonts/ionicons.min.css">
    <link rel="stylesheet" href="css/Login-Form-Dark.css">
    <link rel="stylesheet" href="css/styles.css">
	<link href="../images/favicon-blue.png" rel="shortcut icon" type="image/vnd.microsoft.icon" />
	<link rel="preconnect" href="https://fonts.googleapis.com">
	<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
	<link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet">
	<script src="//cdn.jsdelivr.net/npm/medium-editor@latest/dist/js/medium-editor.min.js"></script>
	<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/medium-editor@latest/dist/css/medium-editor.min.css" type="text/css" media="screen" charset="utf-8">
	<script type="text/javascript">
	function Mudarestado(el) {
        var display = document.getElementById(el).style.display;
        if(display == "none")
            document.getElementById(el).style.display = 'block';
        else
            document.getElementById(el).style.display = 'none';
    }
	</script>
	
	<style>
	body {
		font-family: 'Ubuntu', sans-serif;
	}
	
	.espacoInicial {
		height: 70px;
		-webkit-box-shadow: 0px 6px 5px 0px rgba(0,0,0,0.75);
		-moz-box-shadow: 0px 6px 5px 0px rgba(0,0,0,0.75);
		box-shadow: 0px 6px 5px 0px rgba(0,0,0,0.75);
	}
	
	.bgMenu {
		    position: relative;
			background-color: #64635d;
			border-radius: 4px;
			color: #fff;
			padding: 10px 60px;
			margin: 20px 0px;
	}
	
	.nav-pills .nav-link.active, .nav-pills .show>.nav-link {
		color: #64635d;
		background-color: #ffffff;
	}
	.borderPhoto {
		border: 1px solid #ccc;
		border-radius: 8px;
		padding: 20px 20px;
	}
	.imgPhoto {
		border-radius: 5px;
		width: 100%;
		height: 210px;
		margin-bottom: 10px;
	}
	#arquivo1 {
		display: none;
	}
	#arquivo2 {
		display: none;
	}
	</style>
	
</head>

<body>
    <div class="espacoInicial login-dark">
		
    </div>
	<?php
	include("menu.php");
	?>
	
	<div class="container">
        <div class="row">
			<div class="col-md-12">
						<?php
						
						$pagi = $_GET['pag'];
						if ($pagi == '')
						{$pagi = 1;}
						$sqlpag = $conecta->query("SELECT COUNT(id) FROM eventos WHERE data >= '".date("Y-m-d")."' ORDER BY data ASC");
						while ($consultapag = $sqlpag->fetch_assoc())
						{$q_reg = $consultapag['COUNT(id)'];}
						$result = 5;
						$div = ((int)($q_reg/$result));
						$rest = $q_reg % $result;
						if ($rest > 0)
						{$pag = $div + 1;}
						else
						{$pag = $div;}
						//echo $div." - ".$rest." - ".$pag."<br>";
						$pagc = 1;
						$pagination = "";
						$pagination .= "<p align='center' style='font-size: 12pt;'>[ ";
						if ($q_reg == 0)
						{$pagination .= "Sem Resultados";}
						while ($pagc <= $pag)
						{if ($pagi == $pagc)
						{$pagination .= "<strong>";}
						$pagination .= "<a href='eventos?pag=".$pagc."' style='color: #000; text-decoration: none;' target='_top'>".$pagc."</a>";
						if ($pagi == $pagc)
						{$pagination .= "</strong>";}
						if ($pagc < $pag)
						{$pagination .= " | ";}
						$pagc = $pagc + 1;}
						$pagination .= " ]</p>";
						echo $pagination;
						?>
				<button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#evento"><i class="fas fa-plus-circle"></i> Inserir Evento</button>&nbsp;&nbsp;&nbsp;
				
				<?php
				if($aviso_ins == 1) {
				echo '<br><p class="text-left text-success" style="font-size: 9pt;">Usuário inserido com sucesso!</p>';}
				if($aviso_ins == 2) {
				echo '<br><p class="text-left text-danger" style="font-size: 9pt;">Não foi possível inserir o usuário!</p>';}
				?><br><br>
				<table class="table">
					<thead>
						<tr>
							<th scope="col"><h5 class="text-center">Cadastro de Eventos</h5></th>
						</tr>
						<tr>
							<th class="text-center" style="font-size: 10pt;">Legenda: <i class="fa-solid fa-circle-info text-warning"></i> - Descrição Completa/Flyer do Evento&nbsp;&nbsp;|&nbsp;&nbsp;<i class="fa-regular fa-trash-can text-danger"></i> - Exclusão do Evento</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</div>
	<div class="container">
        <div class="row">
				
				<div class="col-md-12">
				<table class="table">
				  <thead>
					<tr>
					  <th scope="col">&nbsp;</th>
					  <th scope="col">Data</th>
					  <th scope="col">Nome do Evento</th>
					  <th scope="col">Local</th>
					  <th scope="col">&nbsp;</th>
					</tr>
				  </thead>
				  <tbody>
				<?php
					
				$ind = $pagi * $result;
				$offset = $ind - $result;
				$sql2 = $conecta->query("SELECT * FROM eventos WHERE data >= '".date("Y-m-d")."' ORDER BY data ASC LIMIT $result OFFSET $offset");
				while ($consulta2 = $sql2->fetch_assoc()) {

				?>
				
					<tr>
					  <td>
					  <div style="background-image:url('assets/eventos/<?php echo $consulta2['imagem_capa']; ?>'); background-size: cover; background-repeat: no-repeat; position: relative; width: 96px; height: 96px; border-radius: 48px;">
						&nbsp;
					  </div>
					  </td>
					  <td class="align-middle">
					  <?php
					  if($consulta2['data'] == $consulta2['data_final']){
					  echo 'Dia '.date("d/m/Y", strtotime($consulta2['data'])).' a partir das '.$consulta2['horario']; ?> hs</td>
					  <?php
					  } else {
					  echo 'De '.date("d/m/Y", strtotime($consulta2['data'])).' até '.date("d/m/Y", strtotime($consulta2['data_final'])).'<br> a partir das '.$consulta2['horario']; ?> hs</td>
					  <?php
					  }
					  ?>
					  <td class="align-middle"><?php echo utf8_encode($consulta2['titulo']); ?></td>
					  <td class="align-middle"><?php echo utf8_encode($consulta2['local']); ?></td>
					  <td class="align-middle">
					  <a data-toggle="modal" data-target="#detalhes<?php echo $consulta2['id']; ?>">
						<i class="fa-solid fa-circle-info text-warning"></i>
					  </a>&nbsp;&nbsp;
					  <a data-toggle="modal" data-target="#excluir<?php echo $consulta2['id']; ?>">
						<i class="fa-regular fa-trash-can text-danger"></i>
					  </a>&nbsp;&nbsp;
					  </td>
					
				
				
				<!-- Modal da Exclusão -->
						<div class="modal fade" id="excluir<?php echo $consulta2['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
							<div class="modal-dialog" role="document">
								<div class="modal-content">
								<div class="modal-header">
									<h5 class="modal-title" id="exampleModalLabel"><strong>Excluir Evento</strong></h5>
									<button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
										<span aria-hidden="true">&times;</span>
									</button>
								</div>
								<div class="modal-body">
									
									<p class="text-center">Tem certeza que deseja excluir o evento <?php echo utf8_encode($consulta2['titulo']); ?>?</p>

								</div>
								<div class="modal-footer">
									
									<a href="sys_alteventos.php?step=2&pag=<?php echo $pagi; ?>&id=<?php echo $consulta2['id']; ?>" class="btn btn-danger"><i class="fa-regular fa-trash-can"></i> Excluir</a>
									<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
														
								</div>
								</div>
							</div>
						</div>
						
						<!-- Modal do Vídeo -->
						<div class="modal fade" id="detalhes<?php echo $consulta2['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
							<div class="modal-dialog" role="document">
								<div class="modal-content">
								<div class="modal-header">
									<h5 class="modal-title" id="exampleModalLabel"><strong>Evento <?php echo utf8_encode($consulta2['titulo']); ?></strong></h5>
									<button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
										<span aria-hidden="true">&times;</span>
									</button>
								</div>
								<div class="modal-body">
									
									<figure class="figure">
									  <img src="assets/eventos/<?php echo $consulta2['imagem_flyer']; ?>" class="figure-img img-fluid rounded" alt="<?php echo utf8_encode($consulta2['titulo']); ?>">
									  <figcaption class="figure-caption text-justify"><?php echo utf8_encode($consulta2['descricao']); ?></figcaption>
									</figure>

								</div>
								<div class="modal-footer">
									
									<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
														
								</div>
								</div>
							</div>
						</div>
					</tr>
				<?php
				}
				?>
				</tbody>
			</table>
			</div>	
		</div>
	</div>	
						
						<!-- Modal da Inserção do Evento -->
						<div class="modal fade" id="evento" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
							<div class="modal-dialog" role="document">
								<div class="modal-content">
									<div class="modal-header">
									<h5 class="modal-title" id="exampleModalLabel"><strong>Inserir Evento</strong></h5>
									<button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
										<span aria-hidden="true">&times;</span>
									</button>
								</div>
								<div class="modal-body">
									<form action="sys_alteventos.php?step=1" method="POST" enctype="multipart/form-data">
									<div class="form-group">
										<label>Nome do Evento</label>
										<input type="text" name="titulo" class="form-control" placeholder="Digite o Nome do Evento" REQUIRED>
										<input type="hidden" name="pag" value="<?php echo $pagi; ?>">
									</div>
									
									<div class="form-group">
										<label>Descritivo do Evento</label>
										<textarea class="editable" name="descricao"><strong>Apague esse texto e insira a descrição do evento.</strong></textarea>
										<input type="hidden" name="pag" value="<?php echo $pagi; ?>">
									</div>
									
									<div class="form-group">
										<label>Data de Início</label>
										<input type="date" name="data" class="form-control" REQUIRED>
									</div>
									
									<div class="form-group">
										<label>Horário de Início</label>
										<select name="horario" class="form-control" REQUIRED>
											<option value="">-- Selecione o Horário --</option>
											<?php
											$h = 0;
											while($h < 24){
											?>
											<option value="<?php echo $h.':00'; ?>"><?php echo $h.':00'; ?></option>
											<option value="<?php echo $h.':30'; ?>"><?php echo $h.':30'; ?></option>
											<?php
											$h = $h + 1;}
											?>
										</select>
									</div>
									
									<div class="form-group">
										<button type="button" class="btn btn-warning btn-block" onclick="Mudarestado('ocultarDesk')"><i class="fa-solid fa-calendar-days"></i> Inserir Data de Término</button>
									</div>
									
									<div class="form-group" id="ocultarDesk" style="display: none;">
										<label>Data de Término</label>
										<input type="date" name="data_final" class="form-control">
									</div>
									
									<div class="form-group">
										<label>Local</label>
										<input type="text" name="local" class="form-control" placeholder="Digite o Local" REQUIRED>
									</div>
									
									<div class="form-group">
										<label>Imagem da Capa</label><br>
										<label for="arquivo1" class="btn btn-success btn-block"><i class="far fa-hand-pointer"></i> Selecionar Imagem da Capa</label>
										<input type="file" name="arquivo[]" accept="image/png, image/jpeg" id="arquivo1">
									</div>
									<div class="form-group">
										<label>Imagem do Flyer</label><br>
										<label for="arquivo2" class="btn btn-success btn-block"><i class="far fa-hand-pointer"></i> Selecionar Imagem do Flyer</label>
										<input type="file" accept="image/png, image/jpeg" name="arquivo[]" id="arquivo2">
									</div>
									
								</div>
									<div class="modal-footer">
									
									<button type="submit" class="btn btn-success"><i class="fas fa-save"></i> Salvar</button>
									</form>
									<button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="fas fa-times-circle"></i> Fechar</button>
														
								</div>
								</div>
							</div>
						</div>
						
						
	
    <script>
		var editor = new MediumEditor('.editable');
	</script>
	<script src="https://kit.fontawesome.com/6b800f9d42.js" crossorigin="anonymous"></script>
	<script src="js/jquery-3.3.1.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <script id="bs-live-reload" data-sseport="49720" data-lastchange="1588443735284" src="js/livereload.js"></script>
</body>

</html>