<?php

class PesanWA {

    // KirimpesanW('112','PesanKirimTag','202','https://dnynet.my.id/images/image.php')
    public function Create_Pesan($IdBS,$pel,$pesan,$tag='')
    {
        include "config/connect.php";

        $ap = $koneksi->query("SELECT $pesan FROM basicsetting WHERE id_admin = '$IdBS'");
        $pp = $ap->fetch_assoc();
        $dp = $pp[$pesan];

        $ambil = $koneksi->query("SELECT id_paket,no_hp,nama_pelanggan,pppoe,alamat_pelanggan,nama_paket FROM pelanggan JOIN paket ON pelanggan.id_paket=paket.id_paket WHERE id_pelanggan = $pel");
        $pecah = $ambil->fetch_assoc();

        $idp     = $pecah['id_paket'];
        $phone   = $pecah['no_hp'];
        $nama    = $pecah['nama_pelanggan'];
        $pppoe   = $pecah['pppoe'];
        $address = $pecah['alamat_pelanggan'];
        $layanan  = $pecah['nama_paket'];

        if ($tag !='') 
        {
            $tagihan = $koneksi->query("SELECT * FROM tagihan WHERE id_tagihan = $tag");
            $pectag  = $tagihan->fetch_assoc();

            $keterangan = $pectag['KeteranganTag'];
            $limit      = $pectag['limit_pembayaran'];
            $jumlah     = number_format($pectag['JumlahTagihan']);
            $ppn        = number_format($pectag['PPN']);
            $kode       = $pectag['kodeunik'];
            $total      = number_format($pectag['VerifRek']);
        }
        else
        {
            $keterangan = ''; $limit = ''; $jumlah  = ''; $ppn = ''; $kode = ''; $total = '';
        }

            if (preg_match("/#usr/i", $dp) == 1) 
            {
                $t1 = str_replace("#usr",$nama,$dp);
            }
            else { $t1 = $dp; }

            if (preg_match("/#ppp/i", $t1) == 1) 
            {
                $t2 = str_replace("#ppp",$pppoe,$t1);
            }
            else { $t2 = $t1; }

            if (preg_match("/#add/i", $t2) == 1) 
            {
                $t3 = str_replace("#add",$address,$t2);
            }
            else { $t3 = $t2; }

            if (preg_match("/#lyn/i", $t3) == 1) 
            {
                $t4 = str_replace("#lyn",$layanan,$t3);
            }
            else { $t4 = $t3; }
            
            if (preg_match("/#tot/i", $t4) == 1) 
            {
                $t5 = str_replace("#tot",$total,$t4);
            }
            else { $t5 = $t4; }
        
            if (preg_match("/#ket/i", $t5) == 1) 
            {
                $t6 = str_replace("#ket",$keterangan,$t5);
            }
            else { $t6 = $t5; }

            if (preg_match("/#lmt/i", $t6) == 1) 
            {
                $t7 = str_replace("#lmt",$limit,$t6);
            }
            else { $t7 = $t6; }

            if (preg_match("/#jum/i", $t7) == 1) 
            {
                $t8 = str_replace("#jum",$jumlah,$t7);
            }
            else { $t8 = $t7; }

            if (preg_match("/#ppn/i", $t8) == 1) 
            {
                $t9 = str_replace("#ppn",$ppn,$t8);
            }
            else { $t9 = $t8; }

            if (preg_match("/#unk/i", $t9) == 1) 
            {
                $t10 = str_replace("#unk",$kode,$t9);
            }
            else { $t10 = $t9; }

            $this->PesanWA = $t10;

    }

    public function Get_Pesan()
    {
        return $this->PesanWA;
    }

    public function Send_Pesan($apiurl,$apikey,$nowa,$message,$file_url='')
    {
        //Perbaiki kode negara ind nomor WA
        if (substr($nowa,0,2)=='08') 
        {
            $nohp = '62'.substr($nowa, 1);
        }
        elseif (substr($nowa,0,3)=='628') 
        {
            $nohp = $nowa;
        }

        if ($file_url!='') 
        {
        $body = array(
                'Apikey'    => $apikey,
                'Phone'     => $nohp,
                'Message'   => $message,
                'File'      => $file_url
                );
        }
        else
        {
        $body = array(
                'Apikey'    => $apikey,
                'Phone'     => $nohp,
                'Message'   => $message
                );
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $apiurl.'api/sendpost.php');
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT,30);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
        curl_exec($curl);
        curl_close($curl);
    }
}
?>