<?php 
$db = new SQLite3('./api/user_message.db');
$db->exec("CREATE TABLE IF NOT EXISTS messages(id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, message VARCHAR(100), userid TEXT,status TEXT,expire TEXT)");
$res = $db->query('SELECT * FROM messages');

if(isset($_GET['delete'])){
$db->exec("DELETE FROM messages WHERE id=".$_GET['delete']);
header("Location: send_message.php");
}
include ('includes/header.php');
?>


	
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h2>Confirmar</h2>
            </div>
            <div class="modal-body">
                Deseja realmente deletar?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <a class="btn btn-danger btn-ok">Deletar</a>
            </div>
        </div>
    </div>
</div>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><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">
            <h2>Usuario mensagens</h2><a id="button" href="./create_message.php" class="btn btn-primary">Criar</a>
          </div>
		<div class="table-responsive">
			<table class="table table-striped table-sm">
			<thead>
				<tr>
				<th>Usuario</th>
				<th>Nome usuario</th>
				<th>Mensagem</th>
				<th>Status</th>
				<th>Espiração</th>
				<th>Deletar</th>
				</tr>
			</thead>
			<? while ($row = $res->fetchArray()) {?>
			<tbody>
				<tr>
				<td><?=$row['id'] ?></td>
				<td><a href="./update_message.php?update=<?=$row['id'] ?>"><?=$row['userid'] ?></a></td>
				<td><?=$row['message'] ?></td>
				<td><?=$row['status'] ?></td>
				<td><?=$row['expire'] ?></td>
				<td><a class="btn btn-danger btn-ok" href="#" data-href="./send_message.php?delete=<?=$row['id'] ?>" data-toggle="modal" data-target="#confirm-delete"><i class="fas fa-fw fa-chart-area"></i>X</a></td>
				</tr>
			</tbody>
			<? }?>
			</table>
		</div>
</main>

    <br><br><br>
<?php include ('includes/footer.php');?>
    <script>
$('#confirm-delete').on('show.bs.modal', function(e) {
    $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});
    </script>
</body>

</html>