<div class="content-wrapper">
    <div class="content-header bg-link bg-light">
        <div class="container-fluid">
            <div class="row mb-2" mb-2>
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark">Artikel</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item active"><a class="text-dark" href="#">Home / </a></li>
                        <li class="breadcrumb-iten active"><a class="text-dark" href="#"> Artikel / </a></li>
                        <li class="breadcrumb-iten active"><a class="text-dark" href="#"> Update</a></li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <?php

    $id = $_GET['idedit'];
    $dataedit = $koneksi->query("SELECT * FROM tbl_berita WHERE berita_id ='$id' ")->fetch_assoc();
    $datakategori = $koneksi->query("SELECT * FROM tbl_kategori");
    // var_dump($ttl);

    ?>

    <div class="card ml-3 mr-3">
        <di class="card-header bg-success">
            <h1>Update Data Artikel</h1>
        </di>
        <div class="card-body">

            <form action="" method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <label>Nama Berita</label>
                    <input type="text" name="nama" class="form-control" value="<?= $dataedit['berita_nama']; ?>">
                </div>
                <div class="form-group">
                    <label>Kategori Berita</label>
                    <select name="kat" class="form-control">
                        <option value="">Silahkan Pilih</option>


                        <?php foreach ($datakategori as $pecah) : ?>
                            <option <?php echo $dataedit['kategori_id'] == $pecah['kategori_id'] ? 'selected' : '' ?> value="<?= $pecah['kategori_id'] ?>"><?= $pecah['kategori_nama'] ?></option>
                        <?php endforeach; ?>

                    </select>
                </div>
                <div class="form-group">
                    <label>Keterangan Berita</label>
                    <textarea id="ket" name="ket" cols="30" rows="5" class="form-control"><?= $dataedit['berita_ket']; ?></textarea>
                </div>
                <div class="form-group">
                    <label>Tanggal Berita</label>
                    <input type="date" name="tgl" class="form-control" value="<?= $dataedit['berita_tgl']; ?>">
                </div>
                <div class="form-group">
                    <img class="mx-auto d-block m-2 putar" style="width:200px;height:200px;border-radius:50%" src="dist/img/imgberita/<?= $dataedit['berita_foto']; ?>" alt="">
                    <input readonly type="text" class="form-control" name="fotolama" value="<?= $dataedit['berita_foto'] ?>">
                </div>

                <div class="fixed">
                    <div class="form-group">
                        <label>Foto Berita</label>
                        <input type="file" name="foto" class="form-control">
                    </div>

                    <button type="submit" name="edit" class="btn btn-warning btn-block">Simpan</button>
                </div>

            </form>


            <?php

            if (isset($_POST['edit'])) {
                $nama               =  $_POST['nama'];
                $kategori_berita    =  $_POST['kat'];
                $ket                =  $_POST['ket'];
                $tgl                =  $_POST['tgl'];

                $fotolama           =  $_POST['fotolama'];
                $namafoto           =  $_FILES['foto']['name'];
                $lokasifoto         =  $_FILES['foto']['tmp_name'];


                if (!empty($lokasifoto)) {
                    unlink('dist/img/imgberita/' . $fotolama);
                    move_uploaded_file($lokasifoto, "dist/img/imgberita/" . $namafoto);


                    $edit =  $koneksi->query("UPDATE tbl_berita SET kategori_id ='$kategori_berita',
                berita_nama     ='$nama',
                berita_ket      = '$ket',
                berita_tgl      = '$tgl',
                berita_foto     = '$namafoto' WHERE berita_id ='$id'");
                } else {
                    $edit =  $koneksi->query("UPDATE tbl_berita SET kategori_id ='$kategori_berita',
                berita_nama   ='$nama',
                berita_ket    = '$ket',
                berita_tgl    = '$tgl' WHERE berita_id ='$id'");
                }

                if ($edit == TRUE) {
                    echo "<script>
                        alert ('Data Berhasil Di Simpan');
                        window.location= '?page=pages/berita/view'
                        </script>";
                } else {
                    echo "<script>
                        alert ('Data Gagal Di Simpan');
                        window.location= '?page=pages/berita/edit'
                    </script>";
                }
            }



            ?>







        </div>
    </div>
</div>