<?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('/home/devworks/public_html/auanews/config/auanews.config.php');

$sql = $conecta->query("SELECT nome_site FROM config_site WHERE id = '1'");
while ($consulta = $sql->fetch_assoc())
{$nome_site = $consulta['nome_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 = 3;

?>
<!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="https://www.noharbar.com.br/site/visuais/assets/images/favicon.ico" />
    <link rel="stylesheet" href="/controle/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/controle/fonts/ionicons.min.css">
    <link rel="stylesheet" href="/controle/css/Login-Form-Dark.css">
    <link rel="stylesheet" href="/controle/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">
	
	<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;
	}
	
	.boxInformation {
		width: 80%;
		border: 1px solid #ccc;
		border-radius: 8px;
		padding: 12px 42px;
	}
	
	#arquivo1 {
		display: none;
	}
	
	#arquivo2 {
		display: none;
	}
	
	.skeleton {
        background-color: #ddd;
        border-radius: 4px;
        animation: pulse 1.5s infinite;
    }

    @keyframes pulse {
        0% {
            opacity: 0.6;
        }
        50% {
            opacity: 0.3;
        }
        100% {
            opacity: 0.6;
        }
    }

    .real-content {
        display: none;
    }
	
	.contentBox {
		border: 1px solid #ccc;
		border-radius: .25rem;
		padding: 30px 15px;
	}
	
	</style>
	
</head>

<body>
    <div class="espacoInicial login-dark">
		
    </div>
	<?php
	include("menu.php");
	?>
	
	<div class="container">
        <div class="row">
			<div class="col-md-12">
			 &nbsp;
			</div>
		</div>
		<div class="row">
			<div class="col-md-2">
			 &nbsp;
			</div>
			<div class="col-md-8">
				
				<p><a data-toggle="modal" data-target="#insert" style="cursor:pointer;"><i class="fas fa-plus-circle text-success"></i> Inserir</a></p>
				
			</div>
			<div class="col-md-2">
			 &nbsp;
			</div>
		</div>
						<?php
						
						$pagi = $_GET['pag'];
						if ($pagi == '')
						{$pagi = 1;}
						$sqlpag = $conecta->query("SELECT COUNT(id) FROM publicacoes ORDER BY edicao DESC");
						while ($consultapag = $sqlpag->fetch_assoc())
						{$q_reg = $consultapag['COUNT(id)'];}
						$result = 4;
						$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='/controle/publicacoes/".$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;
						?>
		<div class="row">
			<div class="col-md-12 d-flex justify-content-center align-items-center">
				<div class="skeleton" style="width: 100px; height: 25px; margin-bottom: 10px;"></div>
				<div class="real-content"><?php echo $pagination; ?><br></div>
			</div>
		</div>
		<div class="row">
			<div class="col-md-12">
			 &nbsp;
			</div>
			<?php
			$ind = $pagi * $result;
			$offset = $ind - $result;
			$sql2 = $conecta->query("SELECT * FROM publicacoes ORDER BY edicao DESC LIMIT $result OFFSET $offset");
			while ($consulta2 = $sql2->fetch_assoc()){
			?>
			<div class="col-md-3 contentBox">
				<div class="d-flex justify-content-center align-items-center">
				<div class="skeleton" style="width: 152px; height: 187px; margin-bottom: 20px;"></div>
				<div class="real-content">
					<a href="<?php echo URL_BASE; ?>controle/dwn_publi.php?id=<?php echo $consulta2['id']; ?>&pag=<?php echo $pagi; ?>">
						<?php
						// Decodifica a string base64
						$image_data = base64_decode($consulta2['miniatura']);

						$webp_header = 'RIFF';
						if (substr($image_data, 0, 4) === $webp_header) {
						?>
						<img src="data:image/webp;base64,<?php echo $consulta2['miniatura']; ?>" style="width: 152px; height: 187px; border: 1px solid #ccc; margin-bottom: 20px;">
						<?php
						} else {
						// Verifica os primeiros bytes para identificar o tipo
						$first_two_bytes = substr($image_data, 0, 2);
						$jpeg_marker = "\xFF\xD8";
						$png_marker = "\x89\x50";
						if ($first_two_bytes === $jpeg_marker) {
						?>
						<img src="data:image/jpeg;base64,<?php echo $consulta2['miniatura']; ?>" style="width: 152px; height: 187px; border: 1px solid #ccc; margin-bottom: 20px;">
						<?php
						} elseif ($first_two_bytes === $png_marker) {
						?>
						<img src="data:image/png;base64,<?php echo $consulta2['miniatura']; ?>" style="width: 152px; height: 187px; border: 1px solid #ccc; margin-bottom: 20px;">
						<?php
						} else {
							echo "O tipo da imagem não pôde ser determinado.";
						}}
						?>
						
					</a>	
				</div>
				</div>
				<div class="d-flex justify-content-center align-items-center">
				<div class="skeleton" style="width: 80%; height: 30px; margin-bottom: 10px;"></div>
				<div class="real-content">
					<h5>Edição: <?php echo date("m/Y", strtotime($consulta2['edicao'])); ?></h5>
				</div>
				</div>
				<div class="d-flex justify-content-center align-items-center">
				<div class="skeleton" style="width: 80%; height: 20px; margin-bottom: 10px;"></div>
				<div class="real-content">
					<p>Identificador: <?php echo $consulta2['identificador']; ?></p>
				</div>
				</div>
				<div class="d-flex justify-content-center align-items-center">
				<div class="skeleton" style="width: 30px; height: 20px; margin-bottom: 10px;"></div>&nbsp;&nbsp;
				<div class="skeleton" style="width: 30px; height: 20px; margin-bottom: 10px;"></div>
				<div class="real-content">
					<p>
					<a data-toggle="modal" data-target="#delete-<?php echo $consulta2['id']; ?>" style="cursor:pointer;">
					<i class="far fa-trash-alt text-danger"></i>
					&nbsp;&nbsp;
					<a href="<?php echo URL_BASE; ?>controle/dwn_publi.php?id=<?php echo $consulta2['id']; ?>&pag=<?php echo $pagi; ?>">
					<i class="fas fa-cloud-download-alt text-success"></i>
					</a>
					</p>
				</div>
				</div>
			</div>
			
			<!-- Modal Exclusão de Edição -->
			<div class="modal fade" id="delete-<?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">Excluir Edição</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="<?php echo URL_BASE; ?>controle/alt_publicacoes.php" enctype="multipart/form-data" method="POST">
						<p>Deseja realmente apagar a edição identificada com <strong><?php echo $consulta2['identificador']; ?></strong> de <strong><?php echo date("m/Y", strtotime($consulta2['edicao'])); ?></strong>?</p>
						<input type="hidden" value="1" name="step">
						<input type="hidden" value="<?php echo $consulta2['identificador']; ?>" name="identificador">
						<input type="hidden" value="<?php echo $pagi; ?>" name="pag">
				  </div>
				  <div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
					<button type="submit" class="btn btn-danger"><i class="far fa-times-circle"></i> Excluir Edição</button>
					</form>
				  </div>
				</div>
			  </div>
			</div>
			
			<?php
			}
			?>
		</div>
		</div>
	</div>
	
	<!-- Modal Inserção de Edição -->
	<div class="modal fade" id="insert" 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">Inserir Edição</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="<?php echo URL_BASE; ?>controle/alt_publicacoes.php" enctype="multipart/form-data" method="POST">
				<div class="form-group">
					<label>Miniatura</label>
					<label for="arquivo1" class="btn btn-primary btn-block"><i class="far fa-image"></i> Selecionar Miniatura</label>
					<input type="file" name="arquivo[]" accept="image/png, image/jpeg, image/webp" id="arquivo1" REQUIRED>
				</div>
				<div class="form-group">
					<label>Edição Completa (PDF)</label>
					<label for="arquivo2" class="btn btn-danger btn-block"><i class="far fa-file-pdf"></i> Selecionar Edição</label>
					<input type="file" name="arquivo[]" accept="application/pdf" id="arquivo2" REQUIRED>
				</div>
				<div class="form-group">
					<label>Data da Edição</label>
					<input type="date" class="form-control" name="edicao" REQUIRED>
					<input type="hidden" value="0" name="step">
					<input type="hidden" value="<?php echo mt_rand(10000, 99999).'-'.mt_rand(10000, 99999); ?>" name="identificador">
					<input type="hidden" value="<?php echo $pagi; ?>" name="pag">
				</div>
		  </div>
		  <div class="modal-footer">
			<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
			<button type="submit" class="btn btn-success"><i class="far fa-save"></i> Salvar Edição</button>
			</form>
		  </div>
		</div>
	  </div>
	</div>
	
    <script>
		var editor = new MediumEditor('.editable');
	</script>
	
	<script>
        setTimeout(() => {
			document.querySelectorAll('.skeleton').forEach(element => {
				element.style.display = 'none';
			});
			document.querySelectorAll('.real-content').forEach(element => {
				element.style.display = 'block';
			});
		}, 2000);

    </script>
	
	<script src="https://kit.fontawesome.com/4e2ec11868.js"></script>
	<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>

</html>