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

//table name
$table_name = "adds";

//current file var
$base_file = basename($_SERVER["SCRIPT_NAME"]);
							 
//create if not
$db->exec("CREATE TABLE IF NOT EXISTS {$table_name}(id INTEGER PRIMARY KEY	AUTOINCREMENT  NOT NULL, title VARCHAR(100), url TEXT)");

//table call
$res = $db->query("SELECT * FROM {$table_name}");
																										
//update call
@$resU = $db->query("SELECT * FROM {$table_name} WHERE id='{$_GET['update']}'");
@$rowU=$resU->fetchArray();
if(isset($_POST['submitU'])){
	$db->exec("UPDATE {$table_name} SET title='{$_POST['title']}',path='{$_POST['path']}' WHERE  id='{$_POST['id']}'");
	$db->close();
	header("Location: {$base_file}");
}

//submit new
if (isset($_POST['submit'])){
	$db->exec("INSERT INTO {$table_name}(title, path) VALUES('{$_POST['title']}', '{$_POST['path']}')");
	header("Location: {$base_file}");
	}

//delete row
if(isset($_GET['delete'])){
	$db->exec("DELETE FROM {$table_name} WHERE id={$_GET['delete']}");
	header("Location: {$base_file}");
}

?>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content" style="background-color: black;">
			<div class="modal-header">
				<h2 style="color: white;">Confirmar</h2>
			</div>
			<div class="modal-body" style="color: white;">
				Quer realmente apagar?
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" data-dismiss="modal">Confirmar</button>
				<a style="color: white;" class="btn btn-danger btn-ok">Apagar</a>
			</div>
		</div>
	</div>
</div>
<?php
if (isset($_GET['create'])){

//create form
?>    
<div class="container text-center text-dark mt-5">
  <div class="row">
    <div class="col-lg-4 d-block mx-auto mt-5">
      <div class="row">
        <div class="col-xl-12 col-md-12 col-md-12">
          <div class="card">
            <div class="card-body wow-bg" id="formBg">
              <h2 class="colorboard">Ad Image's</h2>                      
              <form method="post">
                <div class="form-group">
                   <label class="form-label " for="title">Title</label> 
                      <input class="form-control" id="description" name="title" placeholder="Title" type="text"/>
                      </div>
                      <div class="form-group">
                         <label class="form-label " for="Ad Image">Ad Image</label>
                            <input class="form-control" id="description" name="path" placeholder="Ad Image" type="text"/>
                      </div>
                      <div class="form-group">
                         <center><button class="btn btn-info " name="submit" type="submit">
                             <i class="icon icon-check"></i>Salvar</button></center>
                      </div>
                 </form>
             </div>
        </div>
    </div>
</div>;
<?php 
}else if (isset($_GET['update'])){ 

//update form
?>
<div class="container text-center text-dark mt-5">
  <div class="row">
    <div class="col-lg-4 d-block mx-auto mt-5">
      <div class="row">
        <div class="col-xl-12 col-md-12 col-md-12">
          <div class="card">
            <div class="card-body wow-bg" id="formBg">
              <center><h2 class="colorboard">Editar imagens do anúncio</h2></center>
                 <form method="post">
                 <div class="form-group">                                    
                    <label class="form-label " for="title">Título</label>
                        <input class="form-control" id="description" name="title" placeholder="Title" value="<?=$rowU['title'] ?>" type="text"/>
                        </div>
                 <div class="form-group">
                    <label class="form-label " for="Ad Image">Imagem do anúncio</label>
                        <input class="form-control" id="description" name="path" placeholder="Ad Image" value="<?=$rowU['path']; ?>" type="text"/>
                        </div>
                     <div class="form-group">
                         <center><button class="btn btn-info " name="submitU" type="submit">Salvar</button></center>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>;
<?php
 }else{
//main table/form
?>        
	 <div class="col-md-12 mx-auto">
		<center>
			<h2 class="colorboard"></i> Imagens do anúncio</h2>
			<a id="button" href="./<?php echo $base_file ?>?create" class="btn btn-primary">Nova imagem do anúncio</a>
		</center>
		<br>
		<div class="table-responsive">
		    <table class="table table-striped table-sm">
		    <thead style="color:white!important">
		        <tr>
		        <th>Index</th>
		        <th>Título</th>
		        <th>Imagem do anúncio</th>
		        <th>&nbsp&nbsp&nbspExcluir</th>
		        </tr>
		    </thead>
		    <?php while ($row = $res->fetchArray()) {?>
		    <tbody>
		       <tr>
		       <td><?=$row['id'] ?></td>
		       <td><?=$row['title'] ?></a></td>
		       <td><img src="<?=$row['path'] ?>" width="75px" height="146px" alt="Havic2u" >
		      </td>
		       <td>
		       <a class="" href="<?php echo $base_file ?>?update=<?=$row['id'] ?>"><i class=""></i></a>
		       &nbsp&nbsp&nbsp
		       <a class="btn btn-danger btn-ok" href="#" data-href="<?php echo $base_file ?>?delete=<?=$row['id'] ?>" data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash-o"></i></a>
		       </td>
		       </tr>
		  </tbody>
		  <?php }?>
			</table>
		</div>
	</div>
<?php }?>

<?php include ('includes/footer.php');?>
</body>
</html>
