<?php
date_default_timezone_set('Asia/Jakarta');
include "../config/rupiah.php";
include "../config/library.php";
include "../config/koneksi.php";

$now = date('m');


//tahun ajaran

//url tagihan
$page_URL = (@$_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);

//whatsapp api
$idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas LIMIT 1"));
$link_send = $idt['link_one_sender'];
$token_send = $idt['token'];

$headers = array();
$headers[] = $token_send;
$headers[] = 'Content-Type: application/x-www-form-urlencoded';

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

    $lst_siswa = mysqli_query($conn, "SELECT * FROM siswa ");
    while ($sws = mysqli_fetch_array($lst_siswa)) {

        $isi = $_POST['isi'];
        $link = $_POST['link'];
        $total_tagihan = 0;
        $no = 1;

        $msg_wa = array();
        $number_wa = array();

        //pesan whatsapp siswa 
        $number_wa[] = $sws['noHpSis'];
        $msg_wa[] = '' . $isi . '';

        //pesan whatsapp ortu 
        $number_wa[] = $sws['noHpOrtu'];
        $msg_wa[] = '' . $isi . '';

        for ($i = 0; $i < count($number_wa); $i++) {

            $apikey = $token_send;
            $tujuan = $number_wa[$i];
            $pesan = $msg_wa[$i];

            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => 'https://starsender.online/api/sendText?message=' . rawurlencode($pesan) . '&tujuan=' . rawurlencode($tujuan . '@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: ' . $apikey
                ),
            ));

            $response = curl_exec($curl);

            curl_close($curl);

            echo   '<script>document.location="index.php?view=pengumuman&sukses";</script><br>';
        }
    }
}
if (isset($_POST['kirim'])) {

    $lst_siswa = mysqli_query($conn, "SELECT * FROM siswa ");
    while ($sws = mysqli_fetch_array($lst_siswa)) {

        $isi = $_POST['isi'];
        $footer = $_POST['footer'];
        $button1 = $_POST['button1'];
        $button2 = $_POST['button2'];
        $total_tagihan = 0;
        $no = 1;

        $msg_wa = array();
        $number_wa = array();

        //pesan whatsapp siswa 
        $number_wa[] = $sws['noHpSis'];
        $msg_wa[] = '' . $isi . '';

        //pesan whatsapp ortu 
        $number_wa[] = $sws['noHpOrtu'];
        $msg_wa[] = '' . $isi . '';

        for ($i = 0; $i < count($number_wa); $i++) {

            $data = [
                "api_key" => $token_send,
                "sender" => $number_send,
                "number" => $number_wa[$i],
                "message" => $msg_wa[$i],
                "footer" => $footer,
                "button1" => $button1,
                "button2" => $button2
            ];

            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $link_sends,
                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_POSTFIELDS => json_encode($data),
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                ),
            ));

            $response = curl_exec($curl);

            curl_close($curl);

            echo   '<script>document.location="index.php?view=pengumuman&sukses";</script><br>';
        }
    }
}
if (isset($_POST['sends'])) {

    $lst_siswa = mysqli_query($conn, "SELECT * FROM siswa ");
    while ($sws = mysqli_fetch_array($lst_siswa)) {

        $isi = $_POST['isi'];

        $total_tagihan = 0;
        $no = 1;

        $msg_wa = array();
        $number_wa = array();

        //pesan whatsapp siswa 
        $number_wa[] = $sws['noHpSis'];
        $msg_wa[] = '' . $isi . '';

        //pesan whatsapp ortu 
        $number_wa[] = $sws['noHpOrtu'];
        $msg_wa[] = '' . $isi . '';

        for ($i = 0; $i < count($number_wa); $i++) {

            $data = [
                "api_key" => $token_send,
                "sender" => $number_send,
                "number" => $number_wa[$i],
                "message" => $msg_wa[$i]

            ];
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $link_sendd,
                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_POSTFIELDS => json_encode($data),
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                ),
            ));

            $response = curl_exec($curl);

            curl_close($curl);

            echo   '<script>document.location="index.php?view=pengumuman&sukses";</script><br>';
        }
    }
}
?>





<section class="content">
<!-- 
    <div class="col-md-6 ">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-whatsapp "> </i> Pesan Gambar Massal </h3>
                <form class="form-horizontal" method="POST">
                    <div class="box-body">
                        <div class="col-sm-12">
                            <label for="" class=" control-label">Isi Pesan</label>

                            <input type="text" name="isi" class="form-control" id="" placeholder="Isi Pesan" required>

                        </div>
                        <div class="col-sm-12">
                            <label for="" class=" control-label">Link Gambar</label>

                            <input type="text" name="link" class="form-control" id="" placeholder="Link Gambar">

                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label"></label>
                            <div class="col-sm-12">
                                <button type="submit" name="send" class="btn btn-info pull-right"><i class="fa fa-whatsapp"> </i> Kirim</button>

                            </div>
                        </div>

                    </div>

                </form>
            </div>
        </div>
    </div> -->
    <div class="col-md-6 ">
        <div class="box box-warning">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-whatsapp "> </i> Pesan Massal </h3>
                <form class="form-horizontal" method="POST">
                    <div class="box-body">
                    <?php
                    if (isset($_GET['sukses'])) {
                        echo "<div class='alert alert-warning alert-dismissible fade in' role='alert'> 
					<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
					<span aria-hidden='true'>×</span></button> <strong>Sukses!</strong> - Pesan telah terkirim,..
					</div>";
                    } elseif (isset($_GET['gagal'])) {
                        echo "<div class='alert alert-danger alert-dismissible fade in' role='alert'> 
					<button type='button' class='close' data-dismiss='alert' aria-label='Close'>
					<span aria-hidden='true'>×</span></button> <strong>Gagal!</strong> - Data tidak Di Proses, terjadi kesalahan dengan data..
					</div>";
                    }
                    ?>
                        <div class="col-sm-12">
                            <label for="" class=" control-label">Isi Pesan</label>

                            <input type="text" name="isi" class="form-control" id="" placeholder="Isi Pesan" required>

                        </div>

                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label"></label>
                            <div class="col-sm-12">
                                <button type="submit" name="send" class="btn btn-info pull-right"><i class="fa fa-whatsapp"> </i> Kirim</button>

                            </div>
                        </div>

                    </div>

                </form>
            </div>
        </div>
    </div>
    <!-- <div class="col-md-6 ">
        <div class="box box-info ">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-whatsapp "> </i> Pesan Tombol Massal </h3>
                <form class="form-horizontal" method="POST">
                    <div class="box-body">
                        <div class="col-sm-12">
                            <label for="" class=" control-label">Isi Pesan</label>

                            <input type="text" name="isi" class="form-control" id="" placeholder="Isi Pesan" required>

                        </div>
                        <div class="col-sm-12">
                            <label for="" class=" control-label">Keterangan</label>

                            <input type="text" name="footer" class="form-control" id="" placeholder="Keterangan">

                        </div>
                        <div class="col-sm-12">
                            <label for="" class="control-label">Tombol 1</label>

                            <input type="text" name="button1" class="form-control" id="" placeholder="Tombol 1">

                        </div>
                        <div class="col-sm-12">
                            <label for="" class=" control-label">Tombol 2</label>

                            <input type="text" name="button2" class="form-control" id="" placeholder="Tombol 2">

                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label"></label>
                            <div class="col-sm-12">
                                <button type="submit" name="kirim" class="btn btn-warning pull-right"><i class="fa fa-whatsapp"> </i> Kirim </button>

                            </div>
                        </div>
                    </div>

                </form>
            </div>
        </div>
    </div>  -->

</section>