<?php
require_once "../mainconfig.php"; //koneksi kepada database
mysqli_query($db, "TRUNCATE TABLE services");
$key = "aa9f8e-bb2b3f-61fa01-587b63-188230"; // your api key
$postdata = "api_key=$key&api_id=2068";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://irvankedesmm.co.id/api/services");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$chresult = curl_exec($ch);
//echo $chresult;
curl_close($ch);
$json_result = json_decode($chresult, true);
$indeks = 0;
// get data service
while ($indeks < count($json_result["data"])) {
    $category = $json_result["data"][$indeks]["category"];
    $average = $json_result["data"][$indeks]["average"];
    $id = $json_result["data"][$indeks]["id"];
    $service = $json_result["data"][$indeks]["name"];
    $min_order = $json_result["data"][$indeks]["min"];
    $max_order = $json_result["data"][$indeks]["max"];
    $price = $json_result["data"][$indeks]["price"];
    $note = $json_result["data"][$indeks]["note"];
    $status = $json_result["data"][$indeks]["status"];
    if ($status == "0") {
        $finalnya = "Not active";
    } else {
        $finalnya = "Active";
    }
    $indeks++;
    // end get data service
    // setting price
    $k = 1.2;
    $rate = $price;
    $rate_asli = $rate * $k; //setting penambahan harga
    $layanan2 = strtr($service, [
    " IRVANKEDE" => " BMED",
    " IRVAN KEDE" => " BMED",
    " Irvankede" => " BMED",
    " IRV" => " BMED",
    " IKP" => " BMED",
    " IK" => " BMED",
]);
$category2 = strtr($category, [
    " IRVANKEDE" => " BMED",
    " IRVAN KEDE" => " BMED",
    " Irvankede" => " BMED",
    " IRV" => " BMED",
    " IKP" => " BMED",
    " IK" => " BMED",
    "TikTok  Views, Komentar , Like Komentar &Share " => "TikTok Service",
    "TikTok  Views, Komentar , Like Komentar &Share " => "TikTok Service",
]);
$note2 = strtr($note, [
    " IRVANKEDE" => " BMED",
    " IRVAN KEDE" => " BMED",
    " Irvankede" => " BMED",
    " IRV" => " BMED",
    " IKP" => " BMED",
    " IK" => " BMED",
]);

// Tambahkan penggantian untuk tanda "(" dan ")"
$layanan2 = str_replace("(", "[", $layanan2);
$layanan2 = str_replace(")", "]", $layanan2);

$category2 = str_replace("(", "[", $category2);
$category2 = str_replace(")", "]", $category2);

$note2 = str_replace("(", "[", $note2);
$note2 = str_replace(")", "]", $note2);
    $category2 = mysqli_real_escape_string($db, $category2);
    $layanan2 = mysqli_real_escape_string($db, $layanan2);
    $insert = mysqli_query($db,"INSERT INTO services (sid,category,service,note, average, min, max, price, status, pid, provider) VALUES ('$id','$category2','$layanan2','$note2', '$average','$min_order','$max_order','$rate_asli','$finalnya','$id','SMMKITA')"); //Memasukan Kepada Database (OPTIONAL)
}
$update_monitor = mysqli_query($db,"UPDATE monitor_cron SET status = 'Success', response = 'File Get service berhasil dijalankan', tanggal = '$date $time' WHERE judul = 'get_service'");
echo "File Get service berhasil dijalankan";
?>