<?php
/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\IpAuth[]|\Cake\Collection\CollectionInterface $ipAuths
 */
?>
<div class="ipAuths index content">
    <?= $this->Html->link('Nueva autorización', ['action' => 'add'], ['class' => 'button float-right']) ?>
    <h3>Autorizaciones</h3>
    <div class="table-responsive">
        <table>
            <thead>
                <tr>
                    <th><?= $this->Paginator->sort('ip') ?></th>
                    <th><?= $this->Paginator->sort('auth', 'Autorización') ?></th>
                    <th><?= $this->Paginator->sort('token') ?></th>
                    <th><?= $this->Paginator->sort('created', 'Registrado') ?></th>
                    <th class="actions">Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($ipAuths as $ipAuth): ?>
                <tr>
                    <td><?= h($ipAuth->ip) ?></td>
                    <td>
                        <?php
                            if($ipAuth->auth){
                                $class = 'warning';
                                $text = 'Desactivar';
                            }else{
                                $class = 'success';
                                $text = 'Activar';
                            }
                        ?>
                        <button class="btn btn-<?= $class ?> auth" id="<?= $ipAuth->id ?>" action="<?= (int)!$ipAuth->auth ?>"><?= $text ?></button>
                    </td>
                    <td><?= h($ipAuth->token) ?></td>
                    <td><?= h($ipAuth->created) ?></td>
                    <td class="actions">
                        <?= $this->Form->postLink('Token', ['action' => 'changeToken', $ipAuth->id], ['confirm' => __('¿Desea cambiar el token de la ip {0}?', $ipAuth->ip)]) ?>
                        <?= $this->Form->postLink('Eliminar', ['action' => 'delete', $ipAuth->id], ['confirm' => __('¿Desea eliminar la ip {0} de la lista de acceso?', $ipAuth->ip)]) ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?= $this->element('paginator') ?>
</div>
<script type="text/javascript">
    $(function(){

        $('.auth').on('click', function(){
            let url = "<?= $this->Url->build(['action' => 'authChange']) ?>";
            let id = $(this).attr('id');
            let action = parseInt($(this).attr('action'));
            let tag = $(this);

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

            alert(resp.mssg);

            if(resp.result){
                if(!action) {
                    tag.removeClass('btn-warning').addClass('btn-success').text('Activar');
                    valAction = 1;
                }else {
                    tag.removeClass('btn-success').addClass('btn-warning').text('Desactivar');
                    valAction = 0;
                }

                tag.attr('action', valAction);

            }

        });

    });
</script>