<?php
ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
session_start();

if($_SESSION['store_type'] != 5){
    header("Location: users.php");
    exit();
}

error_reporting(32767);
include "includes/header.php";


// Verifique se o método da requisição é POST
if($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST['title'];
    $message = $_POST['message'];
    $link = $_POST['link'];

    $users = $dbpans->prepare("SELECT id FROM users WHERE store_type != 5");
    $result = $users->execute();

    $group_id = uniqid(time());


    if ($result) {


        while ($user = $result->fetchArray(SQLITE3_ASSOC)) {
            $account_id = $user['id'];
            $stmt = $dbpans->prepare("INSERT INTO notification (title, message, account_id, link,group_id) VALUES (:title, :message, :account_id, :link, :group_id)");
            $stmt->bindParam(':title', $title);
            $stmt->bindParam(':message', $message);
            $stmt->bindParam(':link', $link);
            $stmt->bindParam(':account_id', $account_id);
            $stmt->bindParam(':group_id', $group_id);
            $stmt->execute();

        }
    } else {
        echo "Erro ao buscar usuários.";
    }
    ?>

    <script>window.location.href="alert_users.php"</script>
    <?php
    header("Location: alert_users.php");
    exit;
}

?>
    <main role="main" class="col-15 pt-4 px-5"><div class="row justify-text-center"><div class="chartjs-size-monitor" style="position:absolute ; left: 0px; top: 0px; right: 0px; bottom: 0px; overflow: hidden; pointer-events: none; visibility: hidden; z-index: -1;"><div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0"></div></div><div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:200%;height:200%;left:0; top:0"></div></div></div>
            <div id="main">


                <!-- Page Heading -->
               <h2>Alerta</h2>

                <br>
            </div>
            <div class="table-responsive">
                <form action="#" method="post">
                    <div class="form-group">
                        <label for="title">Título:</label>
                        <input type="text" class="form-control" id="title" name="title">
                    </div>
                    <div class="form-group">
                        <label for="link">Link:</label>
                        <input type="text" class="form-control" id="link" name="link">
                    </div>
                    <div class="form-group">
                        <label for="message">Mensagem:</label>
                        <textarea class="form-control" id="message" name="message"></textarea>
                    </div>

                    <button type="submit" class="btn btn-primary">Salvar Alerta</button>
                </form>

            </div>
        </div></main>
<?php

include "includes/footer.php";
echo "\r\n<script>\r\n\$(\"#search\").keyup(function () {\r\n    var value = this.value.toLowerCase().trim();\r\n\r\n    \$(\"table tr\").each(function (index) {\r\n        if (!index) return;\r\n        \$(this).find(\"td\").each(function () {\r\n            var id = \$(this).text().toLowerCase().trim();\r\n            var not_found = (id.indexOf(value) == -1);\r\n            \$(this).closest('tr').toggle(!not_found);\r\n            return not_found;\r\n        });\r\n    });\r\n});\r\n</script>\n    <script>\n\$('#confirm-delete').on('show.bs.modal', function(e) {\n    \$(this).find('.btn-ok').attr('href', \$(e.relatedTarget).data('href'));\n});\n    </script>\n</body>\n\n</html>";

?>