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

?>
<!DOCTYPE html>
<html>

<head>
    <meta 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" />

    <link href="http://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>

</head>

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

    $esconde = 0;

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

        $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'];

            //Estrutura do json para fazer a requisição.
            $dados['acao']            = 'consultar';
            $dados['hash']            = $hash;
            $dados['data_inicio']     = $_POST['data_inicio'];;
            $dados['data_fim']        = $_POST['data_fim'];;
            $dados['status']          = $_POST['status'];

            $dado[] = json_encode($dados);

            $ch = curl_init("https://smsmobile.com.br/sms/shortcode/routes/sms.php");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $dado);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $relatorio = curl_exec($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);

            $relatorio = json_decode($relatorio, true);

            // print_r($relatorio);
            // die();
        }
        $esconde = 1;
    }
    ?>

    <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">
                            <h1 class="panel-title">Relatorio</h1>
                        </div>

                        <div class="panel-body">
                            <form action="relatorio_sms.php" method="POST">
                                <div class="form-group">
                                    <label>Selecione o Status</label>
                                    <select class="selectpicker form-control" name="status" data-style="btn-default">
                                        <option value="todos">Todos</option>
                                        <option value="enviado">SMS Enviado</option>
                                        <option value="nao_enviado"> SMS Não enviado</option>
                                        <option value="aguardando_envio">SMS Aguardando envio</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label>Selecione o Periodo</label>
                                    <div class="row">
                                        <div class="col-lg-6">
                                            <input type="date" class="form-control" name="data_inicio" placeholder="Data Inicio" />
                                        </div>
                                        <div class="col-lg-6">
                                            <input type="date" class="form-control" name="data_fim" placeholder="Data Fim" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <input class="btn btn-primary" type="submit" name="enviar" value="Enviar" />
                                </div>
                            </form>
                        </div>
                        <?php

                        if ($esconde == 1) { ?>
                            <div style="overflow-y: scroll; height: 378px;">
                                <div class="panel-body">
                                    <table id="data-table" class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>STATUS</th>
                                                <th>NÚMERO</th>
                                                <th>MENSAGEM</th>
                                                <th>DATA</th>
                                                <th>HORA</th>
                                                <th>ERRO</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            // echo "<pre>";
                                            // print_r($relatorio);
                                            // echo "</pre>";

                                            if ($relatorio['status'] != 'Erro') {
                                                foreach ($relatorio["dados"] as $value) {

                                                    $value['data_envio'] = explode(' ', $value['data_envio']);

                                                    $data = $value['data_envio'][0];
                                                    $hora = $value['data_envio'][1];

                                                    $data = date('d/m/Y',  strtotime($data));

                                                    switch ($value['status_envio']) {
                                                        case '1':
                                                            $status = "Enviado";
                                                            break;
                                                        case '0':
                                                            $status = "Não Enviado";
                                                            break;
                                                        case '2':
                                                            $status = "Aguardando Envio";
                                                            break;

                                                        default:
                                                            $status = "N/A";
                                                            break;
                                                    }

                                                    $value['motivo_erro'] == '' ? $value['motivo_erro'] = 'N/A' : '';

                                            ?>
                                                    <tr class="odd gradeX">
                                                        <td class="pt-3-half"><?php echo $status; ?></td>
                                                        <td class="pt-3-half"><?php echo $value['numero']; ?></td>
                                                        <td class="pt-3-half"><?php echo utf8_decode($value['mensagem']); ?></td>
                                                        <td class="pt-3-half"><?php echo $data; ?></td>
                                                        <td class="pt-3-half"><?php echo $hora; ?></td>
                                                        <td class="pt-3-half"><?php echo $value['motivo_erro']; ?></td>

                                                    </tr>
                                            <?php }
                                                                        } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <a href="javascript:;" class="btn btn-icon btn-circle btn-success btn-scroll-to-top fade" data-click="scroll-top"><i class="fa fa-angle-up"></i></a>
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-1.9.1.min.js"></script>
    <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/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>
        $(document).ready(function() {
            App.init();
            TableManageButtons.init();
        });
    </script>

</body>

</html>