<?php
defined("BASEPATH") or exit("No direct access allowed");
?>
<div class="card-header">
					
          <nav aria-label="breadcrumb">
                      <ol class="breadcrumb bg-warning text-white-all">
                        <li class="breadcrumb-item"><a href="#"><i class="fas fa-tachometer-alt"></i> Home</a></li>
                        <li class="breadcrumb-item"><a href="index.php?page=tempe"><i class="far fa-file"></i> Template Email</a></li>
                        <li class="breadcrumb-item active" aria-current="page"><i class="fas fa-list"></i> Edit</li>
                      </ol>
                    </nav>

    
 <?php 

if (isset($_POST['ubah'])){
          
            $id = $_POST['id'] ;
            $p1 = mysqli_real_escape_string($con,$_POST['p1']);
            
            //$query = mysqli_query($con,"SELECT * FROM template WHERE id=$id");
	        //$check = mysqli_fetch_array($query,MYSQLI_NUM);
	        //if (isset($check[0])) {
			//	$link =$check[2];
			//	unlink($link);
	        //}    
	        
            $namafolder="template/upload/"; 
            
            $ekstensi_diperbolehkan	= array('html');
            $logox = $_FILES['filegbr']['name'];
            $x = explode('.', $logox);
            $ekstensi = strtolower(end($x));
            
            if (in_array($ekstensi, $ekstensi_diperbolehkan) === false){
              
             echo "<script>alert('Upload Gagal !!! Typle File template email harus html)'); window.location = 'index.php?page=tempe'</script>"; 
                exit;
            }
            
            $file = $namafolder . basename($_FILES['filegbr']['name']);
            
            if (move_uploaded_file($_FILES['filegbr']['tmp_name'], $file)) {
            
                //simpan
                 mysqli_query($con,"update template set namatemp='$p1', linktemp='$file' where id='$id'"); 
                
                echo '
                <script>
                	swal("Good Job","Anda sukses menyimpan template","success");
                </script>
                <script language="JavaScript">document.location="index.php?page=tempe"</script>
        		';
            } else {
                echo '
                <script>
                	swal("Good Error","Template gagal diupload","error");
                </script>
        		';
            }
}

?>
   
 <?php 
    
      $tampil=mysqli_fetch_row(mysqli_query($con,"select * from template where id='".$_GET['id']."'"));
      
      

   ?>   

<div class="card-body">            

    <div class="container">
      <form action="" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="id" value="<?php echo $tampil['id'];?>">
      <div class="container">
      <form action="" method="POST">
        <div class="form-group">  
          <p><label>Nama Template</label><input type="text" name="p1" class="form-control" value="<?php echo $tampil[1]; ?>" required/></p>
        </div> 
        <div class="form-group">  
          <p><label>File Template</label>
          <input type='file' name='filegbr' id='Filegambar' required/></p>
        </div>
        
        <div class="form-group">
          <div class  ="col-sm-4">
            <button type="submit" class="btn btn-primary" name="ubah"><span class="glyphicon glyphicon-pencil"></span> Ubah Upload Template</button>
          </div>
        </div>
      </form>
    </div>
</div></div>
    