<?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/worksmidiacom/cvprov/config/cvprov.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 = 1;

?>
<!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="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">
	
	<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;
	}
	
	#fileInput1 {
		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">
			 &nbsp;
			</div>
		</div>
		<div class="row">
			<div class="col-md-2">
			 &nbsp;
			</div>
			<div class="col-md-8">
				<ul class="nav nav-tabs" id="myTab" role="tablist">
				  <li class="nav-item">
					<a class="nav-link active" id="categorias-tab" data-toggle="tab" href="#categorias" role="tab" aria-controls="proximos" aria-selected="true">Categorias</a>
				  </li>
				  <li class="nav-item">
					<a class="nav-link" id="pratos-tab" data-toggle="tab" href="#pratos" role="tab" aria-controls="anteriores" aria-selected="false">Pratos</a>
				  </li>
				</ul>
				<div class="tab-content" id="myTabContent" style="padding-top: 30px;">
				  <div class="tab-pane fade show active" id="categorias" role="tabpanel" aria-labelledby="proximos-tab">
				  
				  <a data-toggle="modal" data-target="#inscategoria" style="cursor:pointer;"><i class="fas fa-plus text-success"></i> Inserir Categoria</a>
				  
					<table class="table">
					  <thead>
						<tr>
						  <th scope="col">Identificador</th>
						  <th scope="col">Nome da Categoria</th>
						  <th scope="col">Ordem</th>
						  <th scope="col"> </th>
						</tr>
					  </thead>
					  <tbody>
						<?php
						$sqlA = $conecta->query("SELECT * FROM categorias ORDER BY ordem DESC");
						while ($consultaA = $sqlA->fetch_assoc()){
						$menos = $consultaA['ordem'] - 1;
						$mais = $consultaA['ordem'] + 1;
						$sqlB = $conecta->query("SELECT COUNT(id) AS verif FROM categorias WHERE ordem = '".$menos."' ORDER BY ordem DESC");
						while ($consultaB = $sqlB->fetch_assoc()){
						$verMenos = $consultaB['verif'];}
						$sqlC = $conecta->query("SELECT COUNT(id) AS verif FROM categorias WHERE ordem = '".$mais."' ORDER BY ordem DESC");
						while ($consultaC = $sqlC->fetch_assoc()){
						$verMais = $consultaC['verif'];}
						?>
						<tr>
						  <td><?php echo $consultaA['identificador']; ?></td>
						  <td><?php echo $consultaA['nome']; ?></td>
						  <td><?php echo $consultaA['ordem']; ?>
						  <?php
						  if($verMais > 0){
						  ?>
						  &nbsp;&nbsp;<a class="text-dark" href="<?php echo URL_BASE.'controle/alt_ordem.php?ordem='.$consultaA['ordem'].'&eq=sobe'; ?>"><i class="fas fa-chevron-up"></i></a>
						  <?php
						  }if($verMenos > 0){
						  ?>
						  &nbsp;&nbsp;<a class="text-dark" href="<?php echo URL_BASE.'controle/alt_ordem.php?ordem='.$consultaA['ordem'].'&eq=desce'; ?>"><i class="fas fa-chevron-down"></i></a>
						  <?php
						  }
						  ?>
						  </td>
						  <td>
						  <a data-toggle="modal" data-target="#inscategoria-<?php echo $consultaA['id']; ?>" style="cursor:pointer;"><i class="fas fa-edit"></i></a>
						  &nbsp;&nbsp;
						  <a data-toggle="modal" data-target="#exccategoria-<?php echo $consultaA['id']; ?>" style="cursor:pointer;"><i class="far fa-trash-alt text-danger"></i></a>
						  </td>
						</tr>
						
						<!-- Modal Alteração de Categoria -->
						<div class="modal fade" id="inscategoria-<?php echo $consultaA['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">Alterar Categoria</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="alt_infos.php" method="POST">
									<div class="form-group">
										<label>Categoria</label>
										<input type="text" class="form-control" name="nome" value="<?php echo $consultaA['nome']; ?>">
										<input type="hidden" value="1" name="step">
										<input type="hidden" value="<?php echo $consultaA['id']; ?>" name="id">
									</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 Categoria</button>
								</form>
							  </div>
							</div>
						  </div>
						</div>
						
						<!-- Modal Exclusão de Categoria -->
						<div class="modal fade" id="exccategoria-<?php echo $consultaA['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 Categoria</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> Tem certeza que deseja excluir a categoria <strong><?php echo $consultaA['nome']; ?></strong>?
								<form action="alt_infos.php" method="POST">
								<input type="hidden" value="2" name="step">
								<input type="hidden" value="<?php echo $consultaA['ordem']; ?>" name="ordem">
								<input type="hidden" value="<?php echo $consultaA['id']; ?>" name="id">
							  </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 Categoria</button>
								</form>
							  </div>
							</div>
						  </div>
						</div
						
						<?php
						}
						?>
					  </tbody>
					</table>
				  
				  </div>
				  <div class="tab-pane fade" id="pratos" role="tabpanel" aria-labelledby="anteriores-tab">
				  
				  <a data-toggle="modal" data-target="#insprato" style="cursor:pointer;"><i class="fas fa-plus text-success"></i> Inserir Prato</a>
				  
					<table class="table">
					  <thead>
						<tr>
						  <th scope="col">Prato</th>
						  <th scope="col">Categoria</th>
						  <th scope="col">Preço</th>
						  <th scope="col"> </th>
						</tr>
					  </thead>
					  <tbody>
						<?php
						$sqlA = $conecta->query("SELECT * FROM pratos ORDER BY prato DESC");
						while ($consultaA = $sqlA->fetch_assoc()){
						$sqlB = $conecta->query("SELECT nome FROM categorias WHERE id = '".$consultaA['categoria']."'");
						while ($consultaB = $sqlB->fetch_assoc()){
						$categoriaPrato = $consultaB['nome'];}
						?>
						<tr>
						  <td><?php echo $consultaA['prato']; ?></td>
						  <td><?php echo $categoriaPrato; ?></td>
						  <td>R$ <?php echo number_format($consultaA['preco'], 2, ',', '.'); ?></td>
						  <td>
						  <a data-toggle="modal" data-target="#insprato-<?php echo $consultaA['id']; ?>" style="cursor:pointer;"><i class="fas fa-edit"></i></a>
						  &nbsp;&nbsp;
						  <a data-toggle="modal" data-target="#altimagem-<?php echo $consultaA['id']; ?>" style="cursor:pointer;"><i class="fas fa-camera-retro"></i></a>
						  &nbsp;&nbsp;
						  <a data-toggle="modal" data-target="#exccategoria-<?php echo $consultaA['id']; ?>" style="cursor:pointer;"><i class="far fa-trash-alt text-danger"></i></a>
						  </td>
						</tr>
						
						<!-- Modal Inserção de Prato -->
						<div class="modal fade" id="insprato-<?php echo $consultaA['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">Editar Prato</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="alt_infos.php" method="POST" enctype="multipart/form-data">
									<div class="form-group">
										<label>Categoria</label>
										<select class="form-control" name="categoria">
										<option value="">-- Selecione --</option>
										<?php
										$sqlM = $conecta->query("SELECT * FROM categorias ORDER BY ordem ASC");
										while ($consultaM = $sqlM->fetch_assoc()){
										?>
										<option value="<?php echo $consultaM['id']; ?>"<?php if($consultaA['categoria'] ==  $consultaM['id']){echo ' SELECTED';} ?>>
											<?php echo $consultaM['nome']; ?>
										</option>
										<?php
										}
										?>
										</select>
									</div>
									<div class="form-group">
										<label>Nome do Prato</label>
										<input type="text" class="form-control" name="prato" value="<?php echo $consultaA['prato']; ?>">
										<input type="hidden" name="id" value="<?php echo $consultaA['id']; ?>">
										<input type="hidden" value="5" name="step">
									</div>
									<div class="form-group">
										<label>Descrição do Prato</label>
										<textarea class="form-control" name="desc_prato"><?php echo $consultaA['desc_prato']; ?></textarea>
									</div>
									<div class="form-group">
										<label>Preço (R$)</label>
										<input type="text" class="form-control" name="preco" value="<?php echo number_format($consultaA['preco'], 2, ',', '.'); ?>">
									</div>
									<div class="form-group">
										<label>É Bebida?</label>
										<select class="form-control" name="bebidas">
										<option value="0"<?php if($consultaA['bebidas'] ==  0){echo ' SELECTED';} ?>>Não</option>
										<option value="1"<?php if($consultaA['bebidas'] ==  1){echo ' SELECTED';} ?>>Sim</option>
										</select>
									</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 Prato</button>
								</form>
							  </div>
							</div>
						  </div>
						</div>
						
						<!-- Modal Exclusão de Prato -->
						<div class="modal fade" id="exccategoria-<?php echo $consultaA['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 Prato</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> Tem certeza que deseja excluir a prato <strong><?php echo $consultaA['prato']; ?></strong>?
								<form action="alt_infos.php" method="POST">
								<input type="hidden" value="4" name="step">
								<input type="hidden" value="<?php echo $consultaA['id']; ?>" name="id">
							  </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 Categoria</button>
								</form>
							  </div>
							</div>
						  </div>
						</div>
						
						<!-- Modal Alteração de Imagem -->
						<div class="modal fade" id="altimagem-<?php echo $consultaA['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">Alterar Imagem</h5>
								<button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
								  <span aria-hidden="true">&times;</span>
								</button>
							  </div>
							  <div class="modal-body">
								<img src="<?php echo URL_BASE.'assets/pratos/'.$consultaA['foto']; ?>" width="100%"><br><br>
								<form action="alt_infos.php" method="POST" enctype="multipart/form-data">
								<div class="form-group">
									<label>Insira a Nova Foto</label><br>
									<input type="file" name="arquivo[]" accept="image/*" multiple>
								</div>
								<input type="hidden" value="6" name="step">
								<input type="hidden" value="<?php echo $consultaA['id']; ?>" name="id">
							  </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 Imagem</button>
								</form>
							  </div>
							</div>
						  </div>
						</div>
						
						<?php
						}
						?>
					  </tbody>
					</table>
				  
				  </div>
				</div>
			</div>
			<div class="col-md-2">
			 &nbsp;
			</div>
		</div>
	</div>
	
	<!-- Modal Inserção de Categoria -->
	<div class="modal fade" id="inscategoria" 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 Categoria</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="alt_infos.php" method="POST">
				<div class="form-group">
					<label>Categoria</label>
					<input type="text" class="form-control" name="nome">
					<input type="hidden" value="0" name="step">
				</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 Categoria</button>
			</form>
		  </div>
		</div>
	  </div>
	</div>
	
	<!-- Modal Inserção de Prato -->
	<div class="modal fade" id="insprato" 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 Prato</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="alt_infos.php" method="POST" enctype="multipart/form-data">
				<div class="form-group">
					<label>Categoria</label>
					<select class="form-control" name="categoria">
					<option value="">-- Selecione --</option>
					<?php
					$sqlM = $conecta->query("SELECT * FROM categorias ORDER BY ordem ASC");
					while ($consultaM = $sqlM->fetch_assoc()){
					?>
					<option value="<?php echo $consultaM['id']; ?>"><?php echo $consultaM['nome']; ?></option>
					<?php
					}
					?>
					</select>
				</div>
				<div class="form-group">
					<label>Nome do Prato</label>
					<input type="text" class="form-control" name="prato">
					<input type="hidden" value="3" name="step">
				</div>
				<div class="form-group">
					<label>Descrição do Prato</label>
					<textarea class="form-control" name="desc_prato"></textarea>
				</div>
				<div class="form-group">
					<label>Preço (R$)</label>
					<input type="text" class="form-control" name="preco">
				</div>
				<div class="form-group">
					<label>É Bebida?</label>
					<select class="form-control" name="bebidas">
					<option value="0">Não</option>
					<option value="1">Sim</option>
					</select>
				</div>
				<div class="form-group">
					<label>Insira a Foto</label><br>
					<input type="file" name="arquivo[]" accept="image/*" multiple>
				</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 Prato</button>
			</form>
		  </div>
		</div>
	  </div>
	</div>
	
    <script>
		var editor = new MediumEditor('.editable');
	</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>