<?php
include "koneksi.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $nama_pasaran = mysqli_real_escape_string($koneksi, $_POST['nama_pasaran']);
    $link_livedraw = mysqli_real_escape_string($koneksi, $_POST['link_livedraw']); // Tambahkan input link_livedraw

    // Pastikan folder penyimpanan ada
    $upload_dir = "images/pasaran"; // Direktori tanpa "../" agar sesuai dengan path penyimpanan langsung
    if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0777, true);
    }

    // Proses Upload Gambar
    $gambar_name = $_FILES['gambar_pasaran']['name'];
    $gambar_tmp = $_FILES['gambar_pasaran']['tmp_name'];
    $gambar_path = $upload_dir . "/" . $gambar_name;

    if (move_uploaded_file($gambar_tmp, $gambar_path)) {
        // Simpan ke database dengan kolom link_livedraw
        $query = "INSERT INTO data_pasaran (nama_pasaran, gambar_pasaran, link_livedraw) 
                  VALUES ('$nama_pasaran', '$gambar_path', '$link_livedraw')";
        if (mysqli_query($koneksi, $query)) {
            header("Location: index.php?page=daftar_pasaran&notif=berhasil_tambah_data");
            exit();
        } else {
            echo "Error: " . mysqli_error($koneksi);
        }
    } else {
        echo "Gagal mengupload gambar.";
    }
}
?>