<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">Berita</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a class="text-dark" href="?page=home">Home</a></li>
                        <li class="breadcrumb-item active"><a class="text-dark" href="?page=pages/berita/view">Berita</a>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
    </div>



    <div class="card ml-3 mr-3">
        <div class="card-header bg-success">
            <h1>Tabel Berita</h1>
        </div>

        <div class="card-body">
            <div class="table-responsive">

                <button data-toggle="modal" title=" Tambah Data" data-target="#tambah" class="btn btn-success mb-3"><i class="fa fa-plus"></i></button>

                <table class="table table-bordered" id='example1' style="text-align: center;">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Nama Berita</th>
                            <th>Kategori Berita</th>
                            <th>Keterangan Berita</th>
                            <th>Tanggal</th>
                            <th>Foto</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>



                        <?php

                        $databerita = $koneksi->query("SELECT * FROM tbl_berita INNER JOIN tbl_kategori ON tbl_berita.kategori_id = tbl_kategori.kategori_id");
                        // var_dump($databerita);
                        // exit;

                        foreach ($databerita as $no => $pecah) :

                        ?>
                            <tr>
                                <td><?= ++$no ?></td>
                                <td><?= $pecah['berita_nama'] ?></td>
                                <td><?= $pecah['kategori_nama'] ?></td>
                                <td><?= $pecah['berita_ket'] ?></td>
                                <td><?= $pecah['berita_tgl'] ?></td>
                                <td>
                                    <img width="100" height="100" src="dist/img/imgberita/<?= $pecah['berita_foto']; ?>" alt=>
                                </td>
                                <td>
                                    <a href="?page=pages/berita/edit&idedit=<?= $pecah['berita_id'] ?>" data-toggle="tooltip" title="Edit Berita" class="btn btn-warning mb-3"><i class="fa fa-edit"></i></a><br>
                                    <a onclick="return confirm('Apa Anda Yakin Ingin Menghapus ?')" href="?page=pages/berita/hapus&idhapus=<?= $pecah['berita_id'] ?>" data-toggle="tooltip" title="Hapus Berita" class="btn btn-danger"><i class="fa fa-trash"></i></a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>

                </table>
            </div>
        </div>
    </div>
</div>
<style>
    .page-item.active .page-link {
        background-color: green;
        border-color: green;
    }
</style>


<!-- Modal -->
<div class="modal fade" id="tambah" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-success">
                <h5 class="modal-title" id="exampleModalLabel">Tambah Data</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-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">
                    </div>
                    <div class="form-group">
                        <label for="">Kategori Berita</label>
                        <select name="kat" class="form-control">
                            <option value="">Silahkan Pilih</option>

                            <?php

                            $datakategori = $koneksi->query("SELECT * FROM tbl_kategori");

                            foreach ($datakategori as $pecah) :

                            ?>
                                <option value="<?= $pecah['kategori_id'] ?>"><?= $pecah['kategori_nama'] ?></option>
                            <?php endforeach; ?>

                        </select>
                    </div>
                    <div class="form-group">
                        <label>Keterangan Berita</label>
                        <textarea name="ket" id="ket" cols="30" rows="5" class="form-control"></textarea>
                    </div>
                    <div class="form-group">
                        <label>Tanggal</label>
                        <input type="date" name="tgl" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Foto Berita</label>
                        <input type="file" name="foto" class="form-control">
                    </div>

            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary" name="simpan">Simpan</button>
            </div>

            </form>
            <?php
            if (isset($_POST['simpan'])) {
                $nama               =  $_POST['nama'];
                $kategori_berita    =  $_POST['kat'];
                $ket                =  $_POST['ket'];
                $tgl                =  $_POST['tgl'];

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

                if ($namafoto !== "") {
                    $namafoto = uniqid() . '-' . $namafoto;
                } else {
                    echo "<script>
                            alert('Masukan Foto Terlebih Dahulu');
                            window.location= '?page=pages/berita/view';
                        </script>";
                    die;
                }

                move_uploaded_file($lokasifoto, "dist/img/imgberita/" . $namafoto);

                $simpan = $koneksi->query("INSERT INTO tbl_berita (kategori_id,berita_nama,
                berita_ket,
                berita_tgl,
                berita_foto) VALUES('$kategori_berita','$nama','$ket','$tgl','$namafoto')");



                if ($simpan == 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/view'
                                </script>";
                }
            }
            ?>

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