<?php
include('../session.php');

// pastikan koneksi dan session sudah aktif
$users     = isset($u['user']) ? $u['user'] : 'admin';
$deskripsi = $_POST['deskripsi'] ?? '';
$sort      = $_POST['sort'] ?? 0;
$catID     = $_POST['postID'] ?? '';
$status    = $_POST['status'] ?? 1;
$kode      = date('YmdHis');

// ==========================
// KONFIGURASI UPLOAD
// ==========================
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . '/upload/slide/'; // ✅ folder slide
$tipe_gambar = [
    'image/jpg',
    'image/jpeg',
    'image/png',
    'image/gif',
    'image/bmp',
    'image/x-png'
];

$gbr    = $_FILES['image']['name'] ?? '';
$tipe   = $_FILES['image']['type'] ?? '';
$error  = $_FILES['image']['error'] ?? 4;
$ukuran = $_FILES['image']['size'] ?? 0;

$extensi = strtolower(pathinfo($gbr, PATHINFO_EXTENSION));
$newname = 'slide_' . $users . '_' . $kode . '.' . $extensi;

// ==========================
// BUAT FOLDER JIKA BELUM ADA
// ==========================
if (!is_dir($upload_dir)) {
    mkdir($upload_dir, 0755, true);
}

// ==========================
// MODE TAMBAH SLIDE BARU
// ==========================
if ($catID == '') {

    if ($ukuran > 0 && $ukuran <= 5242880) { // ✅ 5MB
        if ($gbr != '' && $error == 0) {
            if (in_array(strtolower($tipe), $tipe_gambar)) {

                move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $newname);

                mysqli_query($conn, "
                    INSERT INTO tb_slide (image, deskripsi, sort, user, status)
                    VALUES ('$newname', '$deskripsi', '$sort', '$users', '$status')
                ") or die(mysqli_error($conn));

                header('Location: ' . $urlweb . '/slide.php?notif=1');
                exit;

            } else {
                header('Location: ' . $urlweb . '/slide.php?notif=3');
                exit;
            }
        } else {
            header('Location: ' . $urlweb . '/slide.php?notif=4');
            exit;
        }
    } else {
        header('Location: ' . $urlweb . '/slide.php?notif=2');
        exit;
    }

// ==========================
// MODE EDIT SLIDE
// ==========================
} else {

    if ($ukuran > 0 && $ukuran <= 5242880) {
        if ($gbr != '' && $error == 0) {
            if (in_array(strtolower($tipe), $tipe_gambar)) {

                move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $newname);

                mysqli_query($conn, "
                    UPDATE tb_slide 
                    SET image='$newname', deskripsi='$deskripsi', sort='$sort'
                    WHERE cuid='$catID'
                ") or die(mysqli_error($conn));

                header('Location: ' . $urlweb . '/slide.php?catID=' . $catID . '&notif=1');
                exit;

            } else {
                header('Location: ' . $urlweb . '/slide.php?catID=' . $catID . '&notif=3');
                exit;
            }
        } else {
            mysqli_query($conn, "
                UPDATE tb_slide 
                SET deskripsi='$deskripsi', sort='$sort'
                WHERE cuid='$catID'
            ") or die(mysqli_error($conn));

            header('Location: ' . $urlweb . '/slide.php?catID=' . $catID . '&notif=1');
            exit;
        }
    } else {
        header('Location: ' . $urlweb . '/slide.php?catID=' . $catID . '&notif=2');
        exit;
    }
}
?>
