<?php
ob_start();
session_start();
include "connect.php";
include "check_session.php";
$nome_usuario = $_SESSION['usuario'];

if(isset($_POST['editar'])) {
	$id_pro = $_POST['idpro'];
	$pagina = $_POST['pagina'];
	$secao = $_POST['secao'];

	$ds_texto = $_POST['ds_texto'];
	$ds_texto_en = $_POST['ds_texto_en'];

	$dir = 'arquivos/';
	$dir_en = 'arquivos/en/';
	$prefix = '../';
	
	$sql = 'update sgcws_conteudo set ds_texto = ?, ds_texto_en = ?';
	$campos = array($ds_texto, $ds_texto_en);

	if (!empty($_FILES['ds_arquivo']['name'])) {
		$arquivo = $prefix . $dir . $_FILES['ds_arquivo']['name'];
		move_uploaded_file($_FILES['ds_arquivo']['tmp_name'], $arquivo);
		$ds_arquivo = $dir . $_FILES['ds_arquivo']['name'];
		$ds_img_site1 = $ds_arquivo;
		$sql .= ', ds_arquivo = ?';
		$campos[] = $ds_arquivo;
	}
	if (!empty($_FILES['ds_arquivo_en']['name'])) {
		$arquivo_en = $prefix . $dir_en . $_FILES['ds_arquivo_en']['name'];
		move_uploaded_file($_FILES['ds_arquivo_en']['tmp_name'], $arquivo_en);
		$ds_arquivo_en = $dir_en . $_FILES['ds_arquivo_en']['name'];
		$ds_img_site2 = $arquivo;
		$sql .= ', ds_arquivo_en = ?';
		$campos[] = $ds_arquivo_en;
	}
	$sql .= ' where id = ?';
	$campos[] = $id_pro;
	$stmt = mysqli_prepare($conexao, $sql) or die(mysqli_error($conexao) . '<br />' . $sql);
	$types = str_repeat('s', count($campos) - 1) . 'i';
	$params = array($stmt, $types);
	foreach($campos as &$campo) {
		$params[] = &$campo;
	}
	// chama a função que vai 'bindar' os parâmetros na query
	call_user_func_array('mysqli_stmt_bind_param', $params);
	// executa a query e monta array com resultados, onde a chave será página:seção e no valor um array contendo os valores encontrados para aquela chave, assim
	// quando houver listas não terá problema
	mysqli_stmt_execute($stmt) or die(mysqli_error($conexao));

	header("Location:conteudo_list.php?pagina=$pagina&secao=$secao");
	return;
}

$id = (int)base64_decode($_GET['id']);
$pagina = $_GET['pagina'];
$secao = $_GET['secao'];
$pagina2 = base64_decode($_GET['pagina']);	

if ($id=='') {
	$id = (int)$_POST['idpro'];
}
//Logotipo - INICIO
$consulta_logo = "select ds_arquivo from sgcws_conteudo where ds_titulo = 'logo_admin'";
$resultado = mysqli_query($conexao, $consulta_logo) or die ($erro = mysqli_error($conexao));
$registro = mysqli_fetch_assoc ($resultado);
$img = $registro['ds_arquivo'];
//Lototipo - FIM
?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Painel de Controle CODE</title>
<link rel="stylesheet" href="css/style.default.css" type="text/css" />
<link rel="stylesheet" href="prettify/prettify.css" type="text/css" />
<link rel="stylesheet" href="css/code.css" type="text/css" />

<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery-migrate-1.1.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.9.2.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery.uniform.min.js"></script>
<script type="text/javascript" src="js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="prettify/prettify.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript" src="js/custom.js"></script>

<script type="text/javascript" src="js/tinymce/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
    selector: "textarea",
    theme: "modern",
	plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime media nonbreaking save table contextmenu directionality",
        "emoticons template paste textcolor colorpicker textpattern"
    ],
    toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
    toolbar2: "print preview media | forecolor backcolor emoticons",
    image_advtab: true,
    templates: [
        {title: 'Test template 1', content: 'Test 1'},
        {title: 'Test template 2', content: 'Test 2'}
    ]
})
;
</script>

</head>

<body>

<div class="mainwrapper">
	
    <!-- START OF LEFT PANEL -->
    <div class="leftpanel">
        <?php echo "<div class='logopanel'><a href='principal.php'><img style= 'height:70px' src='$img'></a></div>"; ?>
        <div class="datewidget">Usuário: <?php echo $nome_usuario;?>   Data: <?php  echo $data = date("d/m/Y"); ?></div>
		<?php include "menu.php"; ?> 
		<!--leftmenu-->
        <?php
		$consulta = "select * from sgcws_conteudo where id=$id";
		$sql = mysqli_query($conexao, $consulta) or die(mysqli_error($conexao));
		$resultado = mysqli_fetch_assoc ($sql);

		$campo1 =    $resultado['ds_titulo'];
		$campo1_en = $resultado['ds_titulo_en'];
		$campo2 =    $resultado['ds_texto'];
		$campo2_en = $resultado['ds_texto_en'];
		$campo3 =    $resultado['ds_arquivo'];
		$campo3_en = $resultado['ds_arquivo_en'];
		$campo4 =    $resultado['ds_pagina'];
		$campo5 =    $resultado['ds_secao'];
		$st_editor = $resultado['st_editor'];
		?>
    </div><!--mainleft-->
    <!-- END OF LEFT PANEL -->
    
    <!-- START OF RIGHT PANEL -->
    <div class="rightpanel">
    	<div class="headerpanel">
        	<a href="" class="showmenu"></a>
            
            <div class="headerright">
            	<div class="dropdown notification">
                    <a class="dropdown-toggle"  href="destroy_session.php">
                    	<span class="iconsweets-exit iconsweets-white"></span>Sair
                    </a>
                    
                </div>
    		
            </div><!--headerright-->
            
    	</div><!--headerpanel-->
        <div class="breadcrumbwidget">
        	<ul class="skins">
                <!--<li><a href="default" class="skin-color default"></a></li>
                <li><a href="orange" class="skin-color orange"></a></li>
                <li><a href="dark" class="skin-color dark"></a></li>-->
                <li>&nbsp;</li>
                <li class="fixed"><a href="" class="skin-layout fixed"></a></li>
                <li class="wide"><a href="" class="skin-layout wide"></a></li>
            </ul><!--skins-->
        	<ul class="breadcrumb">
                <li><a href="principal.php">Início</a> n/</li></li>
				<li> Editar /</li>
                <li class="active">EDITAR</li>
            </ul>
        </div><!--breadcrumbs-->
        <div class="pagetitle">
        	<h1>Editar</h1> 
        </div><!--pagetitle-->
        
        <div class="maincontent">
        	<div class="contentinner">
			<form enctype="multipart/form-data" name="cadproduto" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
				<input type="hidden" value="<?php echo $id; ?>" name="idpro" id="idpro">
				<input type="hidden" value="<?php echo $pagina; ?>" name="pagina" id="pagina">
				<input type="hidden" value="<?php echo $secao; ?>" name="secao" id="secao">
				<table class='table-bordered table-form'>
					<!--
					<tr>
						<td width="15%"><h6>id</h6></td>
						<td width="40%"><input name="id" type="text" size="50" value="<?php echo $id; ?>" disabled /></td>
					</tr>
					-->
					<tr>
						<td><h6>Titulo</h6></td>
						<td><input name="ds_titulo" type="text" size="50" value="<?php echo sgcws_attr($campo1); ?>"  /></td>
					</tr>
					<tr>
						<td><h6>Title in english</h6></td>
						<td><input name="ds_titulo_en" type="text" size="50" value="<?php echo sgcws_attr($campo1_en); ?>"  /></td>
					</tr>
					<tr>
						<td><h6>Texto</h6></td>
						<?php
						if ($st_editor =='1') {
							echo "<td><textarea name='ds_texto' style='width:100%'>" . sgcws_html($campo2) . "</textarea></td>";
						} else {
							echo "<td><input name='ds_texto' type='text' size='50' value='" . sgcws_attr($campo2) . "'/></td>";
						}
						?>
					</tr>
					<tr>
						<td><h6>Text in english</h6></td>
						<?php
						if ($st_editor =='1') {
							echo "<td><textarea name='ds_texto_en' style='width:100%'>" . sgcws_html($campo2_en) . "</textarea></td>";
						} else {
							echo "<td><input name='ds_texto_en' type='text' size='50' value=\"" . sgcws_attr($campo2_en) . "\"/></td>";
						}
						?>
						
					</tr>
					<tr>
						<td><h6>Arquivo</h6></td>
						<td>
							<?php 
							if (substr($campo3, -3)=='jpg' || substr($campo3, -3)=='png' || substr($campo3, -3)=='gif') {
								$prefix = substr($campo3, 5) =='http: ' || substr($campo3, 6) == 'https:' ? '' : '../';
								echo "<h6>Imagem atual</h6><img src='$prefix$campo3' width='100px'><br>";
							}
							?>
							<input name="ds_arquivo" type="file"  accept="image/*" size="50"/>
						</td>
					</tr>
					<tr>
						<td><h6>File</h6></td>
						<td>
							<?php 
							if (substr($campo3, -3)=='jpg' || substr($campo3, -3)=='png' || substr($campo3, -3)=='gif') {
								$prefix = substr($campo3_en, 5)=='http:' || substr($campo3_en, 6)=='https:' ? '' : '../';
								echo "<h6>Imagem atual</h6><img src='$prefix$campo3_en' width='100px'><br>";
							}
							?>
							<input name="ds_arquivo_en" type="file"  accept="image/*" size="50"/>
						</td>
					</tr>
					<tr>
						<td><h6>Página</h6></td>
						<td><input name="ds_pagina" type="text" size="50" value="<?php echo sgcws_attr($campo4); ?>" disabled /></td>
					</tr>
					<tr>
						<td><h6>Seção</h6></td>
						<td><input name="ds_secao" type="text" size="50" value="<?php echo sgcws_attr($campo5); ?>" disabled /></td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>
							<input class="btn btn-default" type="button" onClick="window.history.go(-1)" value="Voltar">
							<input class="btn btn-default" type="submit" value="Salvar"  name="editar">
						</td>
					</tr>
				</table>
			</form>
			</div><!--maincontent-->
		</div><!--mainright-->
    </div><!-- END OF RIGHT PANEL -->
    
    <div class="clearfix"></div>
	<div class="footer">
		<div class="footerleft"><a href="http://www.codecrm.com.br">&copy; CODE Tecnologia</div>
		<div class="footerright">Twitter</div>
	</div><!--footer-->
</div><!--mainwrapper-->
</body>
</html>
