<?php
/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\SlowQuery[]|\Cake\Collection\CollectionInterface $slowQueries
 */
?>
<div class="slowQueries index content">
    <?= $this->Html->link('Mes', ['action' => 'index', '1'], ['class' => 'button float-right']) ?>
    <?= $this->Html->link('Semana', ['action' => 'index', '2'], ['class' => 'button float-right']) ?>
    <?= $this->Html->link('Día', ['action' => 'index', '3'], ['class' => 'button float-right']) ?>
    <h3><?= __('Slow Queries') ?></h3>
    <div class="table-responsive">
        <table>
            <thead>
                <tr>
                    <th><?= $this->Paginator->sort('user', 'Usuario') ?></th>
                    <th><?= $this->Paginator->sort('schema', 'Esquema') ?></th>
                    <th><?= $this->Paginator->sort('query_time', 'Ejecución') ?></th>
                    <th><?= $this->Paginator->sort('lock_time', 'Bloqueo') ?></th>
                    <th><?= $this->Paginator->sort('rows_sent', 'Enviados') ?></th>
                    <th><?= $this->Paginator->sort('rows_examined', 'Examinados') ?></th>
                    <th><?= $this->Paginator->sort('date', 'Fecha') ?></th>
                    <th><?= $this->Paginator->sort('created', 'Registrado') ?></th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($slowQueries as $slowQuery): ?>
                <tr>
                    <td><?= h($slowQuery->user) ?></td>
                    <td><?= h($slowQuery->schema) ?></td>
                    <td><?= h($slowQuery->query_time) ?></td>
                    <td><?= h($slowQuery->lock_time) ?></td>
                    <td><?= $this->Number->format($slowQuery->rows_sent) ?></td>
                    <td><?= $this->Number->format($slowQuery->rows_examined) ?></td>
                    <td><?= date('Y-m-d H:i:s', $slowQuery->date) ?></td>
                    <td><?= h($slowQuery->created) ?></td>
                    <td><button class="view-query" id="<?= $slowQuery->id ?>">Consulta</button></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?= $this->element('paginator') ?>
</div>
<!-- Trigger the modal with a button -->
<button id="btnModal" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" style="display: none;"></button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog modal-lg">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Query ejecutada</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        
      </div>
      <div class="modal-body">
        <p id="view-sql" style="word-wrap: break-word;"></p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
      </div>
    </div>

  </div>
</div>
<script type="text/javascript">
    $(function(){
        $('.view-query').on('click', function(e){
            e.preventDefault();

            let url = "<?= $this->Url->build(['action' => 'getQuery']) ?>";
            let id = $(this).attr('id');

            let resp = getRequest(url+'/'+id);

            if(resp.result){
                $('#view-sql').empty().text(resp.mssg);
                $('#btnModal').trigger('click');
            }else{
                alert(resp.mssg);
            }
        });
    });
</script>