<?php
require_once "koneksi.php";

/* =========================
   HAPUS DATA
========================= */
if(isset($_GET['hapus'])){
    $id = $_GET['hapus'];
    $koneksi->query("DELETE FROM materi WHERE id=$id");

    echo "<script>
    localStorage.setItem('hapus_success','1');
    window.location='dashboard_adm_pengembangandiri.php';
    </script>";
    exit;
}

/* =========================
   TAMBAH DATA
========================= */
if(isset($_POST['tambah'])){

    $judul = $_POST['judul'];
    $deskripsi = $_POST['deskripsi'];

    $file = time().$_FILES['file']['name'];
    move_uploaded_file($_FILES['file']['tmp_name'], "materi_file/".$file);

    $gambar = time().$_FILES['gambar']['name'];
    move_uploaded_file($_FILES['gambar']['tmp_name'], "materi_img/".$gambar);

    $koneksi->query("
        INSERT INTO materi (judul, deskripsi, file, gambar)
        VALUES ('$judul','$deskripsi','$file','$gambar')
    ");

    echo "<script>
    localStorage.setItem('tambah_success','1');
    window.location='dashboard_adm_pengembangandiri.php';
    </script>";
    exit;
}

/* =========================
   UPDATE DATA
========================= */
if(isset($_POST['update'])){

    $id = $_POST['id'];
    $judul = $_POST['judul'];
    $deskripsi = $_POST['deskripsi'];

    $data = mysqli_fetch_array(mysqli_query($koneksi,"SELECT * FROM materi WHERE id=$id"));

    if(!empty($_FILES['file']['name'])){
        $file = time().$_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'], "materi_file/".$file);
    } else {
        $file = $data['file'];
    }

    if(!empty($_FILES['gambar']['name'])){
        $gambar = time().$_FILES['gambar']['name'];
        move_uploaded_file($_FILES['gambar']['tmp_name'], "materi_img/".$gambar);
    } else {
        $gambar = $data['gambar'];
    }

    $koneksi->query("
        UPDATE materi SET 
        judul='$judul',
        deskripsi='$deskripsi',
        file='$file',
        gambar='$gambar'
        WHERE id=$id
    ");

    echo "<script>
    localStorage.setItem('update_success','1');
    window.location='dashboard_adm_pengembangandiri.php';
    </script>";
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Admin Materi</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>
body {
    background:#eef2f7;
}
.card-modern{
    border-radius:12px;
    box-shadow:0 6px 20px rgba(0,0,0,0.1);
}
</style>
</head>

<body>

<div class="container mt-4">
<a href="dashboard_admin.php" class="btn btn-secondary btn-modern">
<i class="fas fa-arrow-left"></i> Kembali
</a>
<h3>📚 Kelola Materi</h3>

<!-- FORM TAMBAH -->
<div class="card card-modern p-3 mb-4">
<form method="post" enctype="multipart/form-data">

<input type="text" name="judul" class="form-control mb-2" placeholder="Judul" required>

<textarea name="deskripsi" class="form-control mb-2" placeholder="Isi ringkasan artikel"></textarea>

<label>File Materi</label>
<input type="file" name="file" class="form-control mb-2" required>

<label>Gambar</label>
<input type="file" name="gambar" class="form-control mb-2" required>

<button name="tambah" class="btn btn-primary">Tambah Artikel</button>

</form>
</div>

<!-- TABEL -->
<div class="card card-modern p-3">

<table class="table table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Judul</th>
<th>Aksi</th>
</tr>
</thead>

<tbody>

<?php
$no=1;
$data = mysqli_query($koneksi,"SELECT * FROM materi ORDER BY id DESC");

while($d = mysqli_fetch_array($data)){
echo "<tr>
<td>$no</td>
<td>$d[judul]</td>
<td>

<button class='btn btn-warning btn-sm' onclick='openEditModal(
\"$d[id]\",
\"$d[judul]\",
`$d[deskripsi]`
)'>✏️</button>

<button class='btn btn-danger btn-sm' onclick='hapusData($d[id])'>🗑️</button>

</td>
</tr>";
$no++;
}
?>

</tbody>
</table>

</div>

</div>

<!-- MODAL EDIT -->
<div class="modal fade" id="modalEdit">
<div class="modal-dialog">
<div class="modal-content">

<div class="modal-header bg-warning">
<h5>Edit Artikel</h5>
</div>

<form method="post" enctype="multipart/form-data">
<div class="modal-body">

<input type="hidden" name="id" id="edit_id">

<input type="text" name="judul" id="edit_judul" class="form-control mb-2">

<textarea name="deskripsi" id="edit_deskripsi" class="form-control mb-2"></textarea>

<label>Ganti File</label>
<input type="file" name="file" class="form-control mb-2">

<label>Ganti Gambar</label>
<input type="file" name="gambar" class="form-control mb-2">

</div>

<div class="modal-footer">
<button name="update" class="btn btn-success">Update</button>
</div>

</form>

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

<!-- JS -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
// MODAL EDIT
function openEditModal(id, judul, deskripsi){
    document.getElementById('edit_id').value = id;
    document.getElementById('edit_judul').value = judul;
    document.getElementById('edit_deskripsi').value = deskripsi;

    $('#modalEdit').modal('show');
}

// HAPUS
function hapusData(id){
    Swal.fire({
        title: 'Yakin hapus?',
        text: "Data tidak bisa dikembalikan!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        confirmButtonText: 'Ya, hapus!'
    }).then((result) => {
        if(result.isConfirmed){
            window.location = '?hapus=' + id;
        }
    })
}

// NOTIF GLOBAL
if(localStorage.getItem('tambah_success')){
    Swal.fire({
        icon: 'success',
        title: 'Berhasil!',
        text: 'Artikel berhasil ditambahkan',
        timer: 2000,
        showConfirmButton: false
    });
    localStorage.removeItem('tambah_success');
}

if(localStorage.getItem('update_success')){
    Swal.fire({
        icon: 'success',
        title: 'Berhasil!',
        text: 'Artikel berhasil diupdate',
        timer: 2000,
        showConfirmButton: false
    });
    localStorage.removeItem('update_success');
}

if(localStorage.getItem('hapus_success')){
    Swal.fire({
        icon: 'success',
        title: 'Berhasil!',
        text: 'Artikel berhasil dihapus',
        timer: 2000,
        showConfirmButton: false
    });
    localStorage.removeItem('hapus_success');
}
</script>

</body>
</html>