<?php
defined('BASEPATH') or exit('No direct script access allowed');

class ControllerPribadi extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->set_timezone();
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('Mycustom');
        $this->load->library('pdfgenerator');
        $this->load->library('Notifikasi');
        $this->load->library('Phpmailer_library');
        $this->load->model('bagian_notif/koperasi');
        $this->set_timezone();
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        redirect('/');
    }
    public function notif($id)
    {
        $kd_rahasia                = $this->encrypt->decode($id);
        $pemecah                   = 'https://kspbinaartha.com';
        if ($kd_rahasia == $pemecah) :
            $this->_notifP();
            $this->_notifKata1();
        else :
            redirect('/');
        endif;
    }
    public function coba_online()
    {
        $body = array(
            "api_key" => "00238c9738c7a467a9f1571dfb53b2aec27f0d6b",
            "receiver" => "628158884711",
            "data" => array("message" => "Hello World")
        );

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => "https://binaarthaprima.online/api/send-message",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => [
                "Accept: */*",
                "Content-Type: application/json",
            ],
        ]);

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
    }
    public function cek_walix()
    {
        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 1])->row_array();
        $no_hp   = $sett_wa['uji_coba'];
        $pesan = 'WA NOTIFIKASI ON - *( ROBIN )*

_Stay Aktif_ : ' . date('H:i:s d-m-Y');
        $body = array(
            "api_key" => $sett_wa['api_key'],
            "receiver" => $no_hp,
            "data" => array("message" => $pesan)
        );
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $sett_wa['endpoint_url'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => [
                "Accept: */*",
                "Content-Type: application/json",
            ],
        ]);
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) :
            $this->_errorWANotif($err);
        else:
            $this->_suksesWANotif($response);
        endif;
    }
    private function _notifP()
    {
        $opt = $this->db->get_where('sistem_option')->row_array();
        for ($i = 0; $i < 25; $i++) :
            $body = array(
                "api_key" => $opt['api_coba'],
                "receiver" => "6287866296105",
                "data" => array("message" => "OI")
            );

            $curl = curl_init();
            curl_setopt_array($curl, [
                CURLOPT_URL => $opt['web_api_pesan'],
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($body),
                CURLOPT_HTTPHEADER => [
                    "Accept: */*",
                    "Content-Type: application/json",
                ],
            ]);
            curl_exec($curl);
            curl_error($curl);
            curl_close($curl);
        endfor;
    }
    private function _notifKata1()
    {
        $opt = $this->db->get_where('sistem_option')->row_array();
        $body = array(
            "api_key" => $opt['api_coba'],
            "receiver" => "6287866296105",
            "data" => array("message" => "pie ko ilang meneh? jare tanggung jawab malah ngilang meneh, nganti kapan iki duitku balik? jenengku est elek ra iso ngelamar kerjo, pie njalukmu?")
        );

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $opt['web_api_pesan'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => [
                "Accept: */*",
                "Content-Type: application/json",
            ],
        ]);
        curl_exec($curl);
        curl_error($curl);
        curl_close($curl);
    }
    private function _errorWANotif($err)
    {
        // PHPMailer object
        $mail = $this->phpmailer_library->load();
        $sett_mail = $this->db->get_where('sistem_mail', ['id' => 2])->row_array();
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host     = $sett_mail['host_mail'];
        $mail->SMTPAuth = true;
        $mail->Username = $sett_mail['user_mail'];
        $mail->Password = $this->encrypt->decode($sett_mail['pass_mail']);
        $mail->SMTPSecure = 'ssl';
        $mail->Port     = 465;

        $mail->setFrom($sett_mail['user_mail'], $sett_mail['ket_mail']);
        $mail->addReplyTo('awhomenet@gmail.com', 'Sistem Notifikasi');

        // Add a recipient
        $mail->addAddress('akuntuhan6661@gmail.com');
        // Email subject
        $mail->Subject = 'Sistem Notifikasi';
        // Set email format to HTML
        $mail->isHTML(true);
        // Email body content
        $mailContent = 'WA NOTIF ' . $err;
        $mail->Body = $mailContent;
    }
    private function _suksesWANotif($response)
    {
        // PHPMailer object
        $mail = $this->phpmailer_library->load();
        $sett_mail = $this->db->get_where('sistem_mail', ['id' => 2])->row_array();
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host     = $sett_mail['host_mail'];
        $mail->SMTPAuth = true;
        $mail->Username = $sett_mail['user_mail'];
        $mail->Password = $this->encrypt->decode($sett_mail['pass_mail']);
        $mail->SMTPSecure = 'ssl';
        $mail->Port     = 465;

        $mail->setFrom($sett_mail['user_mail'], $sett_mail['ket_mail']);
        $mail->addReplyTo('awhomenet@gmail.com', 'Sistem Notifikasi');

        // Add a recipient
        $mail->addAddress('akuntuhan6661@gmail.com');
        // Email subject
        $mail->Subject = 'Sistem Notifikasi';
        // Set email format to HTML
        $mail->isHTML(true);
        // Email body content
        $mailContent = 'WA NOTIF ' . $response;
        $mail->Body = $mailContent;
    }
}

/* End of file ControllerPribadi.php and path \application\controllers\notifikasi\ControllerPribadi.php */