<?php
$datetime = new DateTime( "now", new DateTimeZone( "Asia/Jakarta" ) );
session_start();
require("../../mainconfig.php");
$msg_type = "nothing";

if (isset($_SESSION['user'])) {
	$sess_username = $_SESSION['user']['username'];
	$check_user = mysqli_query($db, "SELECT * FROM users WHERE username = '$sess_username'");
	$data_user = mysqli_fetch_assoc($check_user);
	if (mysqli_num_rows($check_user) == 0) {
		header("Location: ".$cfg_baseurl."logout.php");
	} else if ($data_user['status'] == "Suspended") {
		header("Location: ".$cfg_baseurl."logout.php");
	} else if ($data_user['level'] != "Developers") {
		header("Location: ".$cfg_baseurl);
	} else {
		if (isset($_POST['add'])) {
			$post_content = $_POST['content'];

			if (empty($post_content)) {
				$msg_type = "error";
				$msg_content = "<b>Gagal:</b> Mohon mengisi semua input.";
			} else {
			    $date = $datetime->format( 'Y-m-d H:i:s' );
				$insert_news = mysqli_query($db, "INSERT INTO news (date, content) VALUES ('$date', '$post_content')");
				if ($insert_news == TRUE) {
					$msg_type = "success";
					$msg_content = "<b>Berhasil:</b> Berita berhasil ditambahkan.<br /><b>Konten:</b> $post_content<br /><b>Tanggal:</b> $date";
				} else {
					$msg_type = "error";
					$msg_content = "<b>Gagal:</b> Error system.";
				}
			}
		} else if (isset($_POST['edit'])) {
		            $post_id = $_POST['id'];
					$post_content = $_POST['content'];
					if (empty($post_content)) {
						$msg_type = "error";
						$msg_content = "<b>Gagal:</b> Mohon mengisi semua input.";
					} else {
						$update_news = mysqli_query($db, "UPDATE news SET content = '$post_content' WHERE id = '$post_id'");
						if ($update_news == TRUE) {
							$msg_type = "success";
							$msg_content = "<b>Berhasil:</b> Berita berhasil diubah.";
						} else {
							$msg_type = "error";
							$msg_content = "<b>Gagal:</b> Error system.";
						}
					}
				} else if (isset($_POST['delete'])) {
			$post_id = $_POST['id'];
			$checkdb_news = mysqli_query($db, "SELECT * FROM news WHERE id = '$post_id'");
			if (mysqli_num_rows($checkdb_news) == 0) {
				$msg_type = "error";
				$msg_content = "<b>Gagal:</b> Berita tidak ditemukan.";
			} else {
				$delete_news = mysqli_query($db, "DELETE FROM news WHERE id = '$post_id'");
				if ($delete_news == TRUE) {
					$msg_type = "success";
					$msg_content = "<b>Berhasil:</b> Berita deleted.";
				}
			}
		}

	include("../../lib/header.php");
?>
<div class="page-body"> 
<?php include('../../lib/partial/breadcrumb.php') ?>
   <!-- Container-fluid starts-->
   <div class="container-fluid">
            <div class="row justify-content-center">
              <div class="col-xxl-12">
            <div class="card">
  <div class="card-body">
    <div class="table-responsive">
      <table class="table table-hover">
        <thead><tr><th>Judul</th><th>Status</th><th>Response</th><th>Date</th></tr></thead>
        <tbody>
            <?
            $ambil_data = mysqli_query($db, "SELECT * FROM monitor_cron ORDER BY id ASC"); 
            while($datanya = mysqli_fetch_assoc($ambil_data)){
                $judull = $datanya['judul'];
                $statuss = $datanya['status'];
                $respong = $datanya['response'];
                $tanggal = $datanya['tanggal'];
                echo "<tr><td>$judull</td><td>$statuss</td><td><textarea>$respong</textarea></td><td>$tanggal</td></tr>";
            }
            ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
          </div>
              </div>
   </div>
   <!-- Container-fluid Ends-->
</div>
<?php
	include("../../lib/footer.php");
	}
} else {
	header("Location: ".$cfg_baseurl);
}
?>