<?php
require '../../conexao/conexao.php';
//require '../sessao.php';
require '../../configuacoes.php';
require '../../conexao/acesso.php';

// Definir o título da página
$pageTitle = isset($_GET['id']) && $_GET['id'] == '0' ? "Incluir Módulo" : "Alterar Módulo";

// Variáveis
$recordset = null;

// Verificando se é alteração
if (isset($_GET['id']) && $_GET['id'] != '0') {
	$id_modulo = $_GET['id'];

	$sql = "
		SELECT 
			m.id_modulo,
			m.modulo,
			m.caminho,
			m.descricao
		FROM modulo m
		WHERE m.id_modulo = ?
	";

	$stmt = mysqli_prepare($connectionmysql, $sql);
	if ($stmt) {
		mysqli_stmt_bind_param($stmt, "i", $id_modulo);
		mysqli_stmt_execute($stmt);
		$result = mysqli_stmt_get_result($stmt);
		if ($result && mysqli_num_rows($result) > 0) {
			$recordset = mysqli_fetch_assoc($result);
		}
	}
}
?>

<!DOCTYPE html>
<html lang="pt-br">

<?php include('../header.php'); ?>

<body>
	<?php include('../menu.php'); ?>

	<div class="wrapper">
		<div class="container-fluid">
			<div class="card-box mt-3">
				<div class="row">
					<div class="col-6 text-left">
						<h4 class="page-title"><?php echo $pageTitle ?></h4>
					</div>
					<div class="col-6 text-right">
						<a href="javascript:void(0);" class="btn btn-light" onclick="goBack()">
							<i class="mdi mdi-keyboard-backspace"></i> Voltar
						</a>
					</div>
					<div class="col-12"><hr></div>

					<div class="col-12">
						<form method="post" action="processa_cadastro_alteracao.php">
							<div class="form-row mb-3">
								<div class="form-group col-md-6">
									<label for="modulo" class="col-form-label">Nome do Módulo</label>
									<input type="text" name="modulo" id="modulo" class="form-control" placeholder="Ex.: Gestão de Usuários" value="<?php echo $recordset ? htmlspecialchars($recordset['modulo'], ENT_QUOTES, 'UTF-8') : ''; ?>">
								</div>
								<div class="form-group col-md-6">
									<label for="caminho" class="col-form-label">Caminho</label>
									<input type="text" name="caminho" id="caminho" class="form-control" placeholder="/mandareceita/pages/modulo" value="<?php echo $recordset ? htmlspecialchars($recordset['caminho'], ENT_QUOTES, 'UTF-8') : ''; ?>">
								</div>

								<div class="form-group col-md-12">
									<label for="descricao" class="col-form-label">Descrição</label>
									<textarea name="descricao" id="descricao" class="form-control" rows="3" placeholder="Breve descrição do módulo..."><?php echo $recordset ? htmlspecialchars($recordset['descricao'], ENT_QUOTES, 'UTF-8') : ''; ?></textarea>
								</div>

								<input type="hidden" name="id_modulo" value="<?php echo $recordset ? htmlspecialchars($recordset['id_modulo'], ENT_QUOTES, 'UTF-8') : '0'; ?>">

								<div class="col-12 text-right">
									<button type="submit" class="btn btn-primary"><i class="mdi mdi-magnify"></i> Salvar</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>

	<?php include('../footer.php'); ?>
	<?php include('../mensagens_modais.php'); ?>

	<script src="../../../assets/js/vendor.min.js"></script>
	<script src="../../../assets/js/app.min.js"></script>
	<script>
	function goBack() {
		window.history.back();
	}
	</script>
</body>

</html>


