<?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> Upcoming Programs
							</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="newprogram">
										<fieldset>
											<?php $csrf->echoInputField(); ?>

											
											<!-- Team Title input-->
											<div class="form-group">
												<label class="col-md-2 control-label" for="name">Title</label>
												<div class="col-md-6">
													<input id="title" name="title" type="text" class="form-control input-md" required>
												</div>
											</div> 

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

											<!-- Image designation input-->
											<div class="form-group">
												<label class="col-md-2 control-label" for="name"> Description </label>
												<div class="col-md-6">
													<input id="descript" name="descript" type="text" class="form-control input-md" required>
												</div>
											</div>

											<!-- Text input-->
											<div class='form-group'>
											  <label class='col-md-2 control-label' for='status'> Featured </label>  
											  <div class='col-md-6'>
											  	<select class='form-control' id='featured' name='featured' required>										  
													<option value='no'>No</option>
													<option value='yes'>Yes</option>
												</select>
											  </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="newprogram" class="btn btn-primary" value="<?php echo lang('SAVE');?>" />
												</div>
											</div>

										</fieldset>
									</form>
									<?php		

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

											$title = $_POST['title'];   
											$image = $_FILES['image']; 
											$descript = $_POST['descript']; 
											$featured = $_POST['featured']; 

											if (!empty($image["name"])) {
										 
												$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"]); 
										 
												if(($dimensoes[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";
												} 
													preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $image["name"], $ext); 

													$nome_imagem = $image["name"];

													$caminho_imagem = "assets/img/uploads/programs/" . $nome_imagem; 
										 
													move_uploaded_file($image["tmp_name"], $caminho_imagem);

												    $sql = mysqli_query($conection,"INSERT INTO programs VALUES ('', '".$title."', '".$nome_imagem."',  '".$descript."', '".$featured."')");

													// 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; programs.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 -->

    <?php include 'copyright.php'; ?>
    
    </div>
    <!-- /.container-fluid -->

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

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

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