<?php
           require_once 'common/database.php';
           if(!isset($_SESSION['logged_in'])){
               header('location: index.php');
           }
    
               $id = $_POST['id'];
               $nama = $_POST['nama'];
               $deskripsi = $_POST['deskripsi'];  
               $tanggal = $_POST['tanggal'];
               $harga = $_POST['harga'];
               $kuota = $_POST['kuota'];
               
           
            if(isset($_POST['edit_photo'])){
                $gambar = $_FILES['gambar']['name'];
                $tmp = $_FILES['gambar']['tmp_name'];

                $gambar_baru = date('dmYHis').$gambar;
                $path  = "uploads/".$gambar;

                if(move_uploaded_file($tmp,$path)){
                    $datapaket = getOnePaket($_POST['id']);
                    $hasil = mysqli_fetch_array($datapaket,MYSQLI_ASSOC);
                    if(is_file("uploads/".$hasil['gambar']))
                        unlink("uploads/".$hasil['gambar']);
                        $do = EditPaket($id,$nama,$deskripsi,$tanggal,$gambar,$harga,$kuota);
                       
                        if($do){
                            echo "<script>alert('Paket $nama telah diubah');
                                    window.location = 'paket.php'</script>";
                        }
                        else{
                            echo "<script>alert('Paket $nama gagal diubah');
                            window.location = 'paket.php#edit_.'.$id'</script>";
                        }
                }
                else{
                    echo "Maaf, gambar gagal diupload.";
                    redirect('paket.php#edit_'.$id);
                }
            }
            else{
                $query = "UPDATE paket SET nama_paket='$nama',deskripsi='$deskripsi',
                tanggal='$tanggal',harga='$harga',kuota='$kuota' 
                WHERE id_paket='$id'";
                if(ExecuteQuery($query)){
                    echo "<script>alert('Paket $nama telah diubah');
                         window.location = 'paket.php'</script>";
                }
                else {
                    echo "<script>alert('Paket $nama gagal diubah');
                    window.location = 'paket.php#edit_.'.$id'</script>";
                }
            }



?>