<?php include 'header.php'; ?>

<?php include 'sidebar.php'; ?>

<div id="page-wrapper">

    <div class="container-fluid">

		<br/>

         <div class="row">

             <div class="col-lg-12">
				<div class="panel panel-default dash">
					<div class="panel panel-primary">
						<div class="panel-heading">
							<h2 class="sub-header">
								<i class="fa fa-info-circle" aria-hidden="true"></i> <?php echo lang('HYPERMARKETS');?>
							</h2>
						</div>
						<!-- /.panel-heading -->
						<div class="panel-body">
							<div class="table-responsive">
							
								<div class="col-md-12">

									<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" class="form-horizontal" name="newservice">
										<fieldset>
											<?php $csrf->echoInputField(); ?>

											
											<!-- Text input-->
											<div class="form-group">
												<label class="col-md-2 control-label" for="name"><?php echo lang('NAME');?></label>
												<div class="col-md-6">
													<input id="name" name="name" type="text" class="form-control input-md" required>
												</div>
											</div>

											<div class="form-group">
												<label class="col-md-2 control-label" for="sub_title"><?php echo lang('SUBTITLE');?></label>
												<div class="col-md-6">
													<textarea rows ='5' cols='10' id='sub_title' name='sub_title' type='text' class='form-control input-md'></textarea>
												</div>
											</div>
	  										

											<!-- Image input-->
											<div class="form-group">
												<label class="col-md-2 control-label" for="url"><?php echo lang('UPLOAD_IMAGE');?></label>
												<div class="col-md-6">
													<input type="file" name="image" id="image">
												</div>
											</div>

											<div class="form-group">
												<label class="col-md-2 control-label" for="url"><?php echo lang('UPLOAD_LOGO');?></label>
												<div class="col-md-6">
													<input type="file" name="inner_image" id="inner_image" required="">
												</div>
											</div>

											<!-- Button -->
											<div class="form-group">
												<label class="col-md-10 control-label" for="singlebutton"></label>
												<div class="col-md-2">
													<input type="submit" name="newservice" class="btn btn-primary" value="<?php echo lang('SAVE');?>" />
												</div>
											</div>

										</fieldset>
									</form>
									<?php		

										if (!empty($_POST['newservice']))
										 {
											global $conection;

											$image = $_FILES['image'];
											$inner_image = $_FILES['inner_image'];
											$name = $_POST['name']; 
											$slug = str_replace(' ', '_', replace_accents($_POST['name']));
											
											$sub_title = mysqli_real_escape_string($conection, $_POST['sub_title']);


											if (!empty($image["name"]) || ($inner_image["name1"])) {
										 
												$largura = 1000000;
												$altura = 1000000;
												$tamanho = 500000000000;
										 
												if(!preg_match("/image\/(pjpeg|jpeg|png|gif|bmp)/", $image["type"])){
												   $error[1] = "Isso não é uma imagem.";
												} 
										 
												$dimensoes = getimagesize($image["tmp_name"]);
												$dimensoes1 = getimagesize($inner_image["tmp_name"]);
										 
												if(($dimensoes[0] > $largura) || ($dimensoes1[0] > $largura)) {
													$error[2] = "A largura da imagem não deve ultrapassar ".$largura." pixels";
												}
										 
												if($dimensoes[1] > $altura) {
													$error[3] = "Altura da imagem não deve ultrapassar ".$altura." pixels";
												}
										 
												if($image["size"] > $tamanho) {
													$error[4] = "A imagem deve ter no máximo ".$tamanho." bytes";
												}

												if($inner_image["size"] > $tamanho) {
													$error[4] = "A imagem deve ter no máximo ".$tamanho." bytes";
												}
										 
													preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $image["name"], $ext);
													preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $inner_image["name"], $ext);
										 
													$nome_imagem = $image["name"];
													$nome_imagem1 = $inner_image["name"];
										 
													$caminho_imagem = "../assets/img/uploads/partners/" . $nome_imagem;
													$caminho_imagem1 = "../assets/img/uploads/partners/" . $nome_imagem1;
										 
													move_uploaded_file($image["tmp_name"], $caminho_imagem);
													move_uploaded_file($inner_image["tmp_name"], $caminho_imagem1);

										  
													$sql = mysqli_query($conection,"INSERT INTO categories VALUES ('', '".$name."', '".$slug."', '".$sub_title."', '".$nome_imagem."', '".$nome_imagem1."')");

													// Se os dados forem inseridos com sucesso			
													if (!$sql) {
													echo ("Can't insert into database: " . mysqli_error());
													return false;
													} else {
													echo "<script type='text/javascript'>swal('".lang('NICE')."', '".lang('ADDED')."', 'success');</script>";
													echo '<meta http-equiv="refresh" content="1; categories.php">'; 
													die();
													}		
													return true;

												// Se houver mensagens de erro, exibe-as
												if (count($error) != 0) {
													foreach ($error as $erro) {
														echo $erro . "<br />";
													}
												}
											}
										}
											
										?>

								</div>    
							</div>
						</div>
					</div>
				</div>
			</div>
        </div>
        <!-- /.row -->

        <div class="copyright">
            <p>© Copyright 2018  &nbsp;&nbsp;|&nbsp;&nbsp; All Rights Reserved Powered by 
            <a href="http://komquest.com/" target="_blank">Komquest Solutions</a></p>
        </div>
    
    </div>
    <!-- /.container-fluid -->

</div>
<!-- /#page-wrapper -->

</div>
<!-- /#wrapper -->

<?php include 'footer.php'; ?>