<?php

include_once 'config.php';
include_once 'function.php';

$hasil = mysqli_query($koneksi, "SELECT * FROM users_id" );
$check = mysqli_fetch_assoc($hasil);
$apikeystar = $check['apikey'];
$apikeyone = $check['apikeyone'];
$apiurlonesender = $check['apiurlonesender'];

$active_event_id = get_active_event_id();

if(isset($_POST['bulk_delete_submit'])){
    if(!empty($_POST['checked_id']) && $active_event_id){
        // Securely cast IDs to integers
        $ids = array_map('intval', $_POST['checked_id']);
        $idStr = implode(',', $ids);
        
        $delete = $koneksi->query("DELETE FROM comment_id WHERE no_id IN ($idStr) AND event_id = '$active_event_id'");

        if($delete){
            $statusMsgok = "Data berhasil dihapus";
        } else{
            $statusMsgerror = "Data gagal dihapus";
        }
    }else{
        $statusMsgerror = "Pilih data sebelum hapus";
    }
}

if(isset($_POST['bulk_delete_submit_all'])){
    if ($active_event_id) {
        $delete = $koneksi->query("DELETE FROM comment_id WHERE event_id = '$active_event_id'");

        if($delete){
            $statusMsgok = "Semua Data berhasil dihapus";
        } else{
            $statusMsgerror = "Semua Data gagal dihapus";
        }
    } else {
        $statusMsgerror = "Event tidak ditemukan";
    }
}

if(isset($_POST['bulk_send_submit_starsender'])){

    $emailResponses = '';
    foreach($_POST['bulk_send_submit_starsender'] as $row){
        global $apikeystar;
    
        $api = $apikeystar;
        $penerima = $row["wa"];
        $pesan = $row["pesan"];

        $curl = curl_init();
        
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://starsender.online/api/sendText?message='.rawurlencode($pesan).'&tujuan='.rawurlencode($penerima.'@s.whatsapp.net'),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_HTTPHEADER => array(
            'apikey: '.$api
        ),
        ));
        
        $response = curl_exec($curl);

        $json = json_decode($response);
        if ($json->success == true) {
            $emailResponses = 'success';
            
        }else{
            $emailResponses = 'error';
            
        }
    }

    header('Content-Type: application/json');
    echo json_encode($emailResponses);

     
}

if(isset($_POST['bulk_send_submit_onesender'])){

    $emailResponses = '';
    foreach($_POST['bulk_send_submit_onesender'] as $row){
        global $apiurlonesender;
        global $apikeyone;
    
        $api1 = $apikeyone;
        $apiurl = $apiurlonesender;
        $penerima = $row["wa"];
        $pesan = $row["pesan"];
        
        $data = array( 
            'recipient_type' => 'individual',
            'to' => $penerima,
            'type' => 'text',
            'text' => array(
                'body' => $pesan
            )
        ); 
        
        $json_data = json_encode($data);
        $headers = [
               'Content-Type: application/json',
                'Authorization: Bearer ' . $api1
        ];
                
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response1 = curl_exec($ch);
        $header = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        curl_close($ch);

        $json = json_decode($response1);
        if ($json->code == '200') {
            $emailResponses = 'success';
        } else{
            $emailResponses = 'error';
        }

    }
    
    header('Content-Type: application/json');
    echo json_encode($emailResponses);

}