<?php
include '../../../config.php';

// Fungsi untuk kompresi gambar dengan TinyPNG API
function compressImageWithTinyPNG($source, $destination) {
    // global $tinypng_key;

    // $ch = curl_init();
    // curl_setopt($ch, CURLOPT_URL, "https://api.tinify.com/shrink");
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    // curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($source));
    // curl_setopt($ch, CURLOPT_USERPWD, "api:" . $tinypng_key);

    // $result = curl_exec($ch);
    // curl_close($ch);

    // $output = json_decode($result);

    // if ($output && isset($output->output->url)) {
    //     $compressedImageURL = $output->output->url;
    //     file_put_contents($destination, file_get_contents($compressedImageURL));
    //     return true;
    // } else {
    //     return false;
    // }
    
    // update: without compress
    file_put_contents($destination, file_get_contents($source));
    return true;
}

$id_produk_ep = mysqli_real_escape_string($server, $_POST['id_produk_ep']);
$judul_ep = mysqli_real_escape_string($server, $_POST['judul_ep']);
$slug_ep = mysqli_real_escape_string($server, $_POST['slug_ep']);
$harga_ep = str_replace('.', '', $_POST['harga_ep']);
$kategori_ep = mysqli_real_escape_string($server, $_POST['kategori_ep']);
$brand_ep = mysqli_real_escape_string($server, $_POST['brand_ep']);
$berat_ep = str_replace('.', '', $_POST['berat_ep']);
$stok_ep = str_replace('.', '', $_POST['stok_ep']);
$deskripsi_ep = mysqli_real_escape_string($server, $_POST['deskripsi_ep']);
$diskon_ep = str_replace('.', '', $_POST['diskon_ep']);
$kondisi_ep = mysqli_real_escape_string($server, $_POST['kondisi_ep']);
$gratis_ongkir_ep = mysqli_real_escape_string($server, $_POST['gratis_ongkir_ep']);
$id_lokasi_ep = mysqli_real_escape_string($server, $_POST['id_lokasi_ep']);
$lokasi_ep = mysqli_real_escape_string($server, $_POST['lokasi_ep']);
$varian_warna = mysqli_real_escape_string($server, $_POST['varian_warna']);
$varian_ukuran = mysqli_real_escape_string($server, $_POST['varian_ukuran']);

if ($kategori_ep != '58') {
    $berat_ep = str_replace('.', '', $_POST['berat_ep']);
} else {
    $berat_ep = $_POST['berat_ep'];
    $demo_url = mysqli_real_escape_string($server, $_POST['input_demo_url_ep']);
    $url_unduh = mysqli_real_escape_string($server, $_POST['input_unduh_url_ep']);
}


$val_img_ed_ep1 = $_POST['val_img_ed_ep1'];
$val_img_ed_ep2 = $_POST['val_img_ed_ep2'];
$val_img_ed_ep3 = $_POST['val_img_ed_ep3'];
$val_img_ed_ep4 = $_POST['val_img_ed_ep4'];
$val_img_ed_ep5 = $_POST['val_img_ed_ep5'];

$img_name_random = round(microtime(true));

function compressAndUploadImage($fileInputName, $img_name_random) {
    $name = $img_name_random . '-' . $fileInputName . '.' . pathinfo($_FILES[$fileInputName]['name'], PATHINFO_EXTENSION);
    $path = "../../../assets/image/product/" . $name;

    if (!empty($_FILES[$fileInputName]["name"])) {
        $source = $_FILES[$fileInputName]["tmp_name"];
        $compressedPath = "../../../assets/image/product/compressed/" . $name;

        // Kompress gambar menggunakan fungsi TinyPNG
        compressImageWithTinyPNG($source, $compressedPath);

        return $name . ',';
    } else {
        return '';
    }
}

$nametodb1 = ($val_img_ed_ep1 == '1') ? compressAndUploadImage("c_img_ep_1", $img_name_random) : (($val_img_ed_ep1 == '') ? '' : $val_img_ed_ep1 . ',');
$nametodb2 = ($val_img_ed_ep2 == '1') ? compressAndUploadImage("c_img_ep_2", $img_name_random) : (($val_img_ed_ep2 == '') ? '' : $val_img_ed_ep2 . ',');
$nametodb3 = ($val_img_ed_ep3 == '1') ? compressAndUploadImage("c_img_ep_3", $img_name_random) : (($val_img_ed_ep3 == '') ? '' : $val_img_ed_ep3 . ',');
$nametodb4 = ($val_img_ed_ep4 == '1') ? compressAndUploadImage("c_img_ep_4", $img_name_random) : (($val_img_ed_ep4 == '') ? '' : $val_img_ed_ep4 . ',');
$nametodb5 = ($val_img_ed_ep5 == '1') ? compressAndUploadImage("c_img_ep_5", $img_name_random) : (($val_img_ed_ep5 == '') ? '' : $val_img_ed_ep5 . ',');

$name_gambar_add = $nametodb1 . $nametodb2 . $nametodb3 . $nametodb4 . $nametodb5;
$name_gambar = rtrim($name_gambar_add, ',');
$waktu_diperbarui = date('Y-m-d H:i:s');

if ($kategori_ep == '58') {
    $edit_produk_adm = $server->query("UPDATE `iklan` SET `id_kategori`='$kategori_ep', `id_brand`='$brand_ep', `gambar`='$name_gambar', `judul`='$judul_ep', `slug`='$slug_ep', `harga`='$harga_ep', `deskripsi`='$deskripsi_ep', `diskon`='$diskon_ep', `kondisi`='$kondisi_ep', `gratis_ongkir`='$gratis_ongkir_ep', `id_lokasi`='$id_lokasi_ep', `lokasi`='$lokasi_ep', `berat`='$berat_ep',`demo_url`='$demo_url', `unduh_url`='$url_unduh', `warna`='$varian_warna', `ukuran`='$varian_ukuran', `stok`='$stok_ep', `waktu_diperbarui`='$waktu_diperbarui', `status_moderasi`='Menunggu Moderasi' WHERE `id`='$id_produk_ep'");
} else {
    $edit_produk_adm = $server->query("UPDATE `iklan` SET `id_kategori`='$kategori_ep', `id_brand`='$brand_ep', `gambar`='$name_gambar', `judul`='$judul_ep', `slug`='$slug_ep', `harga`='$harga_ep', `deskripsi`='$deskripsi_ep', `diskon`='$diskon_ep', `kondisi`='$kondisi_ep', `gratis_ongkir`='$gratis_ongkir_ep', `id_lokasi`='$id_lokasi_ep', `lokasi`='$lokasi_ep', `berat`='$berat_ep', `warna`='$varian_warna', `ukuran`='$varian_ukuran', `stok`='$stok_ep', `waktu_diperbarui`='$waktu_diperbarui', `status_moderasi`='Menunggu Moderasi' WHERE `id`='$id_produk_ep'");
}

if ($edit_produk_adm) {
    echo json_encode(['status' => 'success', 'message' => 'Produk berhasil diperbarui.']);
?>
    <script>
        location.reload(); // Reload halaman saat proses selesai
    </script>
<?php 
} else {
    echo json_encode(['status' => 'error', 'message' => 'Gagal memperbarui produk.', 'error' => $server->error]);
}
?>
