<?php
    require_once("../_ayarlar.php");
    require_once("../_fonksiyonlar.php");
?>
<!DOCTYPE html>
<html lang="tr">

<head>
    <?php include("_head.php") ?>
    <script src="../ckeditor5/build/ckeditor.js"></script>
    <title>Haber Güncelle</title>
</head>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <?php include("_sidebar.php") ?>

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <?php include("_topbar.php") ?>

                <!-- Sayfaya Özel İçerik -->
                <div class="container-fluid">

                    <div class="card shadow mb-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary">Haber Güncelle</h6>
                        </div>
                        <div class="card-body">
                        <?php
                        $id = @$_GET["id"];

                        if (isset($_POST["haber_guncelle"])) {
                                    
                            $yeni_baslik = addslashes($_POST["baslik"]);
                            $yeni_icerik = addslashes($_POST["icerik"]);
                            $yeni_kategori = addslashes($_POST["kategori"]);
                            
                            if (@$_FILES["kapak"]["tmp_name"] != null) {
                
                                $gecici_adres = $_FILES["kapak"]["tmp_name"];
                                $kapak_ad = $_FILES["kapak"]["name"];
                                $uzanti = pathinfo($kapak_ad, PATHINFO_EXTENSION);
                                $kapak_ad = mb_strtolower($yeni_baslik);
                                $bulunacak = array('ç', 'Ç', 'ı', 'İ', 'ğ', 'Ğ', 'ü', 'ö', 'Ş', 'ş', 'Ö', 'Ü', ',', ' ', '(', ')', '[', ']');
                                $degistir = array('c', 'C', 'i', 'I', 'g', 'G', 'u', 'o', 'S', 's', 'O', 'U', '', '-', '', '', '', '');
                                $kapak_ad = str_replace($bulunacak, $degistir, $kapak_ad);
                                $kapak_ad = preg_replace("/[^a-z0-9\_\-\.]/i", '', $kapak_ad);
                                $zaman_damgasi = date("YmdHis");
                                $kapak_ad = "$zaman_damgasi-$kapak_ad.$uzanti";
                                $yeni_adres = "../kapak/$kapak_ad";
                    
                                if ($uzanti == "png" or $uzanti == "jpg") {
                                    
                                    $yukle = move_uploaded_file($gecici_adres, $yeni_adres);
                    
                                    if ($yukle) {
                                        $sql = "UPDATE haber SET kapak	= '$kapak_ad' WHERE id = '$id' ";
                                        $guncelle2 = $vt->query($sql);
                    
                                        if ($guncelle2 === TRUE) echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
                                                                        Resim güncelleme işlemi başarılı.
                                                                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                                                    </div>';
                                        else echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                                                        Resim güncelleme işleminde hata meydana geldi.
                                                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                                    </div>';
                                    } else {
                                        echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                                                Resim güncelleme işleminde hata meydana geldi.
                                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                            </div>';
                                    }
                                } else {
                                    echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                                        Resmi istenilen uzantıda değildir.<br>Lütfen tekrar deneyiniz.
                                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                    </div>';
                                }
                            }

                            $sql = "UPDATE haber SET 
                                baslik	= '$yeni_baslik',
                                icerik	= '$yeni_icerik',
                                kategori = '$yeni_kategori'
                            WHERE id = '$id' ";

                            $sonuc = $vt->query($sql);

                            if($sonuc===TRUE){
                                echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
                                Haber güncelleme işlemi başarılı.
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>';
                            }
                            else {
                                echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                                Haber güncelleme işleminde hata meydana geldi.
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>';
                            }
                        }
                        ?>
                        <?php
                            $haber_sql = "SELECT * FROM haber WHERE id = '$id' ";
                            $haber_tablo = $vt->query($haber_sql);
                            $haber_satir = $haber_tablo->fetch_array();
                            $haber_resim = $haber_satir["kapak"];
                        ?>
                            <form method="post" enctype="multipart/form-data">
                                <div class="mb-3">
                                    <label for="baslik" class="form-label">Başlık</label>
                                    <input type="text" class="form-control" id="baslik" name="baslik" value="<?php echo haber($id, "baslik"); ?>">
                                </div>
                                <div class="mb-3">
                                    <label for="icerik" class="form-label">Haber Metni</label>
                                    <textarea class="form-control" id="icerik" name="icerik"><?php echo haber($id, "icerik"); ?></textarea>
                                </div>
                                <div class="mb-3">
                                    <label for="kategori" class="form-label">Kategori</label>
                                    <select class="form-control" id="kategori" name="kategori">
                                        <?php kategori_listele(haber($id, "kategori")); ?>
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <img height="100px" src="../kapak/<?php echo haber($id, "kapak"); ?>" /><br>
                                    <label for="kapak" class="form-label">Kapak Görseli <small><< Boş bırakırsanız haber görseli aynı kalacaktır. >></small></label>
                                    <input type="file" class="form-control" id="kapak" name="kapak">
                                </div>
                                <div class="form-group">
                                    <button class="btn btn-primary btn-user btn-block" name="haber_guncelle">Haberi Güncelle</button>
                                </div>
                            </form>

                        </div>
                    </div>

                </div>
                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

            <?php include("_footer.php") ?>

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <?php include("_script.php") ?>
    <script>
        ClassicEditor
            .create( document.querySelector( '#icerik' ) )
            .catch( error => {
                console.error( error );
            } );
    </script>
</body>
</body>

</html>