<?php
error_reporting(0);
ini_set(“display_errors”, 0);
include "protege_professor.php";

?>
<!DOCTYPE html>
<html>

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <title>Immobile | SMS</title>

    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />

    <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-1.9.1.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/jquery-ui/themes/base/minified/jquery-ui.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/animate.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/style.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/style-responsive.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/theme/default.css" rel="stylesheet" id="theme" />
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/media/css/dataTables.bootstrap.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/css/buttons.bootstrap.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Responsive/css/responsive.bootstrap.min.css" rel="stylesheet" />
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/pace/pace.min.js"></script>

    <style>
    </style>
</head>

<body>
    <?php include "topo.php";
    // include_once "func_painel.php";
    // include_once "func_painel_inadimplencia.php";
    include "conexao.php";

    function soNumero($str)
    {
        return preg_replace("/[^0-9]/", "", $str);
    }

    function telephone($number)
    {
        $number = "(" . substr($number, 0, 2) . ") " . substr($number, 2, -4) . " - " . substr($number, -4);
        // primeiro substr pega apenas o DDD e coloca dentro do (), segundo subtr pega os números do 3º até faltar 4, insere o hifem, e o ultimo pega apenas o 4 ultimos digitos
        return $number;
    }

    $query = mysqli_query($db, "SELECT hash FROM usuario_sms WHERE status = 1 ");
    if (mysqli_num_rows($query)) {
        $recebe = mysqli_fetch_assoc($query);
        $hash = $recebe['hash'];

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

            $mensagem = $_POST['texto_envio'];

            foreach ($_POST['check_list'] as $numeros) {
                $numero = soNumero($numeros);
                //Estrutura do json para fazer a requisição.
                $dado = [];
                $dados = [];
                $dados['acao'] = 'enviar';
                $dados['hash'] = $hash;
                $dados['mensagem'] = utf8_encode($mensagem);
                $dados['numero'] = $numero;
                $msg_get = urlencode($mensagem);

                // print_r($dados);
                // die();

                $dado[] = json_encode($dados);
                
                $ch = curl_init("https://smsmobile.com.br/sms/shortcode/routes/sms.php?hash=".$hash."&numero=".$numero."&mensagem=".$msg_get);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

                $server_output = curl_exec($ch);
                $info = curl_getinfo($ch);

               // print_r($info);
               //  die();

                curl_close($ch);








            }

            $server_output = json_decode($server_output, true);
            if ($server_output['status'] == "Sucesso") {
                $status = 1;
            } else {
                $status = 0;
            }
        }
    }


    ?>

    <form action="envio_manual_sms.php" method="POST" id="envia_sms">
    </form>

    <div id="page-container" class="fade in page-sidebar-fixed page-header-fixed">
        <div id="content" class="content">
            <div class="row">
                <div class="col-md-12">
                    <div class="panel panel-inverse">
                        <div class="panel-heading">
                            <h4 class="panel-title">Tabela de Contatos</h4>
                        </div>
                        <div class="panel-body" style="overflow-y: scroll; height: 320px; white-space:nowrap;">
                            <table id="data-table" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th style="text-align: center;">Nome</th>
                                        <th style="text-align: center;">Telefone</th>
                                        <th style="text-align: center;">
                                            Selecione
                                            <input type="checkbox" id="check_all">
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>

                                    <?php

                                    // Aqui vou listar todos os contatos.
                                    $query = mysqli_query($db, "SELECT * FROM cliente LEFT JOIN telefone_cli ON cliente.idcliente = telefone_cli.id_cliente");

                                    if (mysqli_num_rows($query)) {
                                        foreach ($query as $value) {
                                            if (!empty($value['celular'])) {
                                                if (strlen($value['celular']) == 11) {
                                                    $value['celular'] = telephone($value['celular']);
                                                }
                                    ?>
                                                <tr>
                                                    <td style="text-align: center;"><?php echo $value['nome_cli']; ?></td>
                                                    <td style="text-align: center;"><?php echo $value['celular']; ?></td>
                                                    <td style="text-align: center;"><input type="checkbox" name="check_list[]" value="<?php echo $value['celular']; ?>" form="envia_sms"></td>
                                                </tr>
                                    <?php } } } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="panel panel-inverse">
                        <div class="panel-heading">
                            <h4 class="panel-title">Mensagem do Envio</h4>
                        </div>
                        <div class="panel-body">
                            <div class="form-group">
                                <textarea name="texto_envio" form="envia_sms" class="form-control" placeholder="Digite sua mensagem aqui . . ." style="resize: none"></textarea>
                            </div>
                            <div class="form-group" align="right">
                                <input class="btn btn-success m-r-5" type="submit" name="envia_sms" form="envia_sms">
                            </div>
                        </div>
                    </div>

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

    <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-migrate-1.1.0.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-ui/ui/minified/jquery-ui.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-cookie/jquery.cookie.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/ionRangeSlider/js/ion-rangeSlider/ion.rangeSlider.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-colorpicker/js/bootstrap-colorpicker.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/masked-input/masked-input.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-timepicker/js/bootstrap-timepicker.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/password-indicator/js/password-indicator.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-combobox/js/bootstrap-combobox.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-select/bootstrap-select.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-tagsinput/bootstrap-tagsinput.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-tagsinput/bootstrap-tagsinput-typeahead.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-tag-it/js/tag-it.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-daterangepicker/moment.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-daterangepicker/daterangepicker.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/select2/dist/js/select2.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap-eonasdan-datetimepicker/build/js/bootstrap-datetimepicker.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/js/form-plugins.demo.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/masked-input/masked-input.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/media/js/jquery.dataTables.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/media/js/dataTables.bootstrap.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/dataTables.buttons.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/buttons.bootstrap.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/buttons.flash.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/jszip.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/pdfmake.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/vfs_fonts.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/buttons.html5.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Buttons/js/buttons.print.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/DataTables/extensions/Responsive/js/dataTables.responsive.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/js/table-manage-buttons.demo.min.js"></script>
    <script src="https://immobilebusiness.com.br/admin/assets/js/apps.min.js"></script>
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>


    <script>
        $(document).ready(function() {
            App.init();
            TableManageButtons.init();

            $('input#check_all').click(function(){
                if($(this).is(':checked')){
                    $('input[form="envia_sms"]').each(function(){
                        $(this).attr('checked', 'checked');
                    });
                }else{
                    $('input[form="envia_sms"]').each(function(){
                        $(this).removeAttr('checked');
                    });
                }
            });

           
        });
    </script>
    
    <?php

    if ($status != '') {
        switch ($status) {
            case '0':
    ?>
                <script>
                    swal("!Erro",
                        "Seus SMS não foram enviados.",
                        "error")
                </script>
            <?php
                break;
            case '1':
            ?>
                <script>
                    swal("Sucesso!",
                        "Seus SMS foram enviados.",
                        "success")
                </script>
    <?php
                break;
            default:
                break;
        }
    }

    ?>

</body>

</html>