<?php
session_start();
include "config/koneksi.php";
include "config/library.php";
// Cek login
if(!isset($_SESSION['user'])){
    echo "Silakan login terlebih dahulu!";
    exit;
}

// Ambil info user login
$user_login = mysqli_real_escape_string($konek, $_SESSION['user']['email']); 
$level = $_SESSION['user']['level']; // admin/user

// Function SEO title (jika belum ada)
function seo_title($string){
    $string = strtolower($string);
    $string = preg_replace("/[^a-z0-9]+/", "-", $string);
    $string = trim($string, "-");
    return $string;
}

// Hapus data
if(isset($_POST['hapus'])){
    $id = intval($_POST['hapus']);
    if($level == 'admin'){
        mysqli_query($konek, "DELETE FROM berita WHERE id_berita='$id'");
    } else {
        mysqli_query($konek, "DELETE FROM berita WHERE id_berita='$id' AND username='$user_login'");
    }
    echo "Sukses Delete!";
    exit;
}

// Tambah / edit data
if(isset($_POST['judul'])){ 
    $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
    $judul = mysqli_real_escape_string($konek, $_POST['judul']);
    $judul_seo = seo_title($judul);
    $regist = mysqli_real_escape_string($konek, $_POST['regist']);
    $age = mysqli_real_escape_string($konek, $_POST['age']);
    $harga = mysqli_real_escape_string($konek, $_POST['harga']);
    $tanggal = mysqli_real_escape_string($konek, $_POST['tanggal']);

    if($id){ // edit
        if($level == 'admin'){
            mysqli_query($konek, "UPDATE berita SET judul='$judul', judul_seo='$judul_seo', isi_berita='$regist', a1='$age', a2='$harga', tanggal='$tanggal' WHERE id_berita='$id'");
        } else {
            mysqli_query($konek, "UPDATE berita SET judul='$judul', judul_seo='$judul_seo', isi_berita='$regist', a1='$age', a2='$harga', tanggal='$tanggal' WHERE id_berita='$id' AND username='$user_login'");
        }
        echo "Sukses update!";
    } else { // tambah
        mysqli_query($konek, "INSERT INTO berita (judul, judul_seo, isi_berita, a1, a2, tanggal, username) VALUES ('$judul','$judul_seo','$regist','$age','$harga', '$tgl_sekarang','$user_login')");
        echo "Sukses!";
    }
    exit;
}

// Ambil data untuk edit
if(isset($_GET['id'])){
    $id = intval($_GET['id']);
    if($level == 'admin'){
        $res = mysqli_query($konek, "SELECT * FROM berita WHERE id_berita='$id'");
    } else {
        $res = mysqli_query($konek, "SELECT * FROM berita WHERE id_berita='$id' AND username='$user_login'");
    }
    echo json_encode(mysqli_fetch_assoc($res));
    exit;
}

// Load table
if($level == 'admin'){
    $res = mysqli_query($konek, "SELECT * FROM berita ORDER BY id_berita DESC");
} else {
    $res = mysqli_query($konek, "SELECT * FROM berita WHERE username='$user_login' ORDER BY id_berita DESC");
}

echo "<table class='table table-striped table-hover'>
        <thead><tr><th>No</th><th>Domain</th><th>Regist</th><th>Registration</th><th>Maximum Price</th><th>tgl_update</th><th>Action</th></tr></thead><tbody>";
date_default_timezone_set('Asia/Jakarta');
$today = new DateTime();
$no=1;
while($r=mysqli_fetch_assoc($res)){
    // ambil tanggal dari database
    $tgl_db = new DateTime($r['tanggal']);

    // hitung selisih hari
    $diff = $today->diff($tgl_db);

    // default style
    $rowStyle = "";

    // kalo selisih >= 1 bulan (atau lebih)
    if ($diff->m >= 3 || $diff->y >= 3) {
        $rowStyle .= "background:#ff9800;"; 
    }
     $tgl_posting=tgl_indo($r['tanggal']);
    echo "<tr style=\"$rowStyle\">
            <td>$no</td>
            <td>".htmlspecialchars($r['judul'])."</td>
            <td>".htmlspecialchars($r['isi_berita'])."</td>
            <td>".htmlspecialchars($r['a1'])."</td>
            <td>".htmlspecialchars($r['a2'])."</td>
             <td>$tgl_posting</td>
            <td>
              <button class='btn btn-sm btn-primary editBtn' data-id='{$r['id_berita']}'>Edit</button>
              <button class='btn btn-sm btn-danger hapusBtn' data-id='{$r['id_berita']}'>Delete</button>
            </td>
          </tr>";
    $no++;
}
echo "</tbody></table>";
?>
